Home Home Automation
Home Automation
PHP interface to X10 Firecracker through Bottlerocket PDF Print E-mail
Written by David McAndrews   
Wednesday, 01 July 2009 22:28

This code can be found at the following location: 

http://www.sosdg.org/sites/sosdg.org/files/x10.php_1.txt

 Here is the code itself:

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>PHP-BottleRocket/SOSDG - X10 Control Interface</title> <style type="text/css"> <!-- .footer {    font-family: Arial, Helvetica, sans-serif;    font-size: 9px;    font-style: italic;    color: #666666; } .header {    font-family: Arial, Helvetica, sans-serif;    font-size: medium;    font-weight: bold; } .listitem {    font-family: Arial, Helvetica, sans-serif;    font-size: 12px;    font-style: normal;    color: #000000; } .cmdline {    font-family: "Courier New", Courier, monospace;    font-size: 10px;    font-style: normal;    color: #000000; } .noborder4 {    border-top-style: none;    border-right-style: none;    border-bottom-style: none;    border-left-style: none;    padding: 2px; } .error {    font-family: Arial, Helvetica, sans-serif;    font-size: 14px;    font-style: italic;    font-weight: bold;    color: #FF0000; } .noborder2 {    class=\"noborder2\"border-top-style: none;    border-right-style: none;    border-bottom-style: solid;    border-left-style: none;    border-bottom-width: thin;    border-top-style: none;    border-top-width: thin;    border-right-width: thin;    border-left-width: thin;    padding: 2px; } .noborder3 {    class=\"noborder2\"border-top-style: none;    border-right-style: none;    border-bottom-style: solid;    border-left-style: solid;    border-bottom-width: thin;    border-top-style: none;    border-top-width: thin;    border-left-width: thin;    border-right-width: thin;    padding: 2px; } .noborder3t {    border-top-width: thin;    border-right-width: thin;    border-bottom-width: thin;    border-left-width: thin;    border-top-style: none;    border-right-style: none;    border-bottom-style: solid;    border-left-style: none;    padding: 2px; } .noborder2r {    class=\"noborder2\"border-top-style: none;    border-right-style: solid;    border-bottom-style: solid;    border-left-style: none;    border-bottom-width: thin;    border-top-style: none;    border-top-width: thin;    border-right-width: thin;    border-left-width: thin;    padding: 2px; } --> </style> </head> <body> <?php /*   PHP-BottleRocket/SOSDG By Brielle Bruns <
  This e-mail address is being protected from spambots. You need JavaScript enabled to view it
 > http://www.sosdg.org  License: GPLv2 URL: http://www.gnu.org/licenses/gpl-2.0.txt  ChangeLog: 0.1-boihme  (3/11/2009) =   Initial release, modified slightly from script made for home use 0.2-boihme  (3/12/2009) =   added new arrays for Home Codes and added somewhat working Dim features.                      Still some broken code commented out for now. 0.3         (3/13/2009) =   UTF-8, code cleanups thanks to a quick refresher on PHP syntax, CSS code                             for nicer (IMHO) output, added global controls.  For some odd reason, All                             Devices On/Off doesn't seem to affect the appliance module...  Merge in some                             code I was reserving for my home X10 setup with mainline...  So, no need to have -boihme                             fork. 0.4         (3/14/2009) =   Replaced print_r() with a foreach to print out debug from command, make errors not                             completely fatal, setting $tilt will prevent command from actually running on the                      system, comments, more interface tweaks  Known Issues:    * My code sucks.  I know.    * My CSS sucks.  I know.   */  // You can edit stuff in this section to customize parts of the script  // This is the location of bottlerocket $brloc="/usr/bin/br";  // Show debugging info, including command line and output.  Turn off if you need pretty output $debug=1;  // This is the section where you can define home codes and their associated modules. $comments = array (    "A"   => array( 1 => "Receiver Outlet",                2 => "LED Living Room",                3 => "Ceiling Fan Light Living Room"               ) );   // This is not really meant to be user modified - this is the standard X10 home codes // Though, if you really wanted to, this is defined as: // "Home Code" => "Display Name" // Change display name to whatever you want, but don't mess with the home code field. $hcarray = array ("A"=>"A","B"=>"B","C"=>"C","D"=>"D","E"=>"E","F"=>"F","G"=>"G","H"=>"H","I"=>"I",               "J"=>"J","K"=>"K","L"=>"L","M"=>"M","N"=>"N","O"=>"O","P"=>"P");  // The internal version number and name, change if you modify this from the stock $program_name="PHP-BottleRocket/SOSDG"; $interface_version="v0.4";  // Define this script as the actions for POST $selfscript=$_SERVER['PHP_SELF'];  // If we are working with a device number, make sure it's an integer and not a string if (!empty($_REQUEST[device])) {    $device=(int)$_REQUEST[device]; }   // Process homecode if present in POST, if not, assume first load & 'A' if (!empty($_REQUEST[homecode])) {    if (ereg("^[A-P]$",$_REQUEST[homecode])) {       $homecode=$_REQUEST[homecode];    } else {       $inputerr="Error: invalid homecode";    } } else {    $homecode='A'; }   // If we have debug on, then we add some fun output adjustments if (debug) {       $debugcmd=" -vvv "; }  // DIm POST request if (!empty($_REQUEST[dimact])) {    $dim=(int)$_REQUEST[dim];    if (is_int($device)) {       $cmddev=$device;    } else {       $inputerr="Error: Invalid device ID";    }    $cmdline=stripslashes(strip_tags(escapeshellcmd("$brloc $debugcmd --house=$homecode --dim=$dim,$device"))); }  // Device/Lamp On/Off POST request if (!empty($_REQUEST[action])) {    $action=$_REQUEST[action];    unset($dim);    unset($cmdline);    if ($action == "On") {       $cmdact=" --on=";    } elseif($action == "Off") {       $cmdact=" --off=";    } else {       $inputerr="Error: Invalid action";    }    if (is_int($device)) {       $cmddev=$device;    } else {       $inputerr="Error: Invalid device ID";    }    $cmdline=stripslashes(strip_tags(escapeshellcmd("$brloc $debugcmd --house=$homecode $cmdact$cmddev"))); }  // All Lights On/Off POST Request if (!empty($_REQUEST[AllLight])) {    unset($cmdline);    unset($cmdact);    $alllight=$_REQUEST[AllLight];    if ($alllight == "On") {       $cmdact=" --lamps_on";    } elseif ($alllight == "Off") {       $cmdact=" --lamps_off";    } else {       $inputerr="Error: Invalid action";    }    $cmdline=stripslashes(strip_tags(escapeshellcmd("$brloc $debugcmd --house=$homecode $cmdact"))); }  // All Devices On/Off POST Request if (!empty($_REQUEST[AllDevice])) {    unset($cmdline);    unset($cmdact);    $alllight=$_REQUEST[AllDevice];    if ($alllight == "On") {       $cmdact=" --ON";    } elseif ($alllight == "Off") {       $cmdact=" --OFF";    } else {       $inputerr="Error: Invalid action";    }    $cmdline=stripslashes(strip_tags(escapeshellcmd("$brloc $debugcmd --house=$homecode $cmdact"))); }   // If we have some error, set tilt, and display error if (!empty($inputerr)) {    echo "<span class=\"error\">$inputerr</span><br \>\n";    $tilt=1;    //exit(1); }  // Run the actual command and put the output into $output so if we // are in debug mode, we can do something with it.  Don't run the command // if we see that the tilt bit is set. if (!empty($cmdline) && empty($tilt)) {    exec("$cmdline", $output); } ?> <span class="header"><?php echo $program_name; ?> X10 Home Control Interface</span><br /> <table width="90%" border="0" cellpadding="0" cellspacing="0">   <tr>     <td colspan="3" valign="middle" class="noborder4"><form id="homecode" name="homecode" method="post" action="<?php echo $selfscript; ?>">         <label><span class="listitem">Home Code</span><span class="listitem">         <select name="homecode">         <?php foreach ($hcarray as $hcvalue => $hcname) {             if ($hcvalue == $homecode) {             echo "<option value=\"$hcvalue\" selected=\"selected\">$hcname</option>\n";          } else {             echo "<option value=\"$hcvalue\">$hcname</option>\n";          }       }          ?>         </select>         </span></label>         <span class="listitem">         <input name="Change" type="submit" id="Change" value="change" />         </span>       </form></td>     <td valign="middle" class="listitem"><form id="all" name="all" method="post" action="<?php echo $selfscript; ?>">         <input name="homecode" type="hidden" value="<?php echo $homecode ?>" />         All Lamps:         <input name="AllLight" type="submit" id="LOn" value="On" />         <input name="AllLight" type="submit" id="LOff" value="Off" />              All Devices:         <input name="AllDevice" type="submit" id="AllDevice" value="On" />         <input name="AllDevice" type="submit" id="AllDevice" value="Off" />       </form>       </td>   </tr>   <tr>     <th width="4%" class="noborder3t" scope="col">#</th>     <th width="12%" class="noborder2" scope="col">Action</th>     <th width="25%" class="noborder2" scope="col">Dim Level </th>     <th class="noborder2" scope="col">Comment</th>   </tr>   <?php     for ( $counter = 1; $counter <= 16; $counter++ ) {       echo "<tr><td class=\"noborder3\"><span class=\"listitem\"><div align=\"center\"> $homecode $counter </div></span></td>\n";       echo "<td class=\"noborder2\"><form id=\"$counter\" name=\"$counter\" method=\"post\" action=\"$selfscript\">\n";       echo "<input name=\"device\" type=\"hidden\" value=\"$counter\" />\n";       echo "<input name=\"homecode\" type=\"hidden\" value=\"$homecode\" />\n";       echo "<span class=\"listitem\"><div align=\"center\"><input name=\"action\" type=\"submit\" id=\"action\" value=\"On\" />\n";       echo "<input name=\"action\" type=\"submit\" id=\"action\" value=\"Off\" /></div></span>\n";       echo "<td class=\"noborder2\"><span class=\"listitem\"><div align=\"center\">\n";       echo "<span class=\"dimlist\"><select name=\"dim\">\n";       for ( $dimctr = -12; $dimctr <= 12; $dimctr++ ) {          // This code doesn't work right, so its disabled for now.          //if ($dimctr == $dim && $counter == $device ) {          // echo "<option value=\"".$dimctr."\" selected=\"selected\">".$dimctr."</option>\n";          //} elseif {          if ( $dimctr == '0') {             echo "<option value=\"$dimctr\" selected=\"selected\">$dimctr</option>\n";          } else {             echo "<option value=\"$dimctr\">$dimctr</option>\n";          }       }       echo "<input name=\"dimact\" type=\"submit\" id=\"action\" value=\"Dim\" />";       echo "</select></span>\n";       echo "</div></span></td>\n";       echo "</form></td>\n";       echo "<td class=\"noborder2r\"><span class=\"listitem\">".$comments[$homecode][$counter]."</div></td>\n";    }   ?> </table> </p> <?php // Only display this table if we have debug turned on    if (!empty($debug) && !empty($cmdline)) {       echo "<p><table width=\"90%\" border=\"1\"><tr><td><span class=\"cmdline\">Command Line: $cmdline<br />";       if (is_array($output)) {          foreach( $output as $lineout){             echo "$lineout <br />";          }       }    echo "</span></td></tr></table></p>"; } ?> <p> </p> <p><span class="footer"><?php echo "$program_name $interface_version"; ?><br />   Copyright © <a href="mailto:
  This e-mail address is being protected from spambots. You need JavaScript enabled to view it
 ">Brielle Bruns</a> of <a href="http://www.sosdg.org">The Summit Open Source Development Group</a><br />   This program is licensed under the <a href="http://www.gnu.org/licenses/gpl-2.0.txt">GPLv2 license</a>. </span><br /> </p> </body> </html>

 
 
X10 Firecracker & Bottlerocket PDF Print E-mail
Written by David McAndrews   
Wednesday, 01 July 2009 12:59

X10 manufactures home automation products that are fairly cheap, easy to setup & use, and very handy & useful.  These products allow you to control lights & appliances from the touch of a button on a remote control. A radio frequency (RF) signal is used by the remote controls so no direct line of sight is needed. When using an infrared (IR) remote, like most TV remotes, you must point the remote directly at the device to be controlled.  With X10, the remote control sends a signal to a transmitting device that is plugged into a standard 110v house outlet.  This transceiver will then send a signal through the electrical wiring to control a specific device.

A computer can also act as your remote control if an interface, such as the firecracker, is used. This device plugs into a standard 9-pin serial port. X10 provides software that allows you to control your X10 devices as you would from a remote control.  This is nice, but it gets much more interesting if you have a linux machine on your home network. If you plug the firecracker into a linux machine you can control your X10 devices from the linux machine... and also from anywhere you have access to this linux machine.

I have a linux server in my house that is accessible from the internet.  Just yesterday I plugged in my firecracker to this Ubuntu linux server.  After installing the bottlerocket program (apt-get install bottlerocket), I was quickly controlling my X10 devices from a terminal session connected to the server.  And taking this one step further, using a PHP program placed on the webserver running on this machine - I can now control my X10 devices from a webpage that is accessible from the internet.

X10 links: 

Bottlerocket:

PHP-BottleRocket/SOSDG

http://www.sosdg.org/freestuff/phpbr-x10 

http://www.sosdg.org/sites/sosdg.org/files/x10.php_1.txt
 
Last Updated on Thursday, 02 July 2009 00:28