Simple Home Automation Web Interface [Part 3]

home_auto

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.

  1. Setup a web server on your RaspberryPi with PHP.
  2. Move the compiled ‘send’ command using sudo mv ~/rcswitch-pi/send /usr/local/bin
  3. Edit the sudoers file with: sudo visudo
  4. and append the line:  www-data ALL = NOPASSWD:/usr/local/bin/send
  5. 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!

Advertisement
This entry was posted in Automation and tagged , , , , . Bookmark the permalink.

1 Response to Simple Home Automation Web Interface [Part 3]

  1. Mark Toniz says:

    You should write the post clearly.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s