AudioMelody - make better music

How to hack together Siri and Sonos to set the scene

Apple Siri

We’ve all seen it in the movies, where the automated house of the future can dim the lights and get the right music playing with just a simple voice command, but now that is possible thanks to the Internet of Things (IoT), Apple’s Siri, Sonos speakers, and a Philips Hue lighting system.

What you will need

  • Raspberry Pi running HAP-NodeJS
  • iPhone or Ipad with Siri enabled
  • Philips Hue lighting system (or other Siri-compatible coloured lighting)
  • Sonos (or other playback device with an HTTP API)

1. Create a playlist

This is the easy part – make a playlist of your favourite songs to set the mood on your iOS device. For the purposes of this tutorial, we’re going to go with setting the scene for romance – whether that is being Badults or true love – but you could do this for any mood you like.

As we are going for romance, call your playlist “Romance” – if you choose anything else you will have to channge some of the code later on.

Install the HTTP API for Sonos on the Raspberry Pi

On the Raspberry Pi, navigate to the home directory and run the following code:

git clone https://github.com/jishi/node-sonos-http-api.git sonos
cd sonos
npm install production
npm install basic-auth sonos-discovery require-fu request-promise node-static async
npm start

That’s it – you can now run a full suite of commands to control your Sonos sound system using the following format

http://[SERVER IP]:5005/[ROOM NAME]/[ACTION]

Now, let’s make this server start again when the Pi is restarted:

sudo nano /etc/rc.local

Add a line before the exit 0:

sudo node /home/pi/sonos/server.js < /dev/null &

CTRL-X, Y to save.

3. Making a HomeKit device for the Sonos

By default, Sonos cannot be controlled by Siri because of Apple’s love of walled gardens, but we can add a level of control by creating a fake HomeKit device that activates a particular playlist when we turn it on – a fake light is a simple way to do this.

Navigate to your HAP-NodeJS/ directory first, and install the “request” module.

sudo npm install request

Then, navigate to the accessories/ directory and grab a premade fixture form the kind folks at Makeuseof with the following simple command:

wget https://gist.githubusercontent.com/jamesabruce/72bd250fbcf054acaa25/raw/8272d4b9a4ef76db752cab2ddd9bdc47d09a65f1/Romantic_accessory.js

How it works is relatively simple – when the Hue light receives a setPowerOn command from HomeKit, it checks to see if the value is 1 (true), and then pings the Sonos HTTP API url to play that playlist. If the value is 0 (false) then it sends the pause command instead.

This is the code block that does the magic:

setPowerOn: function(on) {
console.log(“Turning the light %s!”, on ? “on” : “off”);
FAKELIGHT.powerOn = on;
console.log(on);
if(on){
request(‘http://localhost:5005/master%20bedroom/playlist/romantic’, function (error, response, body) {
console.log(“Sent play request”);
})
}
else{
request(‘http://localhost:5005/master%20bedroom/pause’, function (error, response, body) {
console.log(“Sent pause request”);
})
}
},

Restart your HAP-NodeJS server, and then follow the same procedure as before on your iOS device to add your fake “Romantic Playlist” light (you’ll need the pairing code 031-45-154).

And that’s it – not much to it really!

Make everything work together

Lastly, you need to make some “scenes” using the Elgato Eve app (or another HomeKit manager), so that everything will work in unison.

For this tutorial, name the scene “Time for romance” – then Siri will know exactly what you mean – and set it up so that the lights will turn off and the playlist starts with just that single command.

We’ve only just skimmed the surface of how to control your Sonos and Hue lights with Siri in this tutorial, Makeuseof has a longer and more in-depth tutorial if you’re getting stuck or want to explore some more options.

Photograph by iPhone Digital

Tagged as: