Remotely controlling lights, etc. from a smartphone

I’ve been thinking for a while how nice it’d be to be able to remotely turn my front walk light on when it’s dark out and nobody is home. I’d been considering buying a home automation kit that integrates with a Mac or PC but figured there must be an easier (and cheaper) way of doing it. Then it dawned on me that since I already have a linux server running at home, and have some X10 modules and a serial interface that I’ve got everything I need, so here’s what I did. First, what you need:

  • A linux system running Apache or another web server that supports PHP.
  • The x10 Firecracker kit.  You’ll need both the Firecracker module and the x10 transceiver.
  • One or more x10 modules to control remotely.
  • The Bottlerocket software (I used version 0.04c) that communicates with the Firecracker
  • My x10 PHP code.  Use it as-is or use it as a template for writing your own.  It’s GPL’d, so have at it.

I’ll leave it up to you to set up your web server & PHP support.  There are plenty of resources for it on the web.

Download and install the Bottlerocket software.  Plug the Firecracker module into your PC’s serial port, plug the transceiver into a wall outlet and set it’s house code to whatever you want.  Use the Bottlerocket software to ensure everything is working.  It’s easiest to just try this as root:

# /usr/local/bin/br A7 ON
# /usr/local/bin/br A7 OFF

The above commands should toggle the device with address A7 on and off.  If you encounter any problems then make sure the Home code on both the transceiver and any other x10 modules are all set the same and make sure you’re using the right unit number.  Also make sure the serial port is enabled and nothing else is currently using it.

Make sure the serial device can be written to by your web server.  For some reason on my CentOS system the serial console is set to be writable only by the root owner and uucp group.  Apache is running using the apache user, so all I did was add the apache user to the uucp group then restarted Apache.  If you want to test this then simply su to the apache user or use sudo to invoke /usr/local/bin/br as apache and make sure it can also control your x10 device(s).

Install the two files from x10php.tar.gz somewhere on your web server.  The x10.php file you shouldn’t need to modify (unless you really want to).  Edit x10-config.php and make sure the $cmd variable points to the br binary, and also include any optional parameters you might want to include.  Modify the $x10_device and $x10_group arrays to suit your needs.  Their format should be very self-explanatory.

Test the PHP scripts by pointing a web browser to the appropriate URL.  You should see a screen that looks something like this:

x10menu

Click on the links to test the devices.  If you have PHP properly installed and set up the Bottlerocket, Firecracker, and permissions on your serial port properly then the devices should respond as expected.

Now for the really neat part…  If you have wifi and a wifi-enabled smart-phone or PDA that you carry with you regularly then you can bookmark URL’s that let you quickly & easily turn devices on and off.  For example, using the default settings in the x10-config.php file you would turn on the Front Walk lights via the following URL:

http://(your_linux_box)/x10.php?cmd=ON&id=D16

Just add that as a bookmarked link and call it something like “Turn on Front Walk lights” and if you’re outside and need the lights just pull out your smartphone and click on the link.   As long as you’re close enough to your wifi it should work without any problems.

Of course if you have a cable modem, FiOS, etc. you should be able to set things up to do this from anywhere, so you can turn on your lights as you’re heading home from the store or work or whatever.  If you’re going to do this then I’d suggest a few things:

  • Set up basic authentication in your web browser to require a userid & password to access x10.php, otherwise anybody who figures out the URL could turn your devices on/off.
  • Use SSL in conjunction with the above to ensure the userid & password is encrypted when it’s sent.
  • Make sure your linux box is firewalled (but open port 80 or 443 or whatever you decide to use) and properly patched.

If you decide to do the above then here’s another simple but effective trick.  First, use a service like DynDNS to set up an easy to remember DNS name for your server.  Once you’ve done that, then modify the URL’s in your smartphone to include your userid & password so you don’t have to always remember them.  (Many browsers in smartphones don’t remember passwords for you).  To do this just update the URL on your smartphone to be in this format:

https://userid:password@DynDNS_hostname/x10.php?cmd=ON&id=D16

Simple, cheap, and effective remote control of your lights & other devices from your smartphone.

One thought on “Remotely controlling lights, etc. from a smartphone

  1. mikepell

    Thank you!

    This is exactly what I was looking for, simple and elegant. I needed it to work with mochad though, so I patched it. I’ll post in case someone else comes across this and finds it useful.

    A couple of hacks:
    1. works with mochad using PHP sockets
    2. prefixing a device with “R” causes CM15A to send an RF signal to the device
    3. rudimentary CSS for display on iphone, slightly larger font for fat fingers

    —patch begin—
    52a53,54
    >
    >
    89c91,98

    > if (substr($req[‘id’], 0, 1) == “R”) {
    > $cmdid = substr($req[‘id’], 1);
    > $brcmd = “rf ” . $cmdid . ” ” . $req[‘cmd’] . “\r\n”;
    > } else {
    >
    > $brcmd = “pl ” . $req[‘id’] . ” ” . $req[‘cmd’] . “\r\n”;
    > }
    98c107,114
    $service_port = 1099;
    > $address = ‘192.168.0.1’;
    >
    > $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    > $result = socket_connect($socket, $address, $service_port);
    >
    > socket_write($socket, $brcmd, strlen($brcmd));
    > socket_close($socket);
    119c135
    < print "\n”;

    > print “\n”;
    131c147
    < print "\n”;

    > print “\n”;
    —patch end—

    —iphone.css begin—
    body {
    background-color: #fff;
    }

    .header, .footer {
    width: 100%;
    }

    .sidebar {
    display: none;
    }

    table.theTable {
    font-size : 13pt;
    }
    —iphone.css end—

Leave a Reply