Skip to main content
Headliner II
Mar 5, 2013 10:27 PM UTC

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

    jishiAuthor
    Headliner II
    Apr 22, 2014 8:32 PM UTC
    Well, I started on it but ran into some issues that resulted in banging my head into the wall an evening and then I gave up and went on with other matters in my life ;)

    The error you are seeing is because it hasn't the necessary sonos-discovery module in it's node_modules folder. It should have the appropriate dependency added in the package.json file so a:

    npm install

    should fix that. However, configuring LIRC isn't as straightforward as one would hope though 🙂 Unfortunately I wiped my installation so I don't have the possibility to check my config right now.
    Translate
    jishiAuthor
    Headliner II
    Apr 23, 2014 8:46 AM UTC
    Doesn't seem like you are standing in the folder where the package.json exists for the IR project.

    If you installed the node-sonos-remote-control into /home/pi/sonos-remote-control, you need to cd into that dir and invoke "npm install".
    Translate
    Avid Contributor I
    Apr 23, 2014 5:47 PM UTC
    Great, that should work. I try it later when I'm at home.
    How is it possible to get the "Turn Light on when audio is playing" feature from the Raspberry Pi? Is there a setting for that?
    Translate
    Matt W
    Sonos Employee
    Apr 28, 2014 4:57 PM UTC
    Hi. Just wanted to drop a quick note of thanks for node-sonos-discovery. I used it in my project to get a Griffin PowerMate to control my Sonos. You can check it out here:

    Controlling a Sonos with the Griffin PowerMate
    Translate
    Principal Sonos Developer Advocate
    jishiAuthor
    Headliner II
    Apr 28, 2014 7:32 PM UTC
    Hi. Just wanted to drop a quick note of thanks for node-sonos-discovery. I used it in my project to get a Griffin PowerMate to control my Sonos. You can check it out here:

    Controlling a Sonos with the Griffin PowerMate


    Wow, that's a neat combo! Ever considered adding an LCD display to make use of the wheel even more? Thinking about the favorites short coming. If you had like double press, then roll wheel to select favorite. Or if you could make a logical blink sequence to feeback which favorite you select.

    Double press, blink once. If you turn the wheel, it would blink twice, trice etc and then press it again selects the favorite.
    Translate
    jishiAuthor
    Headliner II
    Apr 28, 2014 7:35 PM UTC
    Great, that should work. I try it later when I'm at home.
    How is it possible to get the "Turn Light on when audio is playing" feature from the Raspberry Pi? Is there a setting for that?


    No, there is no setting. You will need to hack it in yourself.

    Depending on what you are running, add this second line right after this line:

    code:

    var discovery = new SonosDiscovery();
    discovery.setToggleLED(true);


    You only need to do this for one of your apps, if you are running multiple (like, web-controller and http-api)
    Translate
    Matt W
    Sonos Employee
    Apr 28, 2014 7:35 PM UTC
    Wow, that's a neat combo! Ever considered adding an LCD display to make use of the wheel even more? Thinking about the favorites short coming. If you had like double press, then roll wheel to select favorite. Or if you could make a logical blink sequence to feeback which favorite you select.

    Double press, blink once. If you turn the wheel, it would blink twice, trice etc and then press it again selects the favorite.


    Actually, what I'm looking at now is caching the favorites whenever I receive a "favorites" emit, and then using some kind of text-to-speech to read the favorites when the wheel is turned and the zone player is off. This would go through the Pi's speaker, mostly because I don't want to destroy the Sonos queue by shoving an MP3 at it.

    I considered (and am still considering) the LCD thing, but the text to speech thing sounds more fun! :D

    I'll keep the thread up to date.
    Translate
    Principal Sonos Developer Advocate
    jishiAuthor
    Headliner II
    Apr 29, 2014 1:15 PM UTC
    This would go through the Pi's speaker, mostly because I don't want to destroy the Sonos queue by shoving an MP3 at it.


    FYI, you can play an MP3 on the player without touching the queue. A SetAVTransportURI call will change what is currently playing to a single item which doesn't affect the queue (similar to a radiostation). However, you would loose the queue position and track position, but that could be saved before hand and restored after.

    To restore the queue, you just call SetAVTransportURI and set it to "x-rincon-queue:RINCON_12345678901201400#0" where the RINCON_xxxxxxxxx is the UUID of the player (I don't know why it needs the uuid, I don't think you can set a player to play another players queue...).

    Worth noting is that you don't need to use the x-rincon-mp3radio: scheme for it neither, you can just set it to a connectable http url like: http://192.168.0.100:1234/tts.mp3 and it will play it straight up. It just need to end in .mp3 for it to read is as an mp3.

    A bit more work, and maybe you don't want the sound through your Sonos anyway, but I just wanted to point out that possibility.
    Translate
    Matt W
    Sonos Employee
    Apr 29, 2014 4:04 PM UTC
    FYI, you can play an MP3 on the player without touching the queue. A SetAVTransportURI call will change what is currently playing to a single item which doesn't affect the queue (similar to a radiostation). However, you would loose the queue position and track position, but that could be saved before hand and restored after.


    Thanks. That's a neat solution. I've figured out how to set the mp3 url, then set the queue uri back, but how can I get (and then restore) the queue (and even track, though less important) position?
    Translate
    Principal Sonos Developer Advocate
    jishiAuthor
    Headliner II
    Apr 29, 2014 4:11 PM UTC
    Thanks. That's a neat solution. I've figured out how to set the mp3 url, then set the queue uri back, but how can I get (and then restore) the queue (and even track, though less important) position?

    You need to note the trackno that was active, then use the seek() method to restore it after you have re-enabled the queue. If you have noted the elapsed time as well, you can trackSeek() as well, before you invoke play().

    However I just realized, I don't know what would happen for random playback, because I don't know when the random sequence is set, and if it get reset if you disable the queue temporarily. That might perhaps be a bit of a problem.
    Translate