To continue on from my previous posts about the cheap wireless AC outlets, here is some code for a simple web interface that will let you turn on and off the outlets via buttons on a web page.
- Setup a web server on your RaspberryPi with PHP.
- Move the compiled ‘send’ command using
sudo mv ~/rcswitch-pi/send /usr/local/bin
- Edit the sudoers file with:
sudo visudo
- and append the line:
www-data ALL = NOPASSWD:/usr/local/bin/send
- Create the following php file in the web server directory (updated code here) Eg:
/var/www/html/index.php
<html><head><title>Home Automation</title></head> <body> <?php $pageName = basename($_SERVER["SCRIPT_NAME"]); $pin = 0; if (isset($_GET["on"])) { $state = 1; $id = $_GET["on"]; } else if (isset($_GET["off"])) { $state = 0; $id = $_GET["off"]; } if (isset($state)) { $cmd = "sudo /usr/local/bin/send ".$pin." ".$id." ".$state; exec($cmd); header('Location: '.$pageName); } ?> <table cellspacing='20'> <tr> <td><b>Light 1</b></td> <td><a href='<?php echo $pageName; ?>?off=0'><img width='100' src='off_.png' alt='off'/></a></td> <td><a href='<?php echo $pageName; ?>?on=0'><img width='100' src='on_.png' alt='on'/></a></td> </tr> <tr> <td><b>Light 2</b></td> <td><a href='<?php echo $pageName; ?>?off=1'><img width='100' src='off_.png' alt='off'/></a></td> <td><a href='<?php echo $pageName; ?>?on=1'><img width='100' src='on_.png' alt='on'/></a></td> </tr> </table> </body> </html>
The file can be called anything.php and it should still work. If you need more buttons, simply copy and paste more entries in the table. Have fun!
You should write the post clearly.