Sonos with node.js, my attempt!



Show first post
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 4
Badge +14
Okay, here is the first draft.

https://github.com/jishi/node-sonos-http-api

if you don't want to muck around with git, I have a zip-file which you can use:

http://upload.grabbarna.se/files/sonos-http-api.zip

unzip it to a folder, then just invoke from a command prompt or powershell or bash or whatever while standing in the folder:

code:

node server.js


I don't handle topology yet, had some issues with timing and haven't had time to look into it.

I have only tested it in node 0.8.16, should work fine in 0.8.x but unsure about earlier versions. It works on a raspberry pi, and should be okay on any nodejs supported platform.

Feedback is welcome, forking and patches as well.

Have fun!
Userlevel 4
Badge +14

It would be nice if you can implement the trackseek in the http-api.


I pushed an update to the git repo with "trackseek" implemented. It will take total seconds that you want to seek into (for 2:00):

/Office/trackseek/120
Userlevel 4
Badge +14
Here is an update.

I have managed to implement the topology stuff now as well, which basically means that you are able to send commands to any player, and it will control the coordinator instead.

This means that if:

Kitchen and Office is grouped, command to either player will control them both (except for volume). This is probably much more handy.

Posted a pre-zipped package here:

http://upload.grabbarna.se/files/sonos-http-api-v2.zip

And also pushed the latest version to github.
Userlevel 4
Badge +14
For those interested in the progress:

http://upload.grabbarna.se/files/sonos-web-controller-draft-3.png

Man, does it takes forever to replicate the GUI in HTML...
Userlevel 2
I pushed an update to the git repo with "trackseek" implemented. It will take total seconds that you want to seek into (for 2:00):

/Office/trackseek/120



nice thx
Userlevel 4
Badge +14
Finally pushed the web controller to a public repository:

https://github.com/jishi/node-sonos-web-controller

Nothing is actually working (except for zone discovery), but if you want to try it out to get a feeling on how it will behave in the browser, please feel free to do so. You will also be able to follow my commit logs :)

If you want to help out, give me a shout. The main reason for me doing this, is because I want to expose a working controller for all my co-workers. Being behind firewalls and on different nets, it would simplify it a lot if I could just expose a web-based controller for everyone to use. It could also be useful for controlling a system remotely.
Userlevel 4
Badge +14
Hi guys.

I remember reading somewhere that somebody wanted the LED state of the players to be on when something was playing, and off when the player was paused/stopped.

Realized that isn't such a hard thing to accomplish, so I did a quick function for it.

Doing something like this:

code:

var SonosDiscovery = require('sonos-discovery');
var discovery = new SonosDiscovery();
discovery.setToggleLED(true);


Unfortunately there is a bit of delay (1s or something). I think someone wanted it for a light sensor that would power on/off an amplifier. I thought it was a fun feature anyway, even if nobody finds it useful 🙂
Userlevel 2
Looking really good.....will try it out thnak you for sharing..

Regards

Robert
Userlevel 2
Jishi,
first of all - a great piece of work.

I have spent the whole day installing it (both web controller and API) on my RASPI and in the final step come across a problem when trying to run it in the background.

I have tried using both FOREVER and PM2 to start the processes during boot time. This it does quite happily with no errors shown in the log files.

The problem that I observe however is that the web client wont display in a browser if the process is started with FOREVER (or PM2 when the -x option is used).

PM2 works if the mode is cluster - however this then eats 100% CPU.

The log file contents are identical when starting in the background or when starting from a terminal window. It indicates no errors or warnings.

The browser doesn't complain that the website is not there - it just sits and waits and displays nothing.

Anyone have any ideas?

I'm using the latest versions of everything (node 0.10.2) and the sonos-web-controller_0.6.0_pi build.

Peter
Userlevel 4
Badge +14
I managed to map one of my airmouse volume buttons to control a specific player with my raspberry pi without any problems.

It read out the events on /dev/input/event0 (the keyboard) and maps the appropriate keycode to invoke the setVolume() method, this was with approx 20 lines of js.

It works if run as a daemon as well, I started node.js from a ssh session and controlled it with a USB attached keyboard. Main problem is that the Airmouse operates in the 2.4 GHz span so it seems to drop about 5% of the keystrokes... it's a crappy china-keyboard as well, so that might explain it.
Userlevel 4
Badge +14
Jishi,
first of all - a great piece of work.

I have spent the whole day installing it (both web controller and API) on my RASPI and in the final step come across a problem when trying to run it in the background.

I have tried using both FOREVER and PM2 to start the processes during boot time. This it does quite happily with no errors shown in the log files.

The problem that I observe however is that the web client wont display in a browser if the process is started with FOREVER (or PM2 when the -x option is used).

PM2 works if the mode is cluster - however this then eats 100% CPU.

The log file contents are identical when starting in the background or when starting from a terminal window. It indicates no errors or warnings.

The browser doesn't complain that the website is not there - it just sits and waits and displays nothing.

