Alexa and Remote Controlled Main Plugs
by Ajaxjones in Workshop > Organizing
3142 Views, 35 Favorites, 0 Comments
Alexa and Remote Controlled Main Plugs
I've been using Alexa for a while now, but really wanted to automate some of the devices in the workshop. Things like the power supply for the LiPo charger, the heater and various lights and lamps. However a lot of the units out there that do this are fairly expensive and I didnt particulary fancy hacking mains powered relays for this. I just wanted a cheap and easy solution.
Luckily there has been some great work done with the Raspberry pi and what really tipped the project was the software that is now around that can emulate the simple RF mains switches that you can get.
RF Mains Switches
The first thing I started with was some of these RF controlled switches.
You can get these from Maplin for about £9.99 for one with the remote, but they get a lot cheaper in multi-packs. A 5 pack only costs £34.99 and you can get singles without the remote for £6.99 each
In essence they work and you could just use these but they dont respond to voice and each only controls 4 at a time unless you change the channel switch to use another bank of 4.
5 pack https://www.maplin.co.uk/p/remote-controlled-mains...
single https://www.maplin.co.uk/p/additional-remote-contr...
Raspberry Pi
OK, so having settled on some mains plugs, the next step is to get the raspbery pi zero respond to Alexa. To do this I have used the fauxmo software which you can get from here. https://github.com/n8henrie/fauxmo
After downloading it, you can then instruct Alexa to "discover devices" and it will pick up the software. I added to the demo program so that the single instance of the software would present multiple devices to Alexa to control.
Here is how I modded it to present multiple devices. All the options are in a simple if then else statement and execute local scripts. So you can easily replace these with your own scripts if you want the pi to do other things that you might have set up.
If you get this to work, you will be able to issue commands like "Alexa, turn the heater on" and it will respond with OK.
""" fauxmo_minimal.py - Fabricate.IO
This is a demo python file showing what can be done with the debounce_handler. The handler prints True when you say "Alexa, device on" and False when you say "Alexa, device off".
If you have two or more Echos, it only handles the one that hears you more clearly. You can have an Echo per room and not worry about your handlers triggering for those other rooms.
The IP of the triggering Echo is also passed into the act() function, so you can do different things based on which Echo triggered the handler.
"""
import fauxmo
import time
import os
from debounce_handler import debounce_handler
class device_handler(debounce_handler):
"""Publishes the on/off state requested, and the IP address of the Echo making the request.
"""
TRIGGERS =
{"light":52001,"heater":52002,"charger":52003,"shed":52004,"workshop":52005}
def act(self, client_address, state, name):
print "State", state, "on ", name, "from client @", client_address
if name == "heater":
print ("heater");
if (state):
print ("Turning on the heater");
os.system ("/home/pi/raspberry-strogonanoff/src/strogonanoff_sender.py --channel 1 --button 3 --gpio 15 on")
else:
os.system ("/home/pi/raspberry-strogonanoff/src/strogonanoff_sender.py --channel 1 --button 3 --gpio 15 off")
elif name == "light":
print ("light");
if (state):
print ("Turning on the house light");
os.system ("/home/pi/raspberry-strogonanoff/src/strogonanoff_sender.py --channel 1 --button 2 --gpio 15 on")
else:
os.system ("/home/pi/raspberry-strogonanoff/src/strogonanoff_sender.py --channel 1 --button 2 --gpio 15 off")
elif name == "charger":
print ("charger");
if (state):
print ("Turning on the charger PSU");
os.system ("/home/pi/raspberry-strogonanoff/src/strogonanoff_sender.py --channel 1 --button 1 --gpio 15 on")
else:
os.system ("/home/pi/raspberry-strogonanoff/src/strogonanoff_sender.py --channel 1 --button 1 --gpio 15 off")
elif name == "workshop":
print ("workshop");
if (state):
print ("Turning off the workshop");
os.system ("/home/pi/raspberry-strogonanoff/src/strogonanoff_sender.py --channel 1 --button 1 --gpio 15 off")
os.system ("/home/pi/raspberry-strogonanoff/src/strogonanoff_sender.py --channel 1 --button 2 --gpio 15 off")
os.system ("/home/pi/raspberry-strogonanoff/src/strogonanoff_sender.py --channel 1 --button 3 --gpio 15 off")
else:
os.system ("/home/pi/raspberry-strogonanoff/src/strogonanoff_sender.py --channel 1 --button 1 --gpio 15 off")
os.system ("/home/pi/raspberry-strogonanoff/src/strogonanoff_sender.py --channel 1 --button 2 --gpio 15 off")
os.system ("/home/pi/raspberry-strogonanoff/src/strogonanoff_sender.py --channel 1 --button 3 --gpio 15 off")
elif name == "shed":
print ("shed");
if (state):
print ("Turning off the shed");
os.system ("/home/pi/raspberry-strogonanoff/src/strogonanoff_sender.py --channel 1 --button 1 --gpio 15 off")
os.system ("/home/pi/raspberry-strogonanoff/src/strogonanoff_sender.py --channel 1 --button 2 --gpio 15 off")
os.system ("/home/pi/raspberry-strogonanoff/src/strogonanoff_sender.py --channel 1 --button 3 --gpio 15 off")
else:
os.system ("/home/pi/raspberry-strogonanoff/src/strogonanoff_sender.py --channel 1 --button 1 --gpio 15 off")
os.system ("/home/pi/raspberry-strogonanoff/src/strogonanoff_sender.py --channel 1 --button 2 --gpio 15 off")
os.system ("/home/pi/raspberry-strogonanoff/src/strogonanoff_sender.py --channel 1 --button 3 --gpio 15 off")
return True
if __name__ == "__main__":
# Startup the fauxmo server
#fauxmo.DEBUG = False
p = fauxmo.poller()
u = fauxmo.upnp_broadcast_responder()
u.init_socket()
p.add(u)
# Register the device callback as a fauxmo handler
d = device_handler()
for trig, port in d.TRIGGERS.items():
fauxmo.fauxmo(trig, u, p, None, port, d)
# Loop and poll for incoming Echo requests
##logging.debug("Entering fauxmo polling loop")
while True:
try:
# Allow time for a ctrl-c to stop the process
p.poll(100)
time.sleep(0.1)
except Exception, e:
##logging.critical("Critical exception: " + str(e))
break
Emulating the RF Signal
The next step of course is to be able to control the RF plugs without the remote. To the rescue comes raspberry strogonanoff which you can download here
https://github.com/dmcg/raspberry-strogonanoff
this is based on the reverse engineering from fanjita.org and I in fact used their arduino sketch to begin with to check the TX module. You can follow this at http://www.fanjita.org/serendipity/archives/51-Int...
To tx on RF , i have used a cheap TX module @ 434Mhz from cool components which only costs about £3.54 - https://coolcomponents.co.uk/products/rf-link-tran...
Rather than use the basic connection, I change the sw around to use pin 8 on the pi so that I could whip up a simple breadboard. it simply switches the Gnd and GPIO pin around so its easy to plug into the raspberry header
Once you do this, you can use the example strogonanoff_sender.py prog and just call it like this instead
/home/pi/raspberry-strogonanoff/src/strogonanoff_sender.py --channel 1 --button 3 --gpio 15 off
It's using wiring so that pin 15 isnt actually GPIO15 , its GPIO14 which is in turn pin 8 of the header. You shouldnt have too much trouble working out your pins. Once its done that command line will then turn the relay on and off.
I put this on an offcut of veroboard, and a header and just plugged it into the Pi
Automatically Loading the Software on Boot
This was slightly problematic as at first it seemed that fauxmo didnt like to run in the background if you logged out, nor did it seem to load up using the normal methods.
So this worked, there probably is an explantion for why the normal methods didnt but it works so I'll leave it here.
in sudo crontab -e I run this every minute
* * * * * /home/pi/echo.sh
then the sh is
#!/bin/bash
if [ $(ps aux | grep echo.py | grep home | wc -l) -eq 0 ] then python /home/pi/echo/echo-master/echo.py & fi
all this does is see if the software is running by grepping the output and if it isnt, then it runs my master fauxmo script. Pretty dirty method of doing it,but it works and keeps on running should fauxmo crash
Making a Case
Of course it has to go into a nice case and I have previously made a slide in raspberry pi zero case that does the job nicely here. You can get the various STL for this or one with an OLED option from https://www.thingiverse.com/thing:2491790
I had an old black one lying around, but the Alexa Echo is white in the workshop, so this was just sprayed white to fit.
In Operation
So now simple voice commands can turn mains powered devices on and off. An added bonus is that ANY alexa device in your home can control these. So I can now turn on the heater remotely from the house for example before I go down to the workshop.
I've also added a master command to the script so that "ALEXA , turn the workshop off" cycles through all the devices and turns them all off at once. A great way to shut down the shop.
I hope you like it and it is useful and you get to use it as a simple cheap way of using Alexa to control you own devices.