Being the type of person who likes to automate everything (if it can be scripted it should never be done by hand), I wanted to create a system for automatically turning on my apartment lights in the morning.
There are lots of ways one could do this, likely the easiest would be a lamp timer, but that’s a little too mundane. So I created a solution using a Raspberry Pi, and X10:
Equipment
- A linux based computer; I used a Raspberry Pi since it’s low powered.
- A serial port; I used a USB to serial adapter, they are fairly easy to find online.
- The FireCracker CM17A serial X10 transmitter, an X10 Transceiver, and as many X10 Lamp Modules as you’d like. I got them as a kit with a remote control.
- An Android phone with the Tasker app.
Setup
First download and install the recommended Raspberry Pi operating system. When setting it up, I’d recommend setting the SSH server to start automatically, and setting a static IP (or add a rule to your DHCP server to always give it the same IP). Once it is setup, we need to install BottleRocket, a command line application for controlling the FireCracker CM17A unit.
1 | sudo apt-get install bottlerocket |
Since we are using a USB to serial adapter to attach the CM17A, we need to specify its location to bottlerocket, normally “/dev/ttyUSB0″. Plug in the X10 Transceiver and test if it works by running the following command to turn on all the lights:
1 | br --port=/dev/ttyUSB0 --ON |
Now we need a way for the phone to talk to the Raspberry Pi, there is a huge number of ways to do this, I did it with a simple web server. Specifically I used apache2 and php5.
1 | sudo apt-get install apache2 php5 |
Once the web server is up, create a short php script to control bottlerocket using some GET variables. (Ideally you could create a proper RESTful interface, but I was lazy.) Call this file “var/www/br/index.php”:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?php $number = $_GET['num']; $house = $_GET['house']; $action = $_GET['action']; if ($number < 1 || $number > 16) { echo "error in number"; exit(); } if (preg_match('/^[a-p]{1}$/',$house) != 1) { echo "error in house"; exit(); } if ( $action != 'on' && $action != 'off' ) { echo "error in action"; exit(); } echo shell_exec("sudo /usr/bin/br --port=/dev/ttyUSB0 $house$number $action"); ?> |
Also we need to allow the webserver user, in this case “www-data” to run br using sudo to it can get access to the USB controller. To do this run visudo and add the line:
1 | www-data ALL=(ALL) NOPASSWD:/usr/bin/br |
This isn’t the best practice from a security point of view, but for a simple server running on a local network, it should be fine.
Now you can test the script by turning off your first lamp by visiting the following webpage in your browser:
1 | http://[raspberrypi-ip]/br/index.php?house=a&num=1&action=off |
Where [raspberrypi-ip] is the IP Address of your Raspberry Pi on your local network. You can turn the light back on using:
1 | http://[raspberrypi-ip]/br/index.php?house=a&num=1&action=on |
Now on your Android phone get Tasker to visit the webpage when the phone’s alarm starts; I’m assuming if you made it this far, you can figure out how to do this
So there you have it: a way to turn on lights (or even appliances) in your house whenever your phone alarm goes off. The best part is, once this is set up you don’t have to touch it again. If you change the time you wake up, all you have to do is change your alarm time, and it will just work.