Anyone have any ideas?

I'm using the latest versions of everything (node 0.10.2) and the sonos-web-controller_0.6.0_pi build.

Peter


Hm, that is odd. I just tried it out and it works on my raspberry. I do however run it as root, which you might not.

What does the output from "netstat -ntpl" say? What OS are you running? Rasbian? Which port are you trying to run it under?

And, does it work, if you run it directly from the shell? Even as the same user that pm2 will run under?
Userlevel 2
Thanks and keep up the good work. I would love to see a quick how to of how to get this working with a RasPI.
Userlevel 2
so everything is running as root (I'm lazy 🙂 )

If I do not use PM2 or FOREVER and run directly from the terminal window in x-windows then it works.

Run under PM2 from the terminal window in clustered mode it works.

Run under PM2 from the terminal window in forked mode it doesn't work.

The api program works without problem. It's just the web controller that has issues. The web browser seems to be connecting but just not receiving data (it doesn't even timeout??) have tried with Chrome and Firefox.
Have even tried with only starting the web controller and leaving the api down - but I really want to use both.

OS is RASPIDAN

Port settings are all the defaults - I have not changed any of your settings


UPDATE::::::
Tried it again this morning and PM2 works!!!!
FOREVER still doesn't work but if PM2 is working I'm happy. The only thing that I can think of that I had changed in the mean time was to put the node and application paths into the PATH statement in the etc/.profile
Userlevel 4
Badge +14
Thanks and keep up the good work. I would love to see a quick how to of how to get this working with a RasPI.

I'll try and write something up. Is it node.js installation and how to start a node app that you are interested in?
Userlevel 4
Badge +14
Make sure that it is running in forked mode. If you started under cluster mode, then stopped it, and started it again with -x (without deleting it from pm2) it will still start in cluster mode.

Also, did you fix the startup script for pm2? (pm2 startup) That should fix environment variables, and then you should start pm2 using the init.d script.
Userlevel 2
Yep did all that :)

As I said, suddenly started working this morning.
Userlevel 4
Badge +14
Glad it worked out for you. It's strange that you would need to npm install all libs separately. The package.json file is supposed to keep track of the dependencies for you, making a "npm install" invoke from the same folder to trigger a dependency-check and download stuff that is missing globally. That was my experience when I tested it out, will have to check it out again.

Haven't had much time to work on it, but another guy has forked the project and is pushing minor improvements to it.

Regarding running it as a service, it is doable but requires some manual setup. It's easier on linux afaik, but it's possible on Windows as well.
Userlevel 4
Badge +14
One theory is that you started it in cluster mode once, which would have made the pm2 daemon hog the relevant ports. When you later on tried in forked mode or forever, it wouldn't bind to them. It should have raised an error though...
Userlevel 2
Hi there :

Do you think if i merged both lib http and web to one server there would be a conflict ?

Many Thanks

Regards

Robert
Userlevel 4
Badge +14
Hi there :

Do you think if i merged both lib http and web to one server there would be a conflict ?

Many Thanks

Regards

Robert


No, that is actually preferred. Use a single instance of SonosDiscovery. I do that my self.
Userlevel 4
Badge +14
Okay, finally got some time to work in this and I got around to implement support for presets.

A preset in this sense is a group of players with a predefined volume. This means that you can, by supplying a valid js object, send a signal to the players to group themselves, set the preferred volume and start playing whats in the queue.

A preset looks like this:

code:

{
"players": [
{ "roomName": "room1", "volume": 15},
{"roomName": "room2", "volume": 25}
]
}


The first player in the group will be assigned to coordinator role.

Next step will be to implement some playback options, like random, gapless, a favorite queue etc. It would also be of interest to keep track of which track it was actually playing last time, in order to resume where it left of.

Check it out here: https://github.com/jishi/node-sonos-http-api
Userlevel 2
Could you upload your version with it combined I am also using the remote control code. I keep blowing it up for some reason ........

:~(
Thank You
Robert
Userlevel 4
Badge +14
I made some improvements to the preset signals, previously it did spam a lot which confused the players and failed to add everyone to the group. I also identify if the coordinator already is a coordinator, and skips the BecomeCoordinatorOfStandaloneGroup call.

It also support predefined presets, which you can store in a file, which gives you the ability to define something like an "all" preset, that you can apply by invoking:

code:
http://localhost:5005/preset/all
Userlevel 4
Badge +14
Could you upload your version with it combined I am also using the remote control code. I keep blowing it up for some reason ........

:~(
Thank You
Robert


I don't run the IR remote and the web controller together, since they aren't modularized. Adding the http api is no problem, but I need to rework some stuff to make it easier to package it all together. Right now I'm doing a rewrite of all XML parsing to speed things up, but the plan is to create a complete distribution which contains all parts, which you can configure on/off instead. That would probably make it easier to install.
Userlevel 4
Badge +14
And today I read about this thing: http://tessel.io/