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
Thank You .....!;)
Userlevel 2
Will give it try could it be names with a space I know that if you have a space in fav name it fails...
Thanks..
Userlevel 4
Badge +14
Will give it try could it be names with a space I know that if you have a space in fav name it fails...
Thanks..


It shouldn't, are you sure about that? I have a few favorites with spaces and never had a problem.
Userlevel 2
I will double check and get back after i update the files but no all the rooms are correct but they do have spaces....will test and forward logs thank you again
Userlevel 4
Badge +14

I have one SONOS PlayBar and it's no prblem to manage music/radio on it but I also would like to be able to switch to the TV sound (optical SPDIF).

In org SONOS i select TV input as a source "TV" the same way I do for Spotify, Rado etc.

Any clue?


Unfortunately you can't add a line-in as a favorite, and listing music library and line-ins aren't the priority for now. Great info though, since I don't have a playbar myself, wasn't aware of the different URI format for spdif line in.

I will probably finish presets before I start with music browsing, and in that case you will be able to specify "x-sonos-htastream:RINCON_000E5XXXXXXX01400:spdif" as a preset uri (you could be able to bind this to an IR remote if you get a IR receiver to your raspberry).

Doesn't the playbar have an auto line-in feature like all the other players though?
Userlevel 2

Doesn't the playbar have an auto line-in feature like all the other players though?


Thanks for your quick answer.
Yes the PlayBar could be configured to autoswitch to SPDIF then the TV will output signal on the SPDIF.

My current solution is to switch the TV off and on. Also becuse it's a smart TV (not so smart att all) I like to control my music on the PlayBar from the TV browser.

For sure I will use the Raspberry Pi IR functions.
/Palle
Userlevel 4
Badge +14
Thanks for your quick answer.
Yes the PlayBar could be configured to autoswitch to SPDIF then the TV will output signal on the SPDIF.

My current solution is to switch the TV off and on. Also becuse it's a smart TV (not so smart att all) I like to control my music on the PlayBar from the TV browser.

For sure I will use the Raspberry Pi IR functions.
/Palle


did the TV Browser work with this GUI?
Userlevel 2
did the TV Browser work with this GUI?

Yes for sure. It's a LG midrange smart TV launced this year 2013, I think it use some derivate of Chrome. At startup i looks real nice, but after a while some texts is overlayed but all functions works fine.
[edit] Checked and the scrollbars won't work, but the whole screen will scroll instead. Then Track, Artist, Album strings gets to long they will not do CR/LF instead they will move under the album picture and owerlaying the QUEUE text information.

I found if I don't display the "mini TV frame" in the smart TV browser the TV sound will automatically return then i quit the smart TV browser and start watching TV. Works perfect for me!
Userlevel 2
Well my hats is off to you sir looking very good and As per the space thing I guess I was to blame will bang it more and let u know whats up but first blush and we are smiling Nice job
Userlevel 2
OK so how would one create a preset to u group a group playing and stop all.....Thanks
Userlevel 4
Badge +14
OK so how would one create a preset to u group a group playing and stop all.....Thanks

Ungroup all players? That's not possible with one preset today, you need to create one preset for each player.

If you just want to stop all of them, you can just send a pause signal for any player in the group (I assume you are talking about the HTTP API now)
Userlevel 2
Yes and thanks that will work for now...
Userlevel 4
Badge +14
Yes for sure. It's a LG midrange smart TV launced this year 2013, I think it use some derivate of Chrome. At startup i looks real nice, but after a while some texts is overlayed but all functions works fine.
[edit] Checked and the scrollbars won't work, but the whole screen will scroll instead. Then Track, Artist, Album strings gets to long they will not do CR/LF instead they will move under the album picture and owerlaying the QUEUE text information.

I found if I don't display the "mini TV frame" in the smart TV browser the TV sound will automatically return then i quit the smart TV browser and start watching TV. Works perfect for me!


Yeah, I've heard that modern LG and Samsung is using a version of WebKit, but not sure which one. WebKit do however have some issues with my design because of the expected fixed window size. But for a TV, it would be better to create another UI which suits the format better, and which might be possible to control using the D-pad on the remote and similar. Unfortunately I don't have a smart-TV myself so I can't do any real testing. Next step is to try and create a mobile web UI which will be totally different than the official app, assuming that you can use the touch in an intuitive manner from a phone browser.
New build of the web server seems to be working well.... Updated this afternoon, and just forced the replacement of the sonos-discovery....

Now onto the http-api.

Did a fresh git pull at the same time....

Now, when I load it... I'm getting this:

code:

binding SSDP to port 2594
scanning for players...
port in use 3500 trying new one
notification server listening on port 3501

module.js:485
throw err;
^
SyntaxError:[path]/node-sonos-http-api/presets.json: Unexpected string
at Object.parse (native)
at Object.Module._extensions..json (module.js:482:27)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at [path]/node-sonos-http-api/server.js:12:13
at Object.cb [as oncomplete] (fs.js:168:19)


I figure I'm screwing up the presets.json, but I haven't quite figured how.

I took your sample, and removed all references to your players, and replaced with mine....

code:

