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

Amazon Echo, for example, lets you use any number of voice features, but you have to start a dialogue with a question — you can’t just ask her to announce an important message.

You you can do it with a Sonos system and a little Raspberry Pi magic. Today, I’ll show you how to set up voice notifications on your Sonos system by introducing some helpful IFTTT recipes, OpenHAB integrations, and more to make use of the notifications feature.

Please note that there is a built-in Sonos binding for OpenHAB, but it is known to cause memory issues due to a faulty uPnP library. I would suggest skipping this for now and using the method from this tutorial instead.

A Raspberry Pi with Raspian Linux is perfect for this tutorial, but it should also work on whatever Linux-based home server you use. In this case, I’m using the same Raspberry Pi that runs OpenHAB, an open source home automation system. The rest of this tutorial assumes that you are also running this on a Raspberry Pi and have a local terminal window open on your desktop, or that you are logged in remotely using SSH .

you need

  • Raspberry Pi 2 (older models should work, but check for specific changes in the instructions)
  • At least one Sonos speaker (other audio systems are not supported, this guide is for Sonos only)
  • Free account on VoiceRSS.org [больше не доступен]. Sign up, then find your API key, which we’ll need later to create voice messages.

Last installation node

Check which version of Node you have by typing:

node -v 

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

If you don’t have v5 or get a not found error, 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:

 node -v 

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

Next, we have a few Node modules to install. We also need a Node Package Manager.

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

These are the prerequisites, now the fun part.

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 to Sonos (and control it remotely if you wish, although this tutorial only covers the voice announcement aspect).

 git clone https://github.com/jishi/node-sonos-http-api.git sonos cd sonos npm install --production 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 . If you are experiencing errors related to «requires C++11 compiler» fix them with the following commands:

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

Eventually you should see something like this:

sonos-http-api works

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

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

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 when registering for an API key on VoiceRSS.org. You should already have done this, so type the following and paste in your API key as appropriate:

 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 will hear a delightful English voice (although you will change the end of the URL to en-us, if you want to). To get the Sonos HTTP API server to start again after restarting the Pi:

 sudo nano /etc/rc.local 

Add line before exit 0 :

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

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

OpenHAB event notifications

Let's look at a simple example first: motion detection. This is a common use for lights, but you might also want a voice alert if it's a motion sensor in a low traffic area, or perhaps as a direct warning that someone is walking down a garden path.

 rule "Garden motion detected" when Item Garden_Motion changed then var String message = "You have a visitor" sendHttpGetRequest("http://localhost:5005/kitchen/say/"+message.encode("UTF-8")+"/en-gb") end 

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

Daily weather forecast from IFTTT to OpenHAB for Sonos

In this recipe, Sonos will announce the daily weather forecast at your convenience.

You need to enable the My.OpenHAB binding 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 the first part of our OpenHAB guide. see for instructions, otherwise I'm assuming you've already set it up with persistence enabled for all elements.

Next, create a new String element in your OpenHAB installation that will hold the daily weather report. Before it is visible in the My.OpenHAB channel, we need to initialize it with some default variable. Ping the following URL (replace raspberrypi.local to your OpenHAB server or just use its IP address and Todays_Weather to whatever you named String):

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

Login to My.OpenHAB and check the list elements, to make sure the variable has been exported.

my openhab export receipt

Go back to IFTTT, create a new recipe and use the Weather channel as a trigger for the time of day you choose. You will first need to provide your location if you have never used it before.

Select My.OpenHAB as the action and select Todays_Weather as the update variable.

ift weather forecast

For now, just choose the next half hour to test - you can update the recipe later when you know it works.

Of course, during testing (in fact, a little earlier), I successfully updated the forecast for today.

 2016-02-27 10:28:01.689 [DEBUG] [ooimiMyOpenHABServiceImpl] - Received command Mostly Cloudy today! With a high of 7C and a low of 1C. Repeat. Conditions will be Mostly Cloudy today, with a high of 7C and a low of 1C. for item Todays_Weather 2016-02-27 10:28:01.697 [DEBUG] [ooimiMyOpenHABServiceImpl] - store(Todays_Weather), state = Mostly Cloudy today! With a high of 7C and a low of 1C. Repeat. Conditions will be Mostly Cloudy today, with a high of 7C and a low of 1C. 

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

 rule "Announce daily weather report" when Item Todays_Weather received update then sendHttpGetRequest("http://localhost:5005/kitchen/say/"+Todays_Weather.state.toString.encode("UTF-8")+"/en-gb") end 

