Hi everyone, not sure if this is the right category so feel free to move.
I thought I would share a simple integration that I got working, and which was pleasantly easy for a change! I picked up an Amazon Dot during the black Friday sale, and was able to get it controlling my lights via mqtt and lightwaverf, In case this is of any interest to others, here is what I did:
I had a Raspberry pi Model B running the emonSD-03May16 image, receiving data from emonTX and I had already added the OOK transmitter and was using it to control some lamps that were connected to LightwaveRF sockets. This also uses the Mosquitto MQTT daemon to listen for an MQTT topic which triggers the LighwaveRF daemon lwrfd, to send the RF packets to turn the switches on and off (link to wiki). I could therefore control the lights from an MQTT app on my Android phone, as well as from the Pi via a schedule. Nice, but no voice control âŚ
Amazon have two models, the Echo which is ÂŁ150 and the Dot which is ÂŁ50, and both have the same microphone and use the Alexa cloud service for the âintelligenceâ
After a bit of research I found a couple of options to make Amazon Alexa interact with a Pi, and picked one that makes the Pi emulate a WeMo device, as the Alexa service has native support for Wemo.
I followed the guide http://www.instructables.com/id/Hacking-the-Amazon-Echo/ to install the âfauxmoâ code, which has a few sample scripts. I tested the âexample-minimal.pyâ sample python script, and ran it in foreground, and then told Alexa to âDiscover my devicesâ, which failed. This is because the firewall on the pi was blocking the traffic and so needed updating to allow Alexa to connect to the port used by the new fauxmo service (port 52000). So I ran:
sudo ufw allow from 192.168.0.0/24 to any port 52000 proto tcp
I told Alexa to try the discovery again, and it found one device - my pi. Then all I had to do was say âAlexa device onâ and saw the debug output on the pi show the command had been received by the pi !!! This took me about 10 minutes, including the delay caused by the firewall - unbelievable how easy it was !
Then to make it useful, I needed to make the script control the lights. I decided to use a different sample script - fauxmo_minimal.py, (I canât remember why) and changed the trigger word to âLightsâ. The script already understands âonâ and âoffâ, so I just needed to link these two states to existing scripts that publish to the appropriate MQTT topic, and then lwrfd sends the ON or Off command to the switches.
This shows my changes to the âclassâ part of the script, which specifies the trigger word is âlightsâ and that it uses subprocess.call to invoke my existing scripts.
class device_handler(debounce_handler):
"""Publishes the on/off state requested,
and the IP address of the Echo making the request.
"""
TRIGGERS = {"lights": 52000}
def act(self, client_address, state):
print "State", state, "from client @", client_address
if state==True:
print "Turn lights on"
subprocess.call("/home/pi/data/sunset/lwrf_Ch1_ON")
elif state==False:
print "Turn lights off"
subprocess.call("/home/pi/data/sunset/lwrf_Ch1_OFF")
return True
Note - I needed to add âimport subprocessâ at the top of the script.
This script worked, and I could now say âAlexa lights onâ and after a few seconds they would come on. I was amazed it was so easy. It also works for my wife, which is the real test! I expect to add a new trigger to control another LightwaveRF switch for the Christmas tree lights this week, and this may require a different sample script.
(One little bug - I have tried to set the script to run automatically from cron after a reboot, but the script fails! I still need to work on this).
As usual, there are probably plenty of alternative ways of doing the same thing, but thought I would share this in case itâs of use to anyone, especially as it was so easy - and I am not an experienced python coder!