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

    Lyricist III
    March 19, 2014
    jishi,

    The group volume worked great and I thought the presets was working fine but now I see that it will only group on a fresh start of sonos-remote.

    For example:
    -Group the speakers via IR key
    -Ungroup speakers via the Sonos app
    -Attempt to regroup the speakers via IR key; it does not succeed (#1)
    -Kill sonos-remote and restart sonos-remote
    -Attemp to group via IR key and it does regroup. (#2)

    #1:
    00 KEY_POWER
    before action true
    applying preset { players:
    [ { roomName: 'Living Room', volume: 51 },
    { roomName: 'Dining Room', volume: 51 } ] }
    skipping breakout because already coordinator
    after action false
    Living Room "urn:schemas-upnp-org:service:RenderingControl:1#SetVolume" STATUS: 200
    Dining Room "urn:schemas-upnp-org:service:RenderingControl:1#SetVolume" STATUS: 200
    Living Room "urn:schemas-upnp-org:service:AVTransport:1#Play" STATUS: 200
    emitting group-volume

    #2
    00 KEY_POWER
    before action true
    applying preset { players:
    [ { roomName: 'Living Room', volume: 51 },
    { roomName: 'Dining Room', volume: 51 } ] }
    skipping breakout because already coordinator
    after action false
    01 KEY_POWER
    before action false
    after action false
    Living Room "urn:schemas-upnp-org:service:RenderingControl:1#SetVolume" STATUS: 200
    Dining Room "urn:schemas-upnp-org:service:AVTransport:1#SetAVTransportURI" STATUS: 200
    Dining Room "urn:schemas-upnp-org:service:RenderingControl:1#SetVolume" STATUS: 200
    Living Room "urn:schemas-upnp-org:service:AVTransport:1#Play" STATUS: 200
    emitting group-volume
    emitting group-volume

    You can see the contents of my sonos-lirc.js here:

    http://ur1.ca/gvtr8

    Also, out of curiosity:

    -What is the difference between player.coordinator.groupSetVolume and player.groupSetVolume
    -Are you using pm2 to run sonos-lirc as a forked daemon on your rapi?

    P.S. I appreciate your support but if I'm consuming too much of your time feel free to tell me to bugger off.
    jishiAuthor
    Headliner II
    March 19, 2014
    player is the selected player, which in your case would always be TV Room since you don't have buttons to switch control.

    player.coordinator is the coordinator player for a group. This is the player that you "initiated" the group from. According to your preset this would always be TV Room, but if you were to add "TV Room" to "Dining Room" from your Sonos controller, your groupVolume control wouldn't work.

    I used pm2 ti run this yes, no problem. I have now switched my raspberry installation to rasplex, which is a bit dumber, so now I just start it with a autostart script like:
    node sonos-lirc.js &
    It won't restart if it crashes with this approach though.

    It's weird that the preset only works one time, I haven't noticed this behavior in my setup. IT doesn't look like it breaks according to your logs either...
    Lyricist III
    March 20, 2014
    Bummer okay - I'll just restart the sonos-remote node every 5 minutes or so.
    jishiAuthor
    Headliner II
    March 20, 2014
    Well, that's not really the optimal solution. I would need to get some info about the state of the players before and after you try and group them the second time.

    If you could give me the info from http://IP:1400/status/perf

    From both players and PM them to me. I only need the first few lines (10 or so), the rest is irrelevant.
    Lyricist II
    March 20, 2014
    As I was lacking the music library browsing feature which is my main use, so I decided to start developping it myself. I actually have something working.
    It still needs some polishing and some functions (like adding a whole album to the queue for example) are still missing but browsing back and forth between artists and albums does work as well as going back to the previous menu.

    jishi (or anyone else) : if you're interested I can send you the code in some way (via mail or github). Feel free to do whatever you want with it.

    I do have one question though, one thing that I'm missing is the set of icons used by the Sonos app to have the same look & feel, I was wondering how you got the rest of the design ?

    Best regards
    jishiAuthor
    Headliner II
    March 20, 2014
    Did you fork it on github? Then just send me a pullrequest and I'll try and merge it in.

    I extracted most of the graphical elements from the Windows application, since they were basically embedded png resources. However, I wanted the gui to scale nicely, so I remade a lot of them in SVG and tried to reproduce most of it using standard css. Today it is only the volume scrubber and the shuffle/repeat/crossfade that are still images extracted from the Sonos application.

    If you tell me which one you need I can easily extract them and add to the repo.
    Lyricist II
    March 21, 2014
    For now it's on my home git repo but I'll upload it on my fork on github and send you the link.

    I'm looking for the icons used for the music library browsing : back icons, Favorites and Music library icons. If you already have them that would help me otherwise I'll try to get them from the windows app too.

    I've also looked to implement the "Radio" feature but haven't had the opportunity to do the packet captures to understand how the TuneIn implementation in Sonos works.

    Regards
    jishiAuthor
    Headliner II
    March 21, 2014
    For now it's on my home git repo but I'll upload it on my fork on github and send you the link.

    I'm looking for the icons used for the music library browsing : back icons, Favorites and Music library icons. If you already have them that would help me otherwise I'll try to get them from the windows app too.

    I've also looked to implement the "Radio" feature but haven't had the opportunity to do the packet captures to understand how the TuneIn implementation in Sonos works.

    Regards


    Let me know if you manage to extract them (I think I used Telerik JustDecompile http://www.telerik.com/products/decompiler.aspx), otherwise I can fix it easily.

    I think TuneIn and most music services connect directly from the controller to the services without using the players, which makes it a bit more troublesome to implement. TuneIn doesn't have a free API and you need some credentials (I think) in order to get it to work.
    Lyricist II
    March 21, 2014
    Hello again,

    You can check my changes on my github forks :
    * https://github.com/gfraysse/node-sonos-discovery
    * https://github.com/gfraysse/node-sonos-web-controller

    Let me know if you feel it is of any interest it lacks some polishing but I'm still working on it.
    I still need to get the missing icons and integrate them.
    Contributor I
    March 23, 2014
    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

    Cheers!


    Awesome Library! I've used it to create a tool that does store the listening positions of titles longer 10 minutes (configurable). And it restores the last known position when you start it again.

    http://www.schrankmonster.de/2014/03/23/give-you-sonos-audiobook-podcast-auto-bookmarker-lose-listening-progress-again/

    Of course everything also on Github: https://github.com/bietiekay/sonos-auto-bookmarker