I put some lights on timers whenever we travel. A few lights in the house are on permanent timers (I had bought Aube TI035/U timer switches for the outdoor lights – they work nicely), but most of the indoor lights only need to be on timers when we’re away. You can buy fancy digital temporary light timers, but we have the old mechanical types. I really don’t like these things as they are pretty inaccurate and if there’s a lengthy power failure, the lights will turn on and off at the wrong times once the power comes back on.
So instead, I thought it would be interesting to have the Raspberry Pi control some lights using RF controlled light switches. Home Depot has these. They seem to be especially popular around Christmas time as they can be used to control decorative lights. The package contains a remote fob and three outlet boxes. The outlets boxes are numbered #1, #2 and #3 (though the first package I got had two #2’s and a #3 – I exchanged it) and the fob has three sets of push buttons: an on button and an off button for #1, #2 and #3. This seems perfect – the remote has separate on/off switches (not toggling on/off) and each outlet can be controlled individually. So with one package you can control three outlets (you plug the outlet box into the wall and then plug your lamp or whatever into the box). It also looks like you can control even more as each package is marked with a sticker saying “set A” or “B”, up to “E”. The outlet boxes have the same sticker. So if you, say, buy an “A” package and a “B” package, you can have six individually controlled outlets.
The remote works at 315 MHz. So the task ahead is to somehow “sniff” each code that is transmitted from the remote fob to the box and set up a transmitter so that the Pi can transmit the same thing. If the Pi can send the same code, then the outlet should react as if the corresponding button were pushed on the remote fob.
Sniffing the codes
The basic idea is that you have to buy a 315 MHz transmitter/receiver set. You hook up the receiver to an audio plug that you plug into the line-input jack of your computer (a PC or a Mac – not your Pi). Using an audio capture program like Audacity, you record the signal coming out of the receiver when you push a button on the remote fob. This will give you the code for that button. You then use the transmitter and play that code back and voila, you’ve got control of your lights from your Pi.
What you need to buy
Here’s what I bought to make this work, all from Sparkfun.
- WRL-10535 315 MHz RF Link Transmitter (don’t need this yet)
- WRL-10533 315 MHz RF Link Receiver
- PRT-08032 3.5mm Audio Jack
- CAB-08566 Male to male 3.5mm Audio Cable
At the time of this writing, the total cost for the above is about $12.
I already had a little half length breadboard, a box of various resistors, and wire.
Wiring it up
R1 is a 500K ohm resistor between the receiver output and the audio cable line (the MagPi article suggested 1M ohm). Although the breadboard picture shows the receiver as a chip, it is in fact a very slender PC board with some pins soldered to the bottom. You simply plug it into your breadboard. The pins are all in one row.
Below are some photos of the set up. The first is of the whole thing connected to my Macbook. Power comes from an Energizer 4Ah portable charger (e.g. emergency charger for your cell phone) that has a USB port. I got a Sparkfun Hydra USB power cable (a very clever cable with USB on one end and various plugs on the other for power – including the alligator clips used here. Just make sure they never touch and short out the power source!). The bottom picture is a closeup of the circuit. Note that the receiver has a few more connections to +5V and ground not shown on the schematic. Fortunately, they are labeled on the little receiver board. Note that there’s no external antenna needed.
Trying it out
So now it is time to capture some codes! Power the circuit, plug the audio cable into the line-in jack, and launch Audacity. Make sure Audacity is set to record from line-in. I set the “Project Rate” to 48 KHz (not the default). I pressed a button on the transmitter fob and saw the following:
If you actually play the sound on Audacity, you here a lot of static, then tones (!), then more static. Very cool! Something is working! To see the actual signal, zoom in between 0.80s and 1.20s.
The start of the code is the long pulse high at the beginning. Note that it ends with the very long low signal.
You definitely see some repetition in the code. Zooming in on one of the “blocks”,
you see a pattern of highs and lows. Stare at this long enough and you can determine the code for the various buttons on the transmitter fob.
The next blog post will show how to build and code up the transmitter.