I had previously developed a script to control the x10 firecracker (CM17A) module using python. Of course, my new Raspberry Pi would make a perfect home monitoring server to control the lights. The script works fine on the Pi using a USB-to-serial converter, but when there’s only 2 precious USB ports, why not use the serial pins on the GPIO bus to drive the x10?

The only problem here is the Pi’s GPIO serial port is only wired with Tx and Rx pins. The firecracker isn’t truly a serial interface as it only communicates using two other pins, DTR and RTS, which the Pi doesn’t have. Since we can’t really use the built-in serial port on the Pi, we can certainly bang some bits out on the GPIO pins and connect them up to the DTR and RTS.

I chose GPIO pins 24 and 25, for no particular reason. Now, the firecracker needs at least 5V to do it’s wireless transmitting magic, but the Pi operates on 3.3V, so we need a level shifter. This guy from Adafruit works nicely, it’s only $8, and you don’t have to mess with wiring the transistors yourself. The connections then look like this:

GPIO 24 --> 3.3 shift 5V --> DTR (pin 4 on serial DB9 connector)
GPIO 25 --> 3.3 shift 5V --> RTS (pin 7 on serial DB9 connector)
GPIO GND --> GND (pin 5 on serial DB9 connector)

Here’s what my ugly prototype looked like, being hacked together with alligator clips since I couldn’t seem to find my bare DB9 connector laying around anywhere. If you do this, just make sure the stupid alligators don’t short together, and wire it up a bit more permanently later.

X10 RPi

Notice the level shifter on the breadboard in the foreground, and the Pi Cobbler breakout on the breadboard in the background. The thing that looks like a transistor is the temperature sensor I set up earlier.

I made a few adjustments to the code to support GPIO as well as normal serial bus. You’ll need the RPi.GPIO module to let python talk to the pins. If you haven’t already, install it with:

sudo apt-get install python-rpi.gpio

To use the script in gpio-mode, pass in the portname as ‘pi’. From the terminal, this would look like:

sudo ./firecracker.py A 1 ON pi

to turn on house code A, unit 1. Note it has to be run as root to give python access to GPIO. With a different portname, it can still be run using a standard serial port on any Windows/Mac/Linux system.

The updated script is here in bitbucket.