Family Encyclopedia >> Home & Garden

How to send voice notifications to Sonos speakers

The dream of any smart home enthusiast is a home they can talk to, but the reality is far from that.

Amazon Echo, for example, lets you use any number of voice-controlled features, but you must start the dialog with a question; you can't just make her announce an important message.

You can do this with a Sonos system and a little Raspberry Pi magic. Today I'm going to show you how to set up voice notifications on your Sonos system, introducing a couple of useful IFTTT recipes, OpenHAB integrations, and more to make use of the notification feature.

Note that there is a native Sonos binding for OpenHAB available, but it is known to cause memory issues due to a faulty uPnP library. I suggest skipping that for now, and using the method in this tutorial instead.

A Raspberry Pi running Raspian linux is ideal for this tutorial, but it should also work on any Linux-based home server you're running. In this case, I'm using the same Raspberry Pi running OpenHAB, the open source home automation system Introduction to OpenHAB Home Automation on Raspberry Pi Introduction to OpenHAB Home Automation on Raspberry Pi OpenHAB is a mature, open source home automation platform it runs on a variety of hardware and is protocol independent, which means it can connect to almost any home automation hardware on the market today. Read more . The rest of this tutorial assumes you're also running this on a Raspberry Pi, and either have a local terminal window open on the desktop or are logged in remotely via SSH Setting up your Raspberry Pi for headless use with SSH Setting up your Raspberry Pi for headless use with SSH The Raspberry Pi can accept SSH commands when connected to a local network (either over Ethernet or Wi-Fi), allowing you to easily set it up. The benefits of SSH go beyond altering the daily scan... Read More .

You will need

  • Raspberry Pi 2 (older models should work, but check the instructions for specific changes)
  • At least one Sonos speaker (other audio systems are not supported, this is a Sonos-only tutorial)
  • Free account at VoiceRSS.org. Sign in and then find your API key, which we'll need later to generate the voice messages.

Install the latest node

Check what version of Node you have by typing:

nodo -v 

Please note that version 6 is not supported. If you have Node 6, you need to remove it first, then follow the instructions below to install v5.5.

If you don't have v5 or an error is not found, follow these instructions to install Node. The following command assumes a Raspberry Pi 2; for older models use armv6l instead of armv7l .

wget https://nodejs.org/download/release/latest-v5.x/node-v5.12.0-linux-armv7l.tar.gz tar -xvf node-v5.12.0-linux-armv7l.tar.gz cd node -v5.12.0-linux-armv7l sudo cp -R * / usr / local 

Confirm again by typing:

nodo -v

And you should see v5.12 (or the latest you downloaded).

Next, we have some Node modules to install. We also want the Node Package Manager.

sudo apt-get install npm sudo npm install -g npm sudo npm install -g node-gyp 

Those are the prerequisites, now for the fun.

Sonos HTTP API

The Sonos HTTP API creates a web server on the local network, which allows us to ping a URL with a message to announce on a Sonos (and control it remotely if desired, although this tutorial focuses only on the web aspect). voice notification).

git clone https://github.com/jishi/node-sonos-http-api.git sonos cd sonos npm install --production npm start 

If you see a message about such and such module not found, just do another npm install and the module name, then try npm start again. If you experience errors related to “requires a C++11 compiler” , correct with the following commands:

sudo apt-get install gcc-4.8 g ++ - 4.8 sudo update-alternative --install / usr / bin / gccgcc / usr / bin / gcc-4.6 20 sudo update-alternative --install / usr / bin / gcc gcc / usr / bin / gcc-4.8 50 sudo update-alternative --install / usr / bin / g ++ g ++ /usr/bin/g++-4.6 20 sudo update-alternative --install / usr / bin / g ++ g ++ / usr / bin / g ++ - 4.8 50 

You should eventually see something like this:

How to send voice notifications to Sonos speakers

The server is now running, interacting with Sonos. The format of this API is easy:

http: // [IP DEL SERVIDOR]: 5005 / [NOMBRE DE LA SALA] / [ACCIÓN]

Or as a specific example:

http://192.168.1.99:5005/kitchen/playlist/chillout

The action we are interested in is the "say" command, used as follows:

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

You will hear an error message about having to register for an API key on VoiceRSS.org. You should have already done this, so type the following and paste your API key accordingly:

nano settings.json
 "voicerss": "YOURAPIKEY"

(Press CTRL-X, Y, to save the file)

Restart the server and ping the URL again. After a few seconds, you should hear a lovely voice in English (although it changes the end of the URL to en-us if you prefer). To make the Sonos HTTP API server restart when the Pi restarts:

