Our Boston sales office has a big set of Sonos speakers around their office that pumps tunes out across the office. It’s great for camaraderie, until someone sticks country music on… On a visit, I had an hour to spare, so I decided to find out if I might be able to log the music activity to Logentries, and it was surprisingly simple.

After some quick googling, I found a few node.js libraries that interface with Sonos, and the Sonos HTTP API by Jishi seemed like the easiest to set up. The HTTP API is a simple node js web server that offers a number of HTTP endpoints that lets you interface with sonos programmatically, or with manual GET requests. You can view the state of the Sonos device as you change tracks, pause the music, or turn up the jam.

To do a quick test, I installed the code on my laptop with npm:

npm install git://github.com/jishi/node-sonos-http-api.git

Then I fired up the web server and I was up and running in seconds.

cd node_modules/sonos-http-api/ npm start

The Node.js module automatically detects our Sonos speakers.

After I’d confirmed the API was working by sticking on some minimalist techno, I set up the web server to log to logentries using our webhook logging.

Firing to Logentries

The Web Server’s documentation mentions experimental webhook logging, perfect!

On Logentries, I created a manual TCP token log, and copied the token I received. here’s a guide for that.

Once I had my token, I added a settings file to the root directory of the server (node_modules/sonos-http-api/)

(Replace TOKEN below with the token you got when creating your manual log).

{ "webhook": "https://webhook.logentries.com/webhook/logs/*TOKEN*" }

After restarting the server, everything that happened to our Sonos speakers was logged to logentries, excellent!

And this data is going to look perfect in our upcoming table view!

Once the data is coming into Logentries, we can run a lot of interesting queries against it.
For example, we can see how the volume changes over the course of an hour:
calculate(average:volume) timeslice(10)

The only problem with this was that the web server was running on my laptop, and I couldn’t leave it in Boston when I went back to Dublin, so the next step is to hook this up on a Raspberry Pi, expect an update on that soon!

To start logging from your Sonos speakers create a free Logentries account today!

For another interesting way to use our webhook logging be sure to check out: How to Log Messages from Slack