Monday, August 15, 2016

Sonos Control with the Amazon Echo - Part 2: Raspberry Pi

So, a little while ago I posted how I set up my Amazon Echo to control my Sonos speakers (the article is here).  However, there was a major drawback:  If the computer ever went to sleep, got rebooted or if my user got logged out everything would stop working.  Compound this with the fact that my PC is shared by my kids and you can see that I had a lot of service interruptions.  The obvious solution is to have a permanent computer just to run this service.  I needed something I could leave on 24x7 and that was really inexpensive.  Enter the Raspberry Pi.

The Raspberry Pi computer is cheap, low power consumption, and small.  It seemed like the perfect fit.  So I found a kit (on Amazon of course) and ordered it.  Now to find out if it's as good as it seems.

First, I had to put it together.  That was really easy - I just had to stick the heat sinks on, put the Raspberry Pi board into the case, and put in the SD card.  Then I plugged in a keyboard, mouse, and monitor and I was on my way.

I chose to install the Raspbian OS (Linux for the Raspberry Pi) and in a few minutes it was up and running.  OK - now all I have to do is install node.js, the node-sonos-http-api program and I'm all set.  So I run an internet search on my new Raspberry Pi for instructions (by the way, the computer was set to use duckduckgo.com as the default search engine instead of google or bing or whatever - it worked fine), found some directions and I was off!

NOTE: This is not the full instructions on how to install the Echo control of Sonos, just how to move it from a Windows PC to a Raspberry Pi PC.  The full install instructions are here.

Install Node.js

Based on the instructions I found, I was first directed to update the OS.  That sounded like a good
My Little Raspberry Pi PC
idea so that's what I did:
  • Open up a command line terminal and run the following commands:
    • sudo apt-get update
    • sudo apt-get upgrade
Next it had me install the correct repository to get node.js.  I'm not including the instructions because, after doing this I found that it only got me node.js version 0.10 and I needed version 4.4.x - not even close!  OK, back to the internet.  After a bit more searching I found some better instructions so I did the following:
  • Open up a command line terminal and run the following commands:
    • curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash - 
    • sudo apt-get install -y nodejs 
    • npm -version
Success!  I now have node.js (the correct version) on my Raspberry Pi PC.  

Install node-sonos-http-api

Fortunately, the Raspberry Pi comes with git installed so I didn't have to figure out how to install that in order to install the node-sonos-http-api.  Here's all I had to do:
  • Open up a command line terminal and type:
    • npm install https://github.com/jishi/node-sonos-http-api 
  • Test it by going to the web browser and using the test url:
    • http://localhost:5005/zones 
It worked!  OK - now I have the server set up.  I also copied my presets.json file from my Windows PC to my Raspberry Pi PC so that I would have all of custom presets I've been setting up over the last couple of weeks.

Allow for Remote Connections

At this point, I decided I'd rather be running the whole thing remotely because my kids wanted the keyboard, mouse, and monitor back so they could use the Windows PC.  Fair enough.  Here's how I enabled it:
  • Menu -> Preferences -> Raspberry Pi Configuration 
  • Change password to one I actually know
  • Note that the local user is pi 
  • Find the IP address - this was a challenge since I couldn't remember how to echo your IP address on linux so I looked it up in my router.
  • Go back to my Windows PC
    • Download putty
    • log in to Raspberry Pi PC using the IP address for host, pi for the user and my password for the password
Now I don't need my Raspberry Pi hooked up to anything but power.

Redirect Incoming Connections

So, in order to really test it, I need to redirect the incoming connections from my Alexa Skill from my Windows PC to my Raspberry Pi PC.  To do this I went into my router settings and simply changed my redirect from the IP address of my Windows PC to the IP address of my Raspberry Pi PC.

Oh, and I also made sure my Raspberry Pi PC had a static IP address.  It would be a real pain if it ever got assigned a different IP address.

Test It Out

Now it was time to test it out:
  • "Alexa, tell Sonos to play country"
    • It worked!
  • "Alexa, tell Sonos to pause all"
    • Success!
  • "Alexa, tell Sonos to resume all"
    • Yay!
OK - so now my new little PC is running the show.  Awesome!  However, I wasn't quite done for the day...

Run node-sonos-http-api as a Service

Running node-sonos-http-api from a putty terminal worked great until the computer went to sleep or I was timed out or logged out, etc.  OK - kind of the same problem I was trying to avoid.  So how to I make it run in the background?

After some internet searches and some experimenting, I found something that worked:
  • cd to the node-sonos-http-api home
  • Run the following command:
    • nohup npm start &
This starts it as a service (meaning it runs in the background as long as the PC is running).

I tested it by closing my putty session and logging out of my Windows PC.  Everything still worked.

So now I have a Raspberry Pi PC controlling my Sonos speakers via a custom Alexa Skill routed through an Amazon Lambda service.

Whew!  No wonder I put off doing this!

However, having Alexa controlling my Sonos speakers is REALLY cool!

Alexa, tell Sonos to play celebration music!

1 comment:

Olivia Princess said...

I read this article. I think You put a lot of effort to create this article. I appreciate your work.
see this