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 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 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 60595 times Last modified on Monday, 07 October 2013 22:54
Back to Top