It's easy enough: whenever the Todays_Weather variable is updated (which will happen automatically at 8am each day), ping the URL. We use the function encoding ("UTF-8") String to make the offer from IFTTT 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, with a chance of meatballs. 

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

Finally, let's see how to connect from any IFTTT recipe without the OpenHAB middleman. Instead, we will set up another web server and expose it to the open Internet. It's not without risk: any kind of open server is a security risk, but we mitigate it by not running the full server stack, but just a small ad hoc service that only accepts certain commands in a secure JSON data packet format (so no SQL injection or authentication will possible). Once we're done, you'll have a public URL that you can post to from the IFTTT Maker channel.

Start by setting up one of the many free dynamic DNS services there - I recommend DuckDNS, especially because it gives a simple set of instructions for the Raspberry Pi, allowing it to automatically update your IP address. Follow these instructions and remember your URL for the next steps. Your router may also have a built-in dynamic DNS feature, so check that first. In my case I can get the url *.mynetgear.com free so I used it.

Then look for your router's port forwarding. configuration page; Here 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'm assuming you already have a reserved IP for your Raspberry Pi - if you don't have one, check your router's config page for reserved IPs while you're there as you can't change it next week and for the server to suddenly stop working.

port forwarding router configuration

Now to install the last bit of the 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.

if it is, then node defaults error

This is normal, it just means that we need to update our data. Open config.js file in Nano and edit this data without using the defaults and then save. When you start the server again, you should see this:

if it is then node server is running

Excellent. To check if your computer is accessible from the outside world, enter a dynamic DNS hostname and add / ifttn / to the end of the URL. If everything works, a message will be displayed in your browser "IFTTN - if-this-then-node Version 2.0.1 is up and running!" . Again, to run this script on startup, edit the file /etc/rc.local and add:

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

If-This-Then-Node works through plugins - there are several standard ones, but we are not interested in them. Instead, download this custom plugin I wrote for you with the following commands.

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

You also need to install the module request .

 npm install request 

Then restart the server. The plugin is quite simple, but slightly different from the URL we've been using so far. In this case, I've used a custom "sayall" action that sends a message to each Sonos device.

 request('http://localhost:5005/sayall/'+params.message+'/en-gb', function (error, response, body) 

If you want to use specific devices, replace this line with:

 request('http://localhost:5005/'+params.device+'/say/'+params.message+'/en-gb', function (error, response, body) 

Add another line parameter called device, to the JSON request below to provide the name of the Sonos device.

To test this, I use the IFTTT "Make" button app. Create a new recipe, go to channels and select a channel Maker .

make a button

Create a new recipe. Enter the URL you set up earlier from a free dynamic DNS provider (including /ifttn/ at the end of the URL. Don't forget those slashes). Select request POST content application/json and the text of the message as follows, replacing all the necessary variables with those that you previously configured:

 { "action":"sonos", "user":"YOURUSER", "pw":"YOURPASSWORD", "message":"Incoming message for everyone: Dinner is ready! Repeat. Dinner is ready. Get your asses upstairs now." } 

I now have an easily accessible button that will announce a message on every Sonos device in the house. Of course, you can link this to any IFTTT channel that you want to create a custom message for using variables from that channel. You will even be able to replicate the weather report from above without using OpenHAB now, although it took extra effort to set up another server.

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

web server hacking attempts

What will you announce?

You should now have the tools and knowledge to bathe the whole house in voice notifications all the time about anything you could think of. Who needs music? Your only limit is 350 daily requests from VoiceRSS.org - but that's about once every 4 minutes, so you should be fine.

So what will you do about it? Share your ideas or IFTTT recipe in the comments!

Image Credits: coffee and woman by S_Photo via Shutterstock

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