Sonos with node.js, my attempt!


Userlevel 4
Badge +14
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.

397 replies

Userlevel 2
First I am hacker not a programmer but i have manage to install your code and looking forward to learning from it but I have a few ?

Could you provide a complete zip for the html and web interface as i keep getting init errors when running server will forward errors later

it maybe that you are not supporting the older sonos units upon scanning zp80 and the zp100 as the code seems to die when it finds that unit


Many thanks for all your amazing work .....I now have some more work to play with for my hobby

Regards

Robert
Userlevel 4
Badge +14

it maybe that you are not supporting the older sonos units upon scanning zp80 and the zp100 as the code seems to die when it finds that unit


I don't have any old units my self so I haven't tested it. However, I can't think of anything that should misbehave just because of this. If you have any errors, please post them here and maybe it will give me a hint. In the meantime maybe someone else can confirm if old units work?
robertboca,

I'm not using jishi's package, but I would not expect any model specific issues at the level jishi's program operates. Is there something different about the connection (Wired? Wireless?) to these two units?
Userlevel 2
yes one is wired and the wireless:

C:\xampp\htdocs\sserver>node server.js
http server listening on port 5005
scanning for players...
trying to subscribe to topology 192.168.1.209
trying to subscribe to topology 192.168.1.111
subscribing to events 192.168.1.111 Patio uuid:RINCON_000E5813DC2801400 120
subscribing to events 192.168.1.209 Master Bedroom uuid:RINCON_000E5813DC7201400
120
subscribed to topology 192.168.1.111 412
subscribed to topology 192.168.1.209 412
trying to subscribe to topology 192.168.1.162
type LastChange
Volume
subscribing to events 192.168.1.162 Living Room uuid:RINCON_000E5850693601400 12
0
type LastChange
TransportState
type LastChange
TransportState
trying to subscribe to topology 192.168.1.50
type LastChange
Volume
type LastChange
Volume

events.js:72
throw er; // Unhandled 'error' event
^
Error: Unclosed root tag
Line: 0
Column: 1240
Char:
at error (C:\xampp\htdocs\sserver\node_modules\sonos-discovery\node_modules\
sax\lib\sax.js:616:8)
at strictFail (C:\xampp\htdocs\sserver\node_modules\sonos-discovery\node_mod
ules\sax\lib\sax.js:636:22)
at end (C:\xampp\htdocs\sserver\node_modules\sonos-discovery\node_modules\sa
x\lib\sax.js:623:27)
at Object.SAXParser.end (C:\xampp\htdocs\sserver\node_modules\sonos-discover
y\node_modules\sax\lib\sax.js:137:24)
at SAXStream.end (C:\xampp\htdocs\sserver\node_modules\sonos-discovery\node_
modules\sax\lib\sax.js:209:16)
at IncomingMessage.onend (_stream_readable.js:483:10)
at IncomingMessage.g (events.js:175:14)
at IncomingMessage.EventEmitter.emit (events.js:92:17)
at _stream_readable.js:920:16
at process._tickCallback (node.js:415:13)

C:\xampp\htdocs\sserver>
Userlevel 4
Badge +14
Is one of them using fixed volume perhaps? Seems like it choke on the xml sent by a volume event. I should probably add an error handler for the xml parsing, since it fails occasionally for no apparent reason too.
Userlevel 2
one is using fix output setting correct
Userlevel 4
Badge +14
one is using fix output setting correct

Hm, what version are you actually running? It looks like it's an old version. I have zipped up a new one and you can test with that.

http://upload.grabbarna.se/files/node-sonos-http-api_0.4.3.zip
Userlevel 4
Badge +14
Found the volume control over the last release or two to be getting kinda funky. Response for volume control is slow and a bit less predictable. By that I mean, when I go to slide the volume up, it seems to go down a bit then up. The behaviour seems to be less predictable than a few releases ago. I will test further t o see if it's just me.

I did a major rewrite of the volume element to support individual volumes, and I did notice similar behavior during development, but now I can't reproduce it? It's a bit problematic because when I adjust volume, I also get a notification that volume has changed, which i should act on IF another controller is the source of it. Problem being, I don't know that. What I do is that I don't act on the notification for a set time (800ms) after my own adjustment, to compensate for it. However, this isn't rock solid and might need tweaking.

In which scenario do you experience it? mouse wheel, click or drag? And is it master volume or the player volumes that is worst?
Userlevel 4
Badge +14
I fixed the queue and favorites-update. It now reflects changes from other controllers, and it will switch queue when changing zones.

Favorites will also be updated as soon as some other controller adds one.

If you don't want to muck around with git, I did a pre-zipped version with all modules. Just unzip and run it with node.

http://upload.grabbarna.se/files/node-sonos-web-controller-0.5.1.zip

I also did a new screen cap to reflect the current state: https://www.youtube.com/watch?v=_7Dke5LsTF0

Today I also installed this at work, which will be the main control interface for my co-workers. Will also use a screen-computer which runs chrome in kiosk mode, to present the currently playing track (today it just shows this interface, but I will make a "TV" interface for it which is only for information. Much like my Dashboard application I wrote in .NET, but which will run fine under a lightweight linux installation with X.
Userlevel 2
Good news its running (my fault forgot to change port) but will post error log as there are a few popping up

Thank You....

Robert
Userlevel 4
Badge +14
Well, of course it would break totally if you have an empty queue. Since 4.2 I rarely have empty queues any more, so I never noticed this. Anyway, I committed a fix for it. Thanks for the report.
Userlevel 2
I can confirm that artwork issue has now been fixed :)