{
"all": {"players": [{ "roomName": "LittleRm", "volume": 23 },{ "roomName": "Living Room", "volume": 27 },{ "roomName": "Kitchen", "volume": 18 },{ "roomName": "Office", "volume": 22 },{ "roomName": "Master Bedroom", "volume": 17 },{ "roomName": "Rec Room", "volume": 0 }]},
"tv": {
"players": [{ "roomName": "Rec Room", "volume": 50 }],
"state": "stopped",
"uri": "x-rincon-stream:RINCON_0000000000001400"
},
"AcidJazz": {
"players": [{ "roomName": "Living Room", "volume": 27 },{ "roomName": "Kitchen", "volume": 18 },{ "roomName": "Office", "volume": 22 }],
"favorite": "Acid Jazz Radio"
}
"Thiev": {
"players": [{ "roomName": "Living Room", "volume": 27 },{ "roomName": "Kitchen", "volume": 18 },{ "roomName": "Office", "volume": 22 }],
"favorite": "Thievery Corporation Rdio Radio"
}
}


I'm going to go back a few pages in the thread, and try that "test your file" link you posted J, and see if there's anything it complains about....
Userlevel 2

code:

{
"all": {"players": [{ "roomName": "LittleRm", "volume": 23 },{ "roomName": "Living Room", "volume": 27 },{ "roomName": "Kitchen", "volume": 18 },{ "roomName": "Office", "volume": 22 },{ "roomName": "Master Bedroom", "volume": 17 },{ "roomName": "Rec Room", "volume": 0 }]},
"tv": {
"players": [{ "roomName": "Rec Room", "volume": 50 }],
"state": "stopped",
"uri": "x-rincon-stream:RINCON_0000000000001400"
},
"AcidJazz": {
"players": [{ "roomName": "Living Room", "volume": 27 },{ "roomName": "Kitchen", "volume": 18 },{ "roomName": "Office", "volume": 22 }],
"favorite": "Acid Jazz Radio"
},
"Thiev": {
"players": [{ "roomName": "Living Room", "volume": 27 },{ "roomName": "Kitchen", "volume": 18 },{ "roomName": "Office", "volume": 22 }],
"favorite": "Thievery Corporation Rdio Radio"
}
}




Hi You have missed the "," after ""Acid Jazz Radio" } " I added it in the code above.
Userlevel 2
Ok been testing presets in httpsonos and I believe we have a bug if error in preset file misspellings etc not in syntax the server needs to be rebooted other wise it will fail to read the next request for preset I see no errors in the log .but will keep looking.
Hi You have missed the "," after ""Acid Jazz Radio" } " I added it in the code above.

Thanks... I tried the "," after both new presets, didn't realize that you need to not have it after the last one.....

OK, so now the server is starting....

Connecting through a browser and using:

:5005/presets/Thiev/Play

Output on the screen:

code:

{ room: 'presets', action: 'AcidJazz', value: 'play' }
{ room: 'presets', action: 'Thiev', value: 'Play' }


But it's not doing anything to the players....

If I do the

code:

:5005/office/favorite/thievery corporation rdio radio


It was starting the stream.... with the current grouping. I was also able to stop it.

Thoughts?
Userlevel 4
Badge +14
Thanks... I tried the "," after both new presets, didn't realize that you need to not have it after the last one.....

OK, so now the server is starting....

Connecting through a browser and using:

:5005/presets/Thiev/Play

Output on yeah,I think screen:

code:

{ room: 'presets', action: 'AcidJazz', value: 'play' }
{ room: 'presets', action: 'Thiev', value: 'Play' }


But it's not doing anything to the players....

If I do the

code:

:5005/office/favorite/thievery corporation rdio radio


It was starting the stream.... with the current grouping. I was also able to stop it.

Thoughts?


yaEah, I think you are sending in /play at the end of the URL,which yuou shouldn't, it confuses it.

only invoke /preset/thiev and you should be golden.
Userlevel 4
Badge +14
Just to make sure, there can be no end slash either, or it will interpret the command incorrectly.
Just to make sure, there can be no end slash either, or it will interpret the command incorrectly.

Check.... no trailing slash... unless chrome is "auto" adding....

Sample:

:5005/presets/test2
Userlevel 4
Badge +14
Check.... no trailing slash... unless chrome is "auto" adding....

Sample:

:5005/presets/test2


Well, it should be /preset/test2, it will parse it errorneous if you call it "presets", unfortunately.
Userlevel 2
Player playing display.....I saw you created a page to display what is playing etc could you or would you send it our way to play with many thanks

Robert

PS rasberry on its way oh boy more confusion on my life thanks again really enjoying your stuff and learning slow but learning
Userlevel 4
Badge +14
Player playing display.....I saw you created a page to display what is playing etc could you or would you send it our way to play with many thanks

Robert

PS rasberry on its way oh boy more confusion on my life thanks again really enjoying your stuff and learning slow but learning


Well, I can't really distribute it as is since it is both only Spotify centric (only works for spotify tracks because I download high-res covers from their web services), and because the font used is copyrighted. I will need some work to adjust this and have like a last.fm fallback for the cover arts or something.
Userlevel 2
No worry could you just create a simple example showing how to display in simple formatted

the return from the http call ip/zone/state I am trying but getting no where fast .... thank you Robert
Userlevel 4
Badge +14
I can't reproduce that error. What is Office playing when you try and request the state? What does http://192.168.1.50:1400/status/perf say as current track?