Introduction: Home Automation With Amazon Echo Voice Control
Last month, Amazon released the Echo - a small cylinder capable of powerful voice recognition.
The Echo itself is closed source. But once you figure out the Echo's secrets, it becomes an incredibly powerful tool for voice control, enabling you to control anything in your home or apartment via voice without having to worry about the complexities of the human voice. (see: how to do voice control pre-Echo - hint: it's MUCH harder)
In this Instructable, I'll cover how to use the Amazon Echo to control any device via wifi or MQTT.
All you need is:
- An Amazon Echo that's set up with Amazon's instructions
- A Raspberry Pi or other computer running Linux
- The starter code
Difficulty: You'll need to know a little bit of Python programming to understand the examples.
Time required: 10 - 30 minutes from unboxing to Alexa.
Note: This Instructable is currently Linux-only. Let us know if you'd like it on other platforms - if there's enough interest, we'll look into it!
Step 1: Set Up Your Server
With your Echo up and running, let's talk hacking and home automation.
Specifically, we're about to create a server that pretends to be a WeMo device.
The Echo integrates with the WeMo home automation protocol - simply say "Alexa, discover my devices" and it'll search the local network for WeMo devices. So let's give it a device to discover!
sudo apt-get install python-pip ; sudo pip install requests
3. Extract the zip file
unzip master.zip -d echo
4. Move to the newly created echo folder
5. Start the server - you should see some debugging text indicating the server has started polling.
6. Say to your Echo "Alexa: discover my devices". She'll take a few seconds to find the new device. If she doesn't find it the first time, try killing and restarting the process and discovering devices again.
7. Now try it out! Say, "Alexa: turn off device", or "Alexa, device on". You'll see True or False for on/off, along with the Echo's IP address.
The Echo can also send commands via the Amazon cloud, using their new service called Lambda. This is a convenient way to get started with the Echo, but not great for home automation. It requires that every command include the name of the program, ie "Alexa, ask HOME to turn on the lights", and it also involves a network round trip to distant servers, which slows down the response time.
What we really want is to control local devices locally, without the prefix - ie "Alexa, turn on the lights" - which is why I'm using the WeMo protocol.
Step 2: Control EVERYTHING!
That's all there is to it. You now have a server connected to your Amazon Echo. You can now use your voice to:
- Control the lights, air conditioner, even your TV!
- Run scripts on your computer
- Toggle entire home automation modes ("movie mode", "party mode", etc)
- Prank your roommates
- Save power by making it easier to turn off devices you aren't using
We've set up our apartment to run an Echo in each room, controlling all of our home automation devices via the maker-friendly MQTT protocol.
Read on to learn about MQTT and how to use multiple Echos.
Step 3: (bonus) Using MQTT to Control Devices
MQTT makes controlling home automation devices a breeze using a "publisher-subscriber" or "pub-sub" framework. With pub-sub, you publish messages to topics, and any devices listening on those topics receive those messages. So, for example, you could have a topic bedroom-lights and publish "1" or "0" to turn the lights on or off.
Here's how to use the code I wrote to connect the Amazon Echo to MQTT:
- Use pip to install MQTT by opening up a command window and typing:
pip install paho-mqtt
- You can now run the test MQTT file with
- Open up a web browser and browse to the HiveMQ MQTT Websocket Client
- Click "Connect".
- Click "Add New Topic Subscription". Type "livingroom" in place of "testtopic/#" and hit Subscribe.
- Say "Alexa, turn on the lights"
- You should see a "True" message posted to the topic!
I'll leave it as an exercise to discover the other topics the example code uses.
Disclaimer: The example code uses a public MQTT server to get you up and running as fast as possible, so don't go publishing your Social Security Number to a test topic!
Step 4: (bonus) Using Multiple Echos & Dealing With Echoes
If you've fallen in love with your newfound Echo powers, you might be interested in using more than one Echo. You'll run into two issues doing so - and here are the solutions for both:
Knowing where the Echo is. You don't want the bedroom Echo to turn on the kitchen lights, but these things don't exactly have GPS built in. Instead, you can use the IP address of each Echo to offer it a different set of commands and controls.
Check out fauxmo_mqtt_example.py for one solution to this. I recorded the IP address seen when I ran fauxmo_minimal.py and kept it as a constant. Then whenever I want different echos to have different responses to the same command, I compare the IP against client_address in the handler's act() function.
Echoes from other Echos. The microphones on the Echo are so sensitive that they'll often pick you up from a room away - even with music playing! While incredibly convenient, it also becomes a problem when you have multiple Echos around the house.
To solve this, I added a "debounce" function to the code (see debounce_handler.py) that prevents the same command from being called multiple times in quick succession. So, if the bedroom AND kitchen Echos hear you, only the first one to submit the command will execute. Whichever Echo hears you loudest & is closest to you processes the message faster, submits the network request faster, and has its request processed - while all the Echos that hear quieter echoes take longer to process and get debounced. Voila! The debounce function is built in, so there's actually nothing extra you need to do here - it works out of the box.
Step 5: That's All, Folks!
You now have the skills to control any device in your home with your voice using the Amazon Echo. Go make incredible things!
If you like this Instructable, don't forget to favorite it and follow us on Facebook
Inspired by @MakerMusings' fauxmo control framework.