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.
Upgrade Firmware to OpenWrt
|
Install PHP to Router
|
<?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
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
Note:
index-file.names = ( "index.php", "index.html", "default.html", "index.htm", "default.htm" ) |