Alexa does Dolly, Part 3: Connecting your RaspBerry Pi to Amazon
My quest to play the greatest hits of Dolly Parton by voice command continues. Last time we looked at setting up node.js on a Raspberry Pi so that we could use it to control Sonos devices via a browser. In this post, I’ll show you how to connect the Pi to Amazon’s web services so that we can employ Alexa as our voice-activated DJ.
Time to invest in an Echo
Now that we have a functioning Raspberry Pi, and a way to control our Sonos devices using a browser, we can start adding in the fun bit: our Amazon Echo integration. Obviously, you’ll need an Amazon Echo to do this. As I said in my earlier posts, I’ve opted for the Echo Dot because I’m using my Sonos to play music. These are currently selling for £49.99 on Amazon, so a lot cheaper than the full size Echo, but either should work.
Reserving an IP address for your Raspberry Pi
The next thing we need to do is expose our Raspberry Pi to the outside world. How you’ll go about this depends on how your home network is configured, and who your ISP is. From me it was a pretty straightforward task: there’s a useful guide for Virgin Media users online. BT customers should take a look at this guide.
Importantly, you’ll need to find out the IP address that your Raspberry Pi is on and reserve it, so that it will always get this address if you need to reboot the device. Here’s a guide which shows Virgin customers how to do this:
I’ve left my node server on the default port of 5005 and forwarded any incoming requests to this port from the outside world to the same port on my Pi.
A quick word on security
At this point you might want to think about setting up some security on the server. rgraciano’s guide gives some information on setting up basic authentication, but you’ll want to satisfy yourself that what you put in place is sufficient for your needs. You have been warned!
Go ahead and test this using a browser that’s not on your home network. If it works then you can control your Sonos from outside the home. Hours of fun can be had with this feature alone e.g. when you’re in the pub after work and you want to freak out your spouse/the dog whilst delighting and amazing your geeky work colleagues!
Setting up an Alexa skill
Our next step is to set up an Alexa skill in Amazon. If you’re interested in Alexa skills more generally then I can recommend Matt Brook’s post on developing for the Amazon Echo. If, on the other hand, you’re desperate to start bossing your Sonos around, then return to rgraciano’s post and follow the instructions.
One key difference for folks reading this in the UK is that you need to set the language of your skill to be ‘English (U.K.)’, otherwise your echo won’t recognise that your skill is capable of responding to your commands.
Configuring an AWS Lambda service
Lastly, we need to configure an AWS Lambda service that will talk to our home server. Again, the guide is pretty comprehensive on this.
And that’s it! You should now be able to control your Sonos system using just the power of your voice.