Emonhub, Lightwave RF and Amazon Alexa - a simple example

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 … :wink:

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!

2 Likes