Note: I had a lot of fun building this project, and there's still a lot of good stuff in this post, but if you really want to try doing your own Sonos/Alexa integration, you're much better off going with the project by Ryan Garciano, here. It has been maintained to keep pace with the changes that Amazon has brought to the Alexa ecosystem.

Original

This is my second Sonos/Echo mashup, but the first of any real utility. The first hack was fun, but I don't really think it's at all practical. Here, I'll show you how you can write an Echo app to control your Sonos with voice commands.

Before we get started, you'll need to actually have an Echo, which isn't generally available yet, and you'll have to have an Echo developer account, which is invite only. Even without these things, it's still pretty interesting to follow along, as this kind of functionality is clearly where Amazon is heading with their Echo.

The Shiny

Let's get the show and tell out of the way first. Here's the Echo, in action, controlling my Sonos.

Architecture

An Echo app consists of at least two separate components: the configuration completed on the Amazon developers site, and a public-facing endpoint that can receive callouts from Amazon with information about the command issued by the user.

The configuration on the server was in some ways the hard part: you've got to tell Echo what to listen for, and the format to expect it in.  I agreed to an NDA when I signed up, and though I've heard that the NDA has expired, I don't think I've gotten word of that expiration. I don't have the means to fend off Amazon in a lawsuit, so I'll leave out any further specifics about this part.

The code for my endpoint, running on Heroku, receives the information from Amazon, interprets it, and uses node-sonos-http-api running on Raspberry Pi on my local network to control the Sonos. (I had to open a port on my router to expose the Pi to the internet.)

The code running on Heroku is pretty straightforward: a simple Express-based app to receive the Amazon callout, interpret it (using alexa-nodekit), construct a GET url, and send it to my home Pi.

Here's a section from the heart of the code. We note that we've received an "IntentRequest", which is the kind of "action" callout from Amazon. We then go on to note that we received specific Intent of type "PlayPlaylist", which is a command I defined in the echo developer portal. We grab the specified playlist, construct my url, and send it off to my home machine.

Wrap Up

So the first thing you're thinking is that this'll never scale, and you're right. If this is going to be a real Echo app, we'll need a much more robust app running on Heroku (or wherever), which integrates with a much more robust app running on a user's home network. Ideally, the home app would hit a Heroku app endpoint, register itself, and allow the user to log in with her Amazon account to link her Echo to the system. That's all a long ways down the road, though.

There are some quirks with developing for the Echo that I'm not comfortable talking about publicly yet, due to the whole NDA thing. It's super cool, but I'm definitely learning on the fly.

So grab the git repo, (and the node-sonos-http-api one), sign up for an Echo dev account, and start talking to your Sonos.