sudo nano /etc/rc.local

Add a line before output 0 :

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

You now have the ability to create a voice message from anywhere on the local network by simply pinging a URL, so the possibilities are wide open at this point. However, if you're struggling for ideas, read on for some helpful notifications I've got set up.

OpenHAB Event Notifications

Let's take a simple example first:motion detection. This is a common use case for activating lights, but you might also want a voice notification if it's a motion sensor in a low-traffic area, or perhaps as a warning that someone is walking up the path. from the garden..

Regla "Se detectó movimiento en el jardín" cuando se cambió el elemento Garden_Motion y luego var String message = "Tienes un visitante" sendHttpGetRequest ("http: // localhost: 5005 / kitchen / say /" + message.encode ("UTF-8") + " / en-gb ") fin 

You should see how you can integrate these simple voice notifications into any of your rules, but let's try something a bit more complex.

Daily weather report from IFTTT to OpenHAB to Sonos

In this recipe, we'll have Sonos announce a daily weather report at your preferred time.

You will need the My.OpenHAB link enabled, as this creates a secure connection between your internal OpenHAB server and the external IFTTT service. Set it up if you haven't already; you can refer to part 1 of our OpenHAB guide Introduction to OpenHAB Home Automation on Raspberry Pi Introduction to OpenHAB Home Automation on Raspberry Pi OpenHAB is a mature, open source home automation platform that runs on a variety of hardware and is protocol agnostic, meaning it can connect to almost any home automation hardware on the market today. Read More

Next, create a new string element in your OpenHAB installation, which will store the daily weather report. Before this will be visible in the My.OpenHAB channel, we'll need to initialize it with some default variable. Ping the following URL (change raspberrypi.local to your OpenHAB server, or just use your IP address, and Today's weather to what you called the Chain):

http: //raspberrypi.local: 8080 / CMD? Todays_Weather = Sunny 

Login to My.OpenHAB and check the Items List to ensure that the variable has now been exported.

How to send voice notifications to Sonos speakers

Go back to IFTTT, create a new recipe, and use the weather channel as the trigger at your preferred time of day. You'll need to set your location first if you've never used it.

Select My.OpenHAB as the action, and choose Todays_Weather as the variable to update.

How to send voice notifications to Sonos speakers

For now, just pick the next half hour slot for testing - you can update the recipe later once you know it's working.

Sure enough, at the time of testing (in fact, a little earlier), today's forecast was successfully updated.

2016-02-27 10: 28: 01.689 [DEBUG] [o.o.i.m.i.MyOpenHABServiceImpl] - ¡Se recibió el comando Mayormente nublado hoy! Con un máximo de 7C y un mínimo de 1C. Repetir. Las condiciones serán mayormente nubladas hoy, con un máximo de 7C y un mínimo de 1C. para el artículo Todays_Weather 2016-02-27 10: 28: 01.697 [DEBUG] [o.o.i.m.i.MyOpenHABServiceImpl] - store (Todays_Weather), estado = ¡Mayormente nublado hoy! Con un máximo de 7C y un mínimo de 1C. Repetir. Las condiciones serán mayormente nubladas hoy, con un máximo de 7C y un mínimo de 1C. 

Next, we need an OpenHAB rule to send this variable to the Sonos “say” URL. The following should do it:

la regla "Anuncie el informe meteorológico diario" cuando Item Todays_Weather recibió la actualización, luego envíeHttpGetRequest ("http: // localhost: 5005 / kitchen / say /" + Todays_Weather.state.toString.encode ("UTF-8") + "/ en-gb ") final 

This is pretty simple:every time the Todays_Weather variable is updated (which will automatically be at 8am every day), ping the URL. We use encode("UTF-8") String function to make the IFTTT sentence suitable for use in a URL. To manually test this part of the system, simply use the OpenHAB HTTP API again:

http: //raspberrypi.local: 8080 / CMD? Todays_Weather = Cloudy, con posibilidad de albóndigas. 

Connect to IFTTT without OpenHAB, using the If-This-Then-Node

Finally, let's see how to connect from any IFTTT recipe without the OpenHAB intermediary. Instead, we will install another web server and expose it to the open Internet. This is not without risk - any kind of open server is a security risk, but we are mitigating this by not running a full server stack, just a small specific service that only accepts certain commands in a JSON data packet format. secure (so no SQL injection or authentication attacks will be possible). Once we're done, you'll have a public URL that you can send messages to from the IFTTT Maker channel.

Get started by setting up one of the many free dynamic DNS services. 5 Best Dynamic DNS Providers You Can Search For Free Today. 5 Best Dynamic DNS Providers You Can Search For Free Today. DynDNS was always an important mention when free dynamic DNS was activated. But now that it's gone, are there any good alternatives? Sure there is. Read more about it:I recommend DuckDNS, specifically because it provides a simple set of instructions for the Raspberry Pi, allowing it to update its IP address automatically. Follow these and remember your URL for the next steps. Your router may also have a built-in dynamic DNS feature, so check there first. In my case, I can get a *.mynetgear.com Free URL, so I've used it.

Find your router's port forwarding below. What is port forwarding and how can it help me? [MakeUseOf Explains] What is port forwarding and how can it help me? [MakeUseOf Explains] Do you cry a little inside when someone tells you there's a port forwarding problem and that's why your new app won't work? Your Xbox won't let you play games, your torrent downloads are refused... Read More setup page; this is where we will tell you what to do with incoming requests. Forward all HTTP requests (port 80) to port 1337 on your Raspberry Pi server. I assume you already have an IP address reserved for your Raspberry Pi. If you haven't, check your router's configuration page for reserved IPs while you're there, as it shouldn't change next week. so that the server suddenly stops working.

How to send voice notifications to Sonos speakers

Now to install the last bit of server software.

https://github.com/sebauer/if-this-then-node.git cd if-this-then-node / npm install node server.js 

You should see the following.

How to send voice notifications to Sonos speakers

That's fine, that just means we need to update our details. Open config.js in Nano and edit those details to something other than the default, then save. When you run the server again, you should see this:

How to send voice notifications to Sonos speakers

Cool. You can access your machine from the outside world by typing your dynamic DNS hostname and add /ifttn / to the end of the URL. If everything works, the message. “IFTTN - if version 2.0.1 of if-this-then-node is up and running!” will be displayed in your browser. Again, to make this script run at startup, edit the /etc/rc.local file and add:

nodo sudo /home/pi/if-this-then-node/server.js < /dev/null &

If-This-Then-Node works via plugins:some default ones are supplied, but we don't care about them. Instead, download this custom plugin I've written for you with the following commands.

cd plugins wget https://gist.githubusercontent.com/jamesabruce/4af8db24ba3452b94877/raw/d11c1cff3aa44dbb6a738eeb15202f3db461de75/sonos.js 

You will also need to install the application module.

solicitud de instalación npm 

Then restart the server. The plugin is pretty simple, but a bit different than the URL we've used so far. In this case, I used the special. “Sayall” Action, which sends the message to each Sonos device.

solicitud ('http: // localhost: 5005 / sayall /' + params.message + '/ en-gb', función (error, respuesta, cuerpo) 

If you prefer to be able to target specific devices, replace that line with:

solicitud ('http: // localhost: 5005 /' + params.device + '/ say /' + params.message + '/ en-gb', función (error, respuesta, cuerpo) 

Add another line parameter called device to the JSON request below specifying the Sonos device name.

To test this, I'm using the IFTTT “Make” button app Create a new recipe, browse the channels, and select the Manufacturer channel.

How to send voice notifications to Sonos speakers

Create a new recipe. Enter the URL you configured earlier of a free dynamic DNS provider (including /ifttn/ at the end of the URL. Don't forget those bars. Select SEND request, application/json the content and body of the message as follows, replacing all variables as necessary with the ones you set earlier:

"action": "sonos", "user": "YOURUSER", "pw": "YOURPASSWORD", "message": "Mensaje entrante para todos: ¡La cena está lista! Repita. La cena está lista. Levanta el culo ahora . " 

I now have an easily accessible button that will announce the message on every Sonos device in the house. You can of course pair this with any IFTTT channel you want to make a custom message using variables from that channel. You should even be able to replicate the weather report from above without using OpenHAB now, though it took the extra effort of installing another server.

Note:hackers will scan you automatically . This happens all the time, but if you're looking at logs, it can seem alarming. Below you can see someone systematically trying to determine if any of PHPMyAdmin's standard interfaces were accessible. Which they weren't. Don't be alarmed if you see something similar. It's the internet equivalent of cold calling random phone numbers to see if anyone is there.

How to send voice notifications to Sonos speakers

What are you going to announce??

You should now have the tools and knowledge to constantly bathe your entire home in voice notifications about everything you can think of. Who needs music? Your only limit is 350 daily VoiceRSS.org requests, but that's about one every 4 minutes, so you should be fine.

So what are you going to do with this? Share your ideas or the IFTTT recipe in the comments!