Advertisements

Latest comments

Saturday, 11 August 2012 16:59

PHP Control Arduino with TP-Link TL-WR1043ND Router

My previous article Control Arduino via PHP (EasyPHP) control Arduino from a computer with web server. Another article Control Arduino with TP-Link TL-WR1043ND Router which the Arduino is connecting to the router, but it is not safely to control over the internet.

 
Now I want to control Arduino without a computer and it is very safely control over the internet. Here is the step.
 
 
Hardware
  • TP-LINK TL-WR1043ND
  • Arduino Duemilanove
Arduino Code
The Arduino code is getting from Ocean Control so you don't have to code yourself. Of course you can write your own Arduino code too.
 
 
First, you must follow link below to upgrade firmware to Openwrt & install PHP to the router.
Upgrade Firmware to OpenWrt
 
Install PHP to Router
 
Open USB-Serial Port
This is particular important, it use to prevent autoreset on initiation of serial. Follow step below to make the /dev/ttyUSB0 start on every router reboot. 
  1. Login to router
  2. Click on Sytem
  3. Select Startup 
  4. Your will see a list of startup software, scroll down the screen until you see a box for enter command.
  5. Enter cat/dev/ttyUSB0 in the box, just in front exit 0.
  6. Click on Submit button to save the settings.
  7. Reboot the router, everything is ready, just upload PHP code to the router will do.
As shown in figure below, the cat /dev/ttyUSB0 is save in the startup.
Openwrt StartupOpenwrt Startup
 
 
Copy PHP code to router
PHP code must save in www folder of router, below is an example of PHP code to control Arduino, save it as arduino.php.
<?php
function openSerial($command) {
$openSerialOK = false;
try {
$fp = fopen('/dev/ttyUSB0','r+'); //use this for Linux
$openSerialOK = true;
} catch(Exception $e) {
echo 'Message: ' .$e->getMessage();
}
 
if($openSerialOK) {
fwrite($fp, $command); //write string to serial
fclose($fp);
    } 
}
 
openSerial("Without this line, the first control will not work. I don't know why.");
 
if(isset($_POST['submit1'])) {
    openSerial("@00 on 1\r");
}
 
if(isset($_POST['submit2'])) {
    openSerial("@00 of 1\r");
}
 
if(isset($_POST['submit3'])) {
    openSerial("@00 on 2\r");
}
 
if(isset($_POST['submit4'])) {
    openSerial("@00 of 2\r");
}
?>
 
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
   <input type="submit" name="submit1" value="1 on"><br>
   <input type="submit" name="submit2" value="1 off"><br>
   <input type="submit" name="submit3" value="2 on"><br>
   <input type="submit" name="submit4" value="2 off"><br>   
</form>
<?php
function openSerial($command) {
$openSerialOK = false;
try {
$fp = fopen('/dev/ttyUSB0','r+'); //use this for Linux
$openSerialOK = true;
} catch(Exception $e) {
echo 'Message: ' .$e->getMessage();
}
 
if($openSerialOK) {
fwrite($fp, $command); //write string to serial
fclose($fp);
    } 
}
 
openSerial("Without this line, the first control will not work. I don't know why.");
 
if(isset($_POST['submit1'])) {
    openSerial("@00 on 1\r");
}
 
if(isset($_POST['submit2'])) {
    openSerial("@00 of 1\r");
}
 
if(isset($_POST['submit3'])) {
    openSerial("@00 on 2\r");
}
 
if(isset($_POST['submit4'])) {
    openSerial("@00 of 2\r");
}
?>
 
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
   <input type="submit" name="submit1" value="1 on"><br>
   <input type="submit" name="submit2" value="1 off"><br>
   <input type="submit" name="submit3" value="2 on"><br>
   <input type="submit" name="submit4" value="2 off"><br>   
</form>
 
  
Using WinSCP, arduino.php is copy to the router as shown in figure below.
Using WinSCP to copy arduino.php from computer to routerUsing WinSCP to copy arduino.php from computer to router
 
 

Run the PHP code

  • Run internet explorer
  • Enter 192.168.1.1:81/arduino.php

Following screen will appear. Play arround the button, you will see the LED is turning on and off.

 

You may want to read this article if you want to control the Arduino outside your Local Area Network.

http://ediy.com.my/index.php/tutorials/item/20-port-forward-control-device-over-internet

 

Authentication

The final part is how to add user name and password before control the Arduino.

Here http://php.net/manual/en/function.session-start.php are some examples.

You must install session package to the router before you can use authentication.

opkg install php5-mod-session

 

At the end I provide an example on session authentication to control the Arduino. Most of the code is copy from http://php.net/manual/en/features.http-auth.php

  1. Download PHP source code
  2. Extract to www folder of Router
  3. Run Internet Explorer
  4. Enter http://192.168.1.1:81/arduino or http://192.168.1.1:81/index.php

 

Note:

  • Make sure enter http:// in front of router IP address (192.168.1.1) if you are not using port 80
  • Make sure you add index.php to index-file.name section of /etc/lighttpd/lighttpd.conf configuration file as shown below. Read here fore more information.
index-file.names = ( "index.php", "index.html", "default.html", "index.htm", "default.htm" )
index-file.names = ( "index.php", "index.html", "default.html", "index.htm", "default.htm" )
Read 24956 times Last modified on Monday, 07 October 2013 22:54

Comments  

how do air
#6 how do air 2014-09-08 18:06
I feel this is one of the most significant information for me.
And i am happy reading your article. But should commentary
on few general issues, The web site style is wonderful, the articles is really great : D.
Just right task, cheers
Quote
hoatienii
#5 hoatienii 2014-09-01 16:37
how can add more button?
Quote
hoatienii
#4 hoatienii 2014-09-01 16:32
Quoting sakatgg:
Tested this way, relay on and off, as much as possible in the process learn the status of the relay?
In www file appeared com4 (edit - @ 00 of 1) what is the function of?


you use this relay board?

dhgate.com/.../140173676.html
Quote
Admin
#3 Admin 2014-07-24 08:39
It uses 2 characters for command. Therefore "of" meaning off (turn off)
Quote
sakatgg
#2 sakatgg 2014-07-24 05:32
Tested this way, relay on and off, as much as possible in the process learn the status of the relay?
In www file appeared com4 (edit - @ 00 of 1) what is the function of?
Quote
Payday Loans
#1 Payday Loans 2013-03-02 14:06
Keep this going please, great job!
Quote

Add comment


Security code
Refresh