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.
- TP-LINK TL-WR1043ND
- Arduino Duemilanove
Upgrade Firmware to OpenWrt
|
Install PHP to Router
|
-
Login to router
-
Click on Sytem
-
Select Startup
-
Your will see a list of startup software, scroll down the screen until you see a box for enter command.
-
Enter cat/dev/ttyUSB0 in the box, just in front exit 0.
-
Click on Submit button to save the settings.
-
Reboot the router, everything is ready, just upload PHP code to the router will do.

<?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>
|
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
- Download PHP source code
- Extract to www folder of Router
- Run Internet Explorer
- 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" ) |
- router_arduino.zip (1020 Downloads)