I just recently got an amazon echo and always wanted to expand its home automation possibilities but wasn't happy with the price tag of many of the ready-made IOT solutions and figured I could probably make something with reasonable functionality for much less money.
I already had a bunch of raspberry pi's laying around so I figured that was a good place to start. The pi is definitely the most expensive part of this project but it was free to me and you could probably get away with the $10 Pi Zero Wireless instead.
The idea is to make the Raspberry Pi available to Alexa (from the Echo) and use it to control a relay. This relay opens the possibility for a lot of things but I wanted to use it to control a lamp in my living room.
Step 1: Materials
You'll need:
- Raspberry Pi
- 5V Relay module
- jumper wires
- Amazon Echo
- Male mains plug with wire
- Female mains plug with wire
Step 2: Setting Up the Raspberry Pi
There are various ways of doing this. If you have an HDMI monitor and usb keyboard handy I think it's a bit easier. Otherwise you can do a "headless" setup.
Here's a good tutorial on doing a headless setup on Windows:
http://www.circuitbasics.com/raspberry-pi-basics-s...
And one for Mac:
https://www.instructables.com/id/My-Raspberry-Pi-S...
But the easiest is to load up an SD card with NOOBS, boot the pi up while connected to a monitor and keyboard and just go through the configuration. This tutorial explain it pretty well:
http://lifehacker.com/the-always-up-to-date-guide-...
If you didn't do a headless setup you should still get SSH ready, it's necessary for the rest of this. To do this I use Putty. Get it here:
And if you don't know your Pi's ip address you can use advanced IP scanner:
http://www.advanced-ip-scanner.com/index4.php?utm_...
Then just enter the IP address for the pi in input for Host Name/IP address, leave the port at 22 and click open. You'll be prompted for the login.
Step 3: Installing and Dependencies
A big part of getting this to work for me was getting the dependencies to play nice. Amazon has provided a python library called "flask-ask" to accept incoming calls from Alexa. For more information on flask-ask I encourage you to visit the the john wheeler's (the creator) website: https://alexatutorial.com/flask-ask/ Once it's working this functions really nicely but it was a bit of a pain to get it there.
My first note on this is that I ended up having to use python 3 to get any of this to work.
So here is the list of dependencies I had to sort out:
- apt-get update
- apt-get install libffi-dev
- apt-get install python3-cffi
- apt-get install libssl-dev
- pip3 install python-dateutil
- pip3 install click
- pip3 install cryptography
- pip3 install flask-ask
I think that covers them but I may have missed a few. I hope to at least have saved you a bunch of googling for the right packages.
Step 4: Ngrok
Ngrok is a utility that opens a tunnel from your raspberry pi to an HTTPS endpoint so that Alexa can find it. You'll need to have this running on your pi any time you want your service to be available. You download the zip here:
https://ngrok.com/download (get the stable ARM linux release)
and use SCP to copy this onto you Pi. I like using winSCP for this. You can get that here:
https://winscp.net/eng/download.php
Once it's in your Pi, unzip it:
unzip ngrok-stable-linux-arm.zip
Then run it:
sudo ./ngrok http 5000
It should look like it does in the picture.
Step 5: Python Script
As well as ngrok, your script will need to be running any time you want the service available. Go ahead and open another instance of putty to open another SSH terminal.
You can use the script I attached
It imports all the modules (basically flask and GPIO). Then it sets up the app. Then it sets up an endpoint for the intent named "TurnLights" and maps the "slot" (like a variable) "status" to "status". It then defines a function "turn" to act as the handler. In here we set pin 3 as an output, and if the voice command had status as "on" we turn that pin high, if it was "off" we set the pin low. Then we return some information about what was done.
Step 6: Alexa Skill
Next step is to make the Alexa skill. Go to the amazon developer portal. Make an account if you don't have one
https://developer.amazon.com/edw/home.html#/skills
add a new skill. Name it something recognizable, I went with "RPi control". Then decide on an invocation name, I went with a first name "jeoffrey"
The intent is a little tricky to get right. The syntax is pretty specific and the sample utterances are pretty important in determining what Alexa will actually understand. You can use mine or use it as a jumping off point.
In configuration, select https and enter the url for your ngrok
in ssl certificate select "My development endpoint is a sub-domain of a domain that has a wildcard certificate from a certificate authority"
now once it's done building (can take a couple minutes) you can go to the test page and type in something similar to what we used for the sample utterances ("TurnLights to On") and if everything is set up right you you should see it register on the pi and get the right response.
Step 7: Relay
The last step is to wire up the relay. The relay will open the circuit to allow electricity to flow to your light.
The relay has two sides, the control side and the switching side. On the control side you'll find 3 terminals:
- DC+
- DC-
- IN
The control side is all wired up to the Pi. Reference the pi GPIO pinout to match up the connections. DC+ should go to 5V, DC- to GND and IN to the GPIO being used in this case, pin 3.
On the switching side there's another three terminals:
- NO
- COM
- NC
These are "Normally Open" meaning no current flows to this terminal until the relay is triggered. "Normally Closed" meaning current flows to this terminal until the relay is triggered. And "COM" which is the input. Usually the relay would be triggered on a high signal to the IN pin. This particular relay has a H/L jumper meaning you can pick whether to trigger on High or Low, I left it on High trigger.
We'll only be using two of these terminals. You'll want to connect one wire from the ac power line to the COM terminal. Then wire one wire from the extension chord into the NO terminal. Then connect the remaining wire from the extension chord to the remaining wire on the power plug, I used a wire nut for this.
Step 8: Try It Out!
Time to try it!
Comments