Introduction: Control Any Remote From a Raspberry Pi (and Amazon Echo!)
My wife's weather station had been crashing occasionally, and we are heading out on summer vacation soon, so she asked me for a way to power-cycle the weather station controller remotely. I already own some smart switch sockets, but they're controlled by a remote handset, not by a computer.
I researched hacks of these power outlets and did find some rather complex solutions that involved replicating the 433MHz radio protocol commands, but to put something together quickly I came up with a really simple hack: I simply wired a relay so that it straddled the push-button switch on the remote, and I simulated the button press by closing the circuit with the relay. The relay is connected to a Raspberry Pi which is a simple to use network-connected device that can drive the I/O pins needed to turn the relay on and off.
This seems almost too simple, but it really works! This technique ought to be easy to do with almost any handheld remote.
I know that I could avoid using a relay by driving one side of the button high or low with an appropriate voltage and a pull up or pull down resistor - but working out all the correct parameters and wiring is complicated, whereas adding a relay is easy.
For my wife's purposes, turning the power socket off and on via a linux shell command is quite sufficient, but it's also very easy to connect the Raspberry Pi to an Amazon Echo and control it by voice from within the house - FabricateIO's Instructable has all the details. It took just a few minutes to install the software and make a small mod to have it issue the shell commands to turn the socket off and on.
I've used an 8-channel relay module as I had one handy, but a two-channel relay module would be good enough. With the 8-channel board, I can actually use four of the five buttons on the remote and control four separate devices. This turns out to be a pretty cost-effective method of doing home automation with the Amazon Echo,
Step 1: What You'll Need...
Etekcity Wireless Remote Control Electrical Outlet Switch for Household Appliances, White (Learning Code, 5Rx-2Tx) - kit contains 5 outlets and two remote controls. (Usually $30. Sometimes discounted to $21.50)
jumper wires ($1 - $2)
Raspberry Pi connected to your home network ($35 - $45)
Step 2: Prepare the Remote
What you're doing here is simple and can be done on most remotes. Just locate the switch that is pressed and connect a wire to either side of the switch. I'm only going to do one device here, so I'm using 4 wires - two for the 'on' switch and two for the 'off' switch. If you want to control more devices, just wire up more switches.
A little reminder if you don't do a lot of soldering: put some solder on the ends of your wires before you try to attach the wires to the remote. (It's called "tinning" the wires.) When there's solder on both parts, they attach to each other much more easily.
If you'll look at this remote, you'll notice it's powered by a small 12V battery. It's not really obvious what voltage is used across the switches. An alternative to using a relay would have been to connect one side to Vcc or the other side to Gnd and to use a pull up or a pull down appropriately and of course you'll need an appropriate value of resistor for whichever connection is used. That is all doable, but it's complicated and every remote will be different - but by using a relay to physically emulate a button press, we know that whatever the logic is that is needed to activate the remote, it'll be correct!
Step 3: Connect the Relay
(Before you proceed, make sure you don't have any devices attached to your test outlet - you'll be switching it off and on a lot while you're testing)
For this project I just needed to control one device, however I happened to have an 8-channel relay module already, so I used that, giving me the option of adding three more devices later. (Each device has a separate on and off switch, so two relays are needed per device)
If I wanted to control all 5 devices that are available on the remote, I could add another 2-channel module, or I could have used just a 2-channel module (if I had one) to control one device.
The relay has a strip of pins with GND at one end and VCC at the other, and IN0, IN1, etc in between them. Connect GND to 0V on the the GPIO and VCC to 3.3V on the GPIO. Attach IN0 and IN1 to two free GPIO pins. I used BCM16 and BCM26 on my Pi2, but you can use any two GPIO outputs that you have free.
Your 8-channel module may have a jumper and another connector with GND/VCC on it. Leave it jumpered and do not connect those. They're for systems where the other VCC and GND and not strong enough to drive the actual relays - by removing that jumper you can use one pair of VCC/GND for controlling the logic and the other pair for driving the mechanical relays. You could if you wish use 5V for that purpose but to keep things safe and simple, I recommend ignoring the relay driving power and just use 3.3V on the default pins. (If this doesn't make sense to you, just ignore this whole paragraph)
Finally, note that these relays have 3 connectors each. Connect one of the two wires to each switch to the center of the three connectors. The other wire goes to the connector which is open-circuit when the relay is powered off. You can easily check which is which using an ohm-meter, but since there's only two to chose from it's easy enough just to try each. The worst case if you get it wrong is that the remote button will appear to be pressed all the time while you're testing, in which case just disconnect it. We're wiring these relays so that when the system powers up, the relays are not activated.
Step 4: Program and Test!
Create these three simple programs in /usr/local/bin (or write your own). There's a setup command you have to execute once (most easily by putting it in /etc/rc.local) and there are separate commands for 'off' and 'on'. I've named my commands "zap5" because the devices are labelled "Zap" (although that's not what they're called in the Amazon ad) and the one I'm using is on button #5.
#!/bin/sh # ON switch: BCM 16 (phys 36) echo 16 > /sys/class/gpio/export 2> /dev/null echo out > /sys/class/gpio/gpio16/direction echo 1 > /sys/class/gpio/gpio16/value # OFF switch: BCM 26 (phys 37) echo 26 > /sys/class/gpio/export 2> /dev/null echo out > /sys/class/gpio/gpio26/direction echo 1 > /sys/class/gpio/gpio26/value
#!/bin/sh # press the on button for a second and then release # 0 is circuit-made 1 is circuit broken echo out > /sys/class/gpio/gpio16/direction echo 0 > /sys/class/gpio/gpio16/value sleep 1 echo 1 > /sys/class/gpio/gpio16/value
#!/bin/sh # press the off button for a second and then release # 0 is circuit-made 1 is circuit broken echo out > /sys/class/gpio/gpio26/direction echo 0 > /sys/class/gpio/gpio26/value sleep 1 echo 1 > /sys/class/gpio/gpio26/value
That is all you need to control the relay from the command line. However for a tiny little effort more, you can control it from an Amazon Echo as well!
Follow the instructions in FabricateIO's Instructable to configure your Raspberry Pi to impersonate a Wemo, and modify example-minimal.py as follows:
Near the top, add:
from subprocess import call
and then modify procedure 'act' to look like this:
def act(self, client_address, state): print "State", state, "from client @", client_address if state: # turn on call(["/usr/local/bin/zap5-on"]) else: # turn off call(["/usr/local/bin/zap5-off"]) return True
Run the python file and once your device has been located, issue the command "Alexa, turn on device" and "Alexa, turn off device"
It should work! (It worked for me, first time. Took literally 10 minutes to set up)
If you're away from home, connect in using ssh and issue "zap5-on" or "zap5-off" at the command line. (You may need to use 'sudo' - depends on your device permissions and what groups user 'pi' is in)
Step 5: Things to Watch Out For...
I mentioned earlier that sometimes it may be necessary to use the secondary power option on the 8-channel relay module. I actually had a problem related to that - the pi was making the light on the relay go off and on, but I never heard the click that meant that the relay had switched. However in my case, even by driving the relay from 5V power, I wasn't able to make it switch. The problem was that my Pi was being driven from the USB-out of my Frankenputer, and it didn't have enough power to run the pi, drive the relays, and operate the four other sensors I have attached to that Pi! I worked around the problem when I discovered that the two relays at each end of the strip worked OK even though the others did not, and the problem went away entirely when I moved the relays from the Frankenputer to a stand-alone Pi dedicated to this task.
Another problem I had was that even though I was successfully triggering the remote control, the power outlet was not going off and on. The fact that I could see the red light on the remote when the button was (virtually) pressed confirmed that the problem was not with the relays, but with the remote itself. It turned out to be a simple matter of what orientation the remote was held in (or in this case, taped in, on the lid of my Frankenputer). When I moved the whole rig to a stand-alone pi and placed the remote in a more sensible location, it started working beautifully. I'm fairly sure that the problem is in the 'lobes' of the remote's built-in radio antenna.
Anyway, I don't expect you'll hit these problems - this is actually a pretty simple build. I'm looking forward to hearing what uses other people can put it to.
mrlewis93 made it!