Skip to main content
Headliner II
March 5, 2013

Sonos with node.js, my attempt!

INFO

Source code here: https://github.com/jishi/node-sonos-http-api
Sonos Web Controller draft (work in progress!): https://github.com/jishi/node-sonos-web-controller
Sonos IR control (requires lircd and IR reciever): https://github.com/jishi/node-sonos-remote-control

==============================================================


I know that there already is an attempt at this by this guy: http://forums.sonos.com/showthread.php?t=32643, but I found it to be a bit lacking in functionality so I decided to write up my own.

I have created a simple web-based API using what I have, which could be useful for integrating stuff with other applications. This web based API is inspired by other RESTful APIs, however I don't think it follows the correct guidelines to be called a REST API.

IT supports most basic features like:

play, pause, seek, next, prev, volume, mute, setAVTransportURI

It also supports these advanced fatures:

State of player as JSON, zone info as JSON, Play favorite item, presets (grouping, volume, avTransportURI)

You can read more in the README for each project.

To run it as a service under linux, I suggest using pm2 (https://github.com/Unitech/pm2). You need to run it in forked mode (-x)!

For Windows, you might try Winser http://jfromaniello.github.io/winser/

Cheers!
    This topic has been closed for further comments. You can use the search bar to find a similar topic, or create a new one by clicking Create Topic at the top of the page.

    391 replies

    Avid Contributor I
    December 6, 2014
    Thanks a lot, jishi; again!

    Yes; I was being dense. I forgot all about the install procdedure, but updating/reinstalling was a breeze when I found my notes. I have had a very stable system with your previous version.

    And the grouped custom action works great. Now I'll just script

    code:
    curl -s http://localhost:5005/{room}/grouped |grep false && curl http://localhost:5005/{room}/pause


    instead of just 'pause'. I can't see any advantage to having this linked to zone actions. This is perfect.

    Thank you ever so much!

    PS:
    I never got pm2 installed. I believe because I had trouble installing a recent enough version of node (the easiest way of updating node on raspbian was sudo npm cache clean -f ; sudo npm install -g n ; sudo n sudo n 0.8.21). Therefore, I used rc.local, and the API has always been available. I just created the following file (with execute permissions): /home/pi/node-sonos-http-api-master/rc_local.sh

    I put the folloing in it:

    code:
    #!/bin/sh
    sleep 60
    cd /home/pi/node-sonos-http-api-master
    /usr/local/bin/node server.js >> /home/pi/node-sonos-http-api-master/node.log 2>&1


    I assume, of course that your user name is pi, and that node-sonos-http-api-master is in your home directory; change accordingly.

    Then add to '/etc/rc.local' (obviouslys before the line "exit 0".)

    code:
    # Starting Sonos http api
    sh /home/pi/node-sonos-http-api-master/rc_local.sh &


    My reference for this was: http://www.raspberrypi.org/documentation/linux/usage/rc-local.md

    Of course, this will not make it run as a service but good enough for me.
    jishiAuthor
    Headliner II
    December 6, 2014

    PS:
    I never got pm2 installed. I believe because I had trouble installing a recent enough version of node (the easiest way of updating node on raspbian was sudo npm cache clean -f ; sudo npm install -g n ; sudo n sudo n 0.8.21).


    There is a prebuilt binary for the pi. It is usually a few versions behind, but nevertheless newer than the one in rasbian repo:

    http://nodejs.org/dist/v0.10.28/node-v0.10.28-linux-arm-pi.tar.gz

    I usually use that one, and is very easy to install (it's self contained, no dependencies). The README has a oneliner for extracting it into /usr/local/ or whichever path you want to add it to.
    Avid Contributor I
    December 8, 2014
    Hi, again, jishi!

    I am becoming a downright nuisance, aren't I?

    Ok, so I had a total system crash, and decided to make a fresh install, and took the opprotunity to install the version of node you linked to. Very easy, yes! I installed the newest version of the sonos api, but my problem is that my 'say' scripts are behaving very wonky. The new version of 'say' seems to behave rather differently from the old one. Firstly, nothing worked because I had a sentence with an exclamation mark in the script (exclamation marks work fine from the browser addres bar). In the scripts, I use '%20' instead of spaces. This is fine; but the deal breaker now is that whenever I issue a second say command (from a script or from the browser address bar), the first say phrase is repeated, either after the new phrase or instead of it... This even survives a reboot! (I guess becaue it is in the sonos "memory".) Any idea?
    jishiAuthor
    Headliner II
    December 8, 2014
    Hi, again, jishi!

    I am becoming a downright nuisance, aren't I?

    Ok, so I had a total system crash, and decided to make a fresh install, and took the opprotunity to install the version of node you linked to. Very easy, yes! I installed the newest version of the sonos api, but my problem is that my 'say' scripts are behaving very wonky. The new version of 'say' seems to behave rather differently from the old one. Firstly, nothing worked because I had a sentence with an exclamation mark in the script (exclamation marks work fine from the browser addres bar). In the scripts, I use '%20' instead of spaces. This is fine; but the deal breaker now is that whenever I issue a second say command (from a script or from the browser address bar), the first say phrase is repeated, either after the new phrase or instead of it... This even survives a reboot! (I guess becaue it is in the sonos "memory".) Any idea?


    No worries, I anticipate a certain amount of support is needed and I'm happy to help as long as people aren't to dense :)

    I can not reproduce the behavior that you are seeing. What I do notice it that if the player wasn't grouped, it would repeat the same message twice, but then stop. This is of course not optimal, and started when I tried to fix the resuming of the player after a TTS command.

    If the player continued to play even after a reboot, my guess is that it is your trigger script that has started repeating it self, since a player is cleared after a reboot (queue is gone, the current playing track is also gone).
    Avid Contributor I
    December 10, 2014
    I was not being clear, I meant pi reboot, not sonos reboot.

    To reproduce:
    Reboot the pi and the sonos player you will be using. From a browser, issue

    code:

    http://localhost:5005/{Room}/say/Test/en


    This will produce the expected results.

    Issuing

    code:
    http://localhost:5005/{Room}/say/This is a test sentence. This Too. And this. /en


    will produce the expected result, but the phrase from the first say command (Test) is repeated after the second phrase.

    Does this only happen on my system?
    jishiAuthor
    Headliner II
    December 10, 2014

    code:
    http://localhost:5005/{Room}/say/This is a test sentence. This Too. And this. /en


    will produce the expected result, but the phrase from the first say command (Test) is repeated after the second phrase.

    Does this only happen on my system?


    I'm aware that issuing a "say" command to a player that has previously only issued a say command, would repeat the first say command once again. This is a bug, introduced when I tried to fix resuming. But it doesn't keep repeating it over and over again, that was what I was curious about.

    The say command was contributed by another user and very experimental. The url syntax that is used isn't well equipped for this kind of stuff, and one would probably like more features for it to be useful, like:

    grouping all players temporarily for a TTS message, then resume them again

    Ungroup a player temporarily for a message and then regrouping it again (this works today)

    Some sort of command builder with a text field where you can write anything in it and it would be escaped accordingly to allow for weird characters etc.
    Avid Contributor I
    December 11, 2014
    Ok, thanks for the feedback, jishi.

    The previous version of the 'say' command, was very useful to me. I had written elaborate scripts announcing weather and stuff when movement in the bathroom was detected on weekday mornings. It still is useful to announce what radio station is about to be played (I use an rf remote, and one button scrolls through a list of favourite stations). Of course, the 'say' command is experimental and a work in progress. I'll stay with current version because of the other new features. Thanks!
    jishiAuthor
    Headliner II
    December 15, 2014
    Ok, thanks for the feedback, jishi.

    The previous version of the 'say' command, was very useful to me. I had written elaborate scripts announcing weather and stuff when movement in the bathroom was detected on weekday mornings. It still is useful to announce what radio station is about to be played (I use an rf remote, and one button scrolls through a list of favourite stations). Of course, the 'say' command is experimental and a work in progress. I'll stay with current version because of the other new features. Thanks!


    Well, apart from the repeating issue, it still works the same way? I take it that the regrouping isn't an issue for you?
    jishiAuthor
    Headliner II
    December 20, 2014


    code:
    http://localhost:5005/{Room}/say/This is a test sentence. This Too. And this. /en


    will produce the expected result, but the phrase from the first say command (Test) is repeated after the second phrase.

    Does this only happen on my system?


    I have tried to fix the repeating issue. If you want to try it out, checkout the latest version.
    Avid Contributor I
    December 21, 2014
    Thanks you so much for your efforts, jishi! I've been quite busy, but I just saw your message, and installed the latest version. It works a lot better! Mostly everything is fine, almost like the previous version. I'll put my TTS functionality back up and see how it goes. Thanks!