I've written a blog entry about this project which may help some people not too familar with git/node.js.

http://danonit.blogspot.com.au/2013/11/web-api-for-controlling-sonos-system.html

I'm running the web controller on my NAS (Drobo-FS) and it's working a treat for basic control.
Userlevel 2
Empty queue is pretty common if you move your sonos around the house. Thanks for the fix.

I still do get the occasional null value error as well which crashes nodejs. Likely due to malformed or empty ID tags on mp3's. Have these issues been addressed yet? If to the best of your knowledge they have been addressed, I will look into more closely where exaclty I'm getting the null value.
Thanks, yvon
Userlevel 2
Multiple nodes on one machine can you run both versions of your server on the same windows pc any I trying the web based and the html they seems to hang during init if one is running and thoughts? Is it better to run a Linux box and then run both servers? If so what flavor of Linux would be best?

Many thanks and what a great project ........super job!
Userlevel 4
Badge +14
Empty queue is pretty common if you move your sonos around the house. Thanks for the fix.

I still do get the occasional null value error as well which crashes nodejs. Likely due to malformed or empty ID tags on mp3's. Have these issues been addressed yet? If to the best of your knowledge they have been addressed, I will look into more closely where exaclty I'm getting the null value.
Thanks, yvon


Yeah, I didn't mean to sound rude, I was more being rude to myself for not testing it :)

I thought the most of the null fails was adressed, but if you can give me error logs I will see what I can do.
Userlevel 4
Badge +14
Multiple nodes on one machine can you run both versions of your server on the same windows pc any I trying the web based and the html they seems to hang during init if one is running and thoughts? Is it better to run a Linux box and then run both servers? If so what flavor of Linux would be best?

Many thanks and what a great project ........super job!


Do you mean that you want to run both the http-api and the web-controller on the same machine? Unfortunately at the moment, the use a fixed port for notifications, which means that they don't start. You can combine them though, if you want, and use the same underlying sonos-discovery, which would save memory, but that's a bit of a hack at the moment.

I might add some autodiscovery for the listening port to make it less error prone.
Userlevel 2
Yes it would be very cool to have both running as I am trying to integrate it with my home automation system and both are just what I have been looking for .......:D Thank You
Userlevel 4
Badge +14
I changed the port behavior for discovery and notifcations to support multiple instances, which means that:

for ssdp it will randomize a port between 1902 and 2702 (it was impossible in node to identify occupied ports). For notification it will try and bind to port 3500 (like the android client), but if that one is taken it will try 3501, 3502 etc. This is usually not a problem for you, unless you have a segmented network and need to handle firewall rules.

I also added support for settings.json for sonos-web-controller, to let you configure listening port (for web GUI) and cache dir without changing the source. That way it will live through an update.

I have also verified that this runs on the raspberry pi. It has to work for it a bit, but it's not overwhelming.
Userlevel 2
Unfortunately with the latest update, the empty queue error has returned.
Userlevel 2
Favorites in the html server....

Does it support radio Fav Like Pandora etc or am I doing something wrong?

Ordered a raspberry pi to run this very cool it really will make a amazing package for sonos integration.

Thank You Robert
Userlevel 4
Badge +14
Okay, I added some checks when trying to parse the XML, seems like it act up occasionally, I don't know why yet.

And seems like I had forgotten to commit the empty queue fixes, my bad. They should be up in the git repo now. Make sure that you are using v 0.7.4 of the sonos-discovery module (check packages.json in the module folder), if not, delete it and run npm install again.
Userlevel 4
Badge +14
Does it support radio Fav Like Pandora etc or am I doing something wrong?

Ordered a raspberry pi to run this very cool it really will make a amazing package for sonos integration.


Hi, it is supposed to support Pandora, however I have no way of testing it (we don't have pandora here in Sweden). If you are using the http-api, you could send me the /favorites output so that I can see what the pandora items looks like.

Regarding the raspberry, I tested it with the B version (the one with 512 MB RAM and ethernet). I suggest you buy that one, at least for the ethernet port (you can just jack it into any ZonePlayer).
Userlevel 2
How nice would it be if this code (once completed) ultimately resided in the Sonos bridge or one of the players themselves. A fully native web based controller that would be good. No issues of operating systems, client apps, etc..

Just a thought.
Userlevel 4
Badge +14
How nice would it be if this code (once completed) ultimately resided in the Sonos bridge or one of the players themselves. A fully native web based controller that would be good. No issues of operating systems, client apps, etc..

Just a thought.


I don't see that happening though, I'm afraid. I saw that someone suggested this over at ask.sonos.com, https://ask.sonos.com/sonos/topics/bridge_web_server_to_control_the_system_extend_the_memory_limit

But in my experience, this is not something that would benefit the majority of the users, which means that it isn't likely to be produced.

Either way, a raspberry pi is very inexpensive (even cheaper than a bridge), and it would even be possible to make a preinstalled image for it (with node and stuff already installed), to flash on an SD-card. And if I had a 3D printer I would build a small Sonos-like casing for it as well 🙂
Userlevel 4
Badge +14
If you have a player that is currently playing a queue, and you start playing that pandora radio, will the queue remain on that player and say (not in use)?