Yesterday we showed you how to create a homemade Wi-Fi light. what can be controlled with Siri; today we’re going to use that knowledge to get Siri to work with the Sonos sound system as well, and then put it all together in a romantic scene.

Check out the end result:

The video above was not spoofed or staged — anything is possible using Siri and the guide is below. No jailbreak required, although there are some programs for which I have provided code.

Things you will need:

  • Raspberry Pi
  • iOS device
  • Siri compatible color lighting like Philips Hue with 2nd generation hub or our DIY Wi-Fi Light
  • Sonos (or other playback device with HTTP API)
  • (Optional) NodeMCU / ESP8266, relays and sockets; or a commercial HomeKit socket accessory

Why is it so difficult?

HomeKit, and managing your devices with voice control is really neat. Unfortunately, Apple’s closed ecosystem of HomeKit is limited to a handful of devices. Sonos is not one of them. However, not all is lost. Thanks to some industrious hackers, the HomeKit protocol has recently been changed, and as demonstrated in our latest project, you can now create your own fake HomeKit devices. We will use this.

Get started by following our tutorial » because it will be the foundation of everything else. In fact, if you don’t already have a Philips Hue lighting system, you can go ahead and actually create the Wi-Fi lighting described in this guide.

Even if you have a Hue system, go ahead and follow the instructions because you will need to create fake HomeKit accessories for everything else (code below). Continuing this tutorial, I’m going to assume that you already have a HAP-NodeJS server running and have successfully added a fake indicator and tested the server, even if it’s not connected to the actual DIY hardware.

Make a playlist

Create a new Sonos playlist called Romantic. It doesn’t have to be the same name, but you’ll have to set up additional scripts later if you change it. I added some Marvin Gaye and Sade tracks to mine. Smooth

Then, on a Raspberry Pi running HAB-NodeJS (if you don’t know what I’m talking about, you haven’t read the Wi-Fi lighting tutorial. Do this first) navigate to your home directory and enter the following to install the HTTP API for Sonos. This will give us a web URL that we can ping to trigger certain actions on Sonos.

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 

If you see a message that such and such a module was not found, just run another npm installation and module name, then try again run npm . I may have already installed modules globally from other projects and you may need to install more. Hope you see something like this:

sonos-http-api works

Congratulations, you now have a simple HTTP API that you can use to launch your Sonos. Read the documentation for a complete list of commands, but the format we’re most interested in is this:

  http: // [IP-адрес СЕРВЕРА]: 5005 / [ИМЯ КОМНАТЫ] / [ДЕЙСТВИЕ] 

or as a specific example:

  http://192.168.1.99:5005/master%20bedroom/playlist/romantic 

The case doesn’t matter. %20 is a URL-encoded space, but you should also be able to enter an actual space and your browser will automatically convert it. The above command simply runs the romantic playlist on the device in the master bedroom. Use command pauses to stop it:

  http://192.168.1.99:5005/master%20bedroom/pause 

Apart from this, the API also provides us with a feature that will be very useful for other projects: the ability to speak a specific statement, such as a notification, then continue playing the current queue. Try:

  http://192.168.1.99:5005/master%20bedroom/say/make%20use%20of%20is%20awesome/en-gb 

The first time you do this, you will be prompted to register for an API key on VoiceRSS.org [Больше не доступно] (more than 350 requests per day are free). When you’re done with that, type:

 nano settings.json 

and paste the key into the following example:

 { "voicerss":"YOURAPIKEY" } 

Restart the server and ping the URL again. In a few seconds you will hear a delightful English voice. You can change the end of the URL to en-us, if you prefer an American voice. Well-groomed! Absolutely irrelevant to this project, but still neat!

Finally, let’s start this server again after restarting the Pi:

 sudo nano /etc/rc.local 

Add line before exit 0 :

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

CTRL-X, Y to save. You can see me running this along with OpenHAB, a Bluetooth Presence Detector for the Home, and HAP-NodeJS HomeKit Server - it should also be running if you've already completed the Wi-Fi Lighting Pre-Tutorial.

add to rc-local Creating a HomeKit device for Sonos

The point of this project is that Sonos cannot be controlled by Siri, however we can add some degree of control by creating a fake HomeKit device that activates a specific playlist when we turn it on - fake light is the easiest way to do this.

First go to catalog HAP-NodeJS / and install the "request" module.

 sudo npm install request 

Then go to directory accessories / and grab my finished fixture with the following simple command:

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

Make some changes if necessary - this is set to the Sonos location "Bedroom hosts" default so change master lines %20bedroom in direction of the beginning of the code if you want it to be directed to another room. Since we were pretending to be light, sending the brightness command won't really do anything, although I'll update it at some point to handle the volume. This is the block of code 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"); }) } }, 

You can see that it's surprisingly simple: when the source receives the setPowerOn command from HomeKit, it checks if the value is 1 (or true), and at what point it sends the Sonos HTTP API URL to play that playlist. If it's something else (i.e. 0, false), it sends a pause command. Restart the HAP-NodeJS server, then go to the iOS device and follow the same procedure as before to add the fake "Romantic" indicator list playback" (you will need pairing code 031-45-154) .

Check if your new device works, then proceed. If you're a bit busy programming, try displaying the brightness changes in volume (and please share the code in the comments!).

(Optional) Fairy lights and other AC devices

It may surprise you that there aren't any official HomeKit fairy lights, and I couldn't find any universal HomeKit power outlets that I liked for the look/price - so I put together a homemade one using the NodeMCU/ESP8266 dev board. relay and standard two-way socket. I won't go into details about the hardware as you shouldn't put things in sockets unless you're completely sure about such things, but I'll provide you with my code for the NodeMCU side (connect trigger relay to D2/GPIO4 pin) and accessory for him.

node relay

Like the Wi-Fi indicator, it transmits commands over the MQTT ("sleeping-fork") channel, so you need to change the code with the address of your MQTT broker (check out the second part of our OpenHAB guide on how to set up an MQTT broker), change channel names if you like, and of course enter your own Wi-Fi details.

Putting it all together

Finally, we are going to make some "scenes" using the Elgato Eve app or your preferred HomeKit manager. In HomeKit language, a scene can consist of any number of accessories and commands for those accessories.

You can name the scene whatever you want and Siri can match it, without even saying "set the scene" . So I did two scenes, one called "kids in bed" and the other "let's be serious."

The first turns off one of the Hue lights, the other is pink, then turns on the romantic playlist accessory and fairy lights. The latter makes the lights white again and pauses Sonos (i.e. disables the playlist's romantic accessory). Simples!

creating a homekit scene

I hope you enjoyed this tutorial and can really see how we can extend Siri's control to anything without too much trouble thanks to the powerful HAP-NodeJS. Is there anything you'd like to see specific Siri actions for? Let us know in the comments and tell us how you've used Siri to control your home.

Похожие записи