Sunday, December 18, 2016

Home automation - Etekcity outlets meet Raspberry Pi meets Amazon Echo

I came across Sam Kear's beautiful article on controlling power outlets wirelessly and couldn't wonder about what it would take to take the next step of enabling this interaction in Amazon Echo.

It seems the universe is one boundless crucible of creativity and ideas - Chris Derossi at makermusings has already done the hard work of figuring it out.

The idea is simple:

  • Echo automatically detects Wemo devices
  • Use wireshark to reverse engineer the conversation taking place once you trigger "Discover Devices" 
  • Use fauxmo to mimic a Belkin hub and direct the request to your RESTful service that will do the needful
  • Implement your on/off behaviour via the RESTful service, internally making a system call via codesend (described in Sam Kear's posting).




This is how I went about it.

The ingredients in the recipe:
  1. Etekcity Wireless Remote Control Electrical Outlet Switch for Household Appliances: This device came on sale recently with a coupon, dropping the price to $21.48.
  2. A Raspberry Pi: I set out building it with the Model B and I hit one of the troubles he mentions in Troubleshooting,  an observation that the receiver was not super sensitive when you press the buttons on the Etekcity remote. My feeling was that the Model B is a bit underpowered for this, the frustration made me use a Model 3 (yes, Frys have it now) which was remarkably sensitive to the clicks on the receiver. Using the Model 3 also obviates the need for a Wifi dongle.
  3. Receiver transmitter
  4. A micro SD card, at least 16 GB in capacity(Elementary tip: if you want to reuse the cards on which you've written the images, you'd see a drive with reduced capacity. See the fix here.)
  5. Power adapter for the Pi (one lying around from a retired iPad came handy)
  6. GPIO Breakout and Cable for Raspberry Pi
  7. Breadboard, jumper wires, soldering iron,solder etc
  8. Wire for antenna - I used an old coat hanger connected to a wire that was soldered on to the transmitter. Works at least over 30 ft and across at least 1 floor.

Steps:
  1. Set up your Pi using Sam's posting. I don't think I can improve on something that is so well done.
  2. Find and note the on/off codes using RFSniffer.
  3. Set up fauxmo. I used the steps detailed here. 
  4. At this point, Echo should be able to find the dummy devices setup in config.json (use the Smart Home->Discover Devices link in the Alexa app). Try saying "Alexa, turn on fake switch one" and Alexa will start blinking with the blue light moving round and round, making every effort to fulfill your request, but will fail with a fairly verbose message.
  5. Install/configure flask.
    1. Create the virtual env (install virtualenv with "sudo pip install virtualenv" if you do not already have it).

      • pi@raspberrypi:~/projects/flaskserver $ virtualenv flaskvenv
        New python executable in /home/pi/projects/flaskserver/flaskvenv/bin/python
        Installing setuptools, pip, wheel...done.
        pi@raspberrypi:~/projects/flaskserver $
    2. Activate the virtual env and then install flask.
      • (flaskvenv) pi@raspberrypi:~/projects/flaskserver $ pip install Flask
    3. (This step is a placeholder for a future refinement and is not necessary). Create a directory called template and drop the index.html file here.
    4. Create your flask app . Here's mine.
    5. Create a shell script that initializes and starts flask. This will come in handy later once we start using systemd to automate flask initialization on startup.
  6. Edit the config.json file for fauxmo. Set up the codes you recorded in Step 2. Use a different port number for each device. Modify for ip address and port numbers per your setup. I've followed a format of http://ipaddress:port/<device group>/<device>/<code>. I have no idea at this point on what I might do with the device group and device values, but something tells me that some sort of grouping might be useful once I drift into tools like Home Assistant etc.
  7. Restart fauxmo. Rediscover the devices. You should see your devices from the changed config.json file now.
  8. Test with "Alexa, turn on <your device name>" and "Alexa, turn off <your device name>"
  9. If everything works all right, setup fauxmo and flask for initialization during startup. Create the service file (my samples here) and enable them with systemd (good tutorial here ):
    • sudo systemctl enable fauxmo@pi.service

Happy automating!