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 4
Badge +14
A bit off topic but how does one run your code as a server on a raspberry as a service every time i redirect node server.js > robert.txt i do not return to the bash so i can run the other script .....thanks

It does run though so the install is correct.....But have no clue what I am doing yet but learning....

Thanks


The best option that I have found is to use pm2 (https://github.com/Unitech/pm2)

Basically, what you do is (as root):

code:

npm -g install pm2
pm2 startup
cd /your/app/folder
pm2 start server.js


This will persist through reboot and such because "pm2 startup" installed the necessary init scripts for it.

pm2 will revive running processes at reboot, but not stopped processes. This means that if the process crashed, then a reboot won't fix it.
Badge
I've been looking through the thread, but can't seem to see if you have a webpage or a file outlining instructions on how to set all this up from scratch?

I have a 2008 R2 server running as a NAS to host, but can probably set up a VM running Linux if required.
Userlevel 4
Badge +14
I've been looking through the thread, but can't seem to see if you have a webpage or a file outlining instructions on how to set all this up from scratch?

I have a 2008 R2 server running as a NAS to host, but can probably set up a VM running Linux if required.


I haven't really made a how-to yet since I haven't packaged it for distribution. But the short version is:

Install node.js, there is an installer for windows (http://nodejs.org/download/)

Download the repo that you want, there is a downloadable zip at each repo (a button to the right, says "Download ZIP"). for the web controller, this is https://github.com/jishi/node-sonos-web-controller/archive/master.zip

unzip it to a folder, invoke in that folder(from cmd or powershell):

npm install
node server.js
Userlevel 2
looks like all versions of node lead to the same issue i tried versions 0.8 and 0.10.

Can I define the sonos bridge, server interface IP(1 only) or even a list of networks to scan in the settings.json file and have it passed on instead?

Just a thought.
Userlevel 4
Badge +14
looks like all versions of node lead to the same issue i tried versions 0.8 and 0.10.

Can I define the sonos bridge, server interface IP(1 only) or even a list of networks to scan in the settings.json file and have it passed on instead?

Just a thought.


That's not necessary. I can make a fallback to the old behavior if no interfaces are found, that should resolve your issue.

I should file a bug ticket for the freebsd version as well, they might wanna fix that.
Userlevel 4
Badge +14
Okay, ynot, I pushed a fix for freeBSD.

Could you please also test this (relevant for a bug report):

code:

var os = require('os');
console.log(os.networkInterfaces());


Add that to a js file (test.js or similar) and invoke it with "node test.js", and send me the output? I want to see if it actually finds any interfaces, or if they are incorrectly labeled as "internal". It would be nice if I didn't have to install a VM with freeBSD just to test this 🙂
Userlevel 2
The fall back works well. Thank you.

As suspected, FreeBsd 91.p5 returns the following output:

{}

when interrogating for the network interface list

Thanks again. Yvon.
Userlevel 4
Badge +14
Did some updates to the web-controller, it now has working player mute buttons, and you can seek in tracks by dragging the progress-bar.

Will add mousewheel/click to it later on.

Enjoy!
Userlevel 2
Yes, this is because you are running node 0.6.x. You need 0.8 or higher, and you might need to install it manually of it isn't in the ubuntu repos.

Now i got it. I have to upgrade "node" itself. Sorry.

Will do it tomorrow.

Sacha
Userlevel 2
Now i got it. I have to upgrade "node" itself. Sorry.

Will do it tomorrow.

Sacha


Hi jishi,

Works perfect. Well done. Very usefull.

Sacha
Userlevel 2
If I might ask, what are the future plans for this controller. Do you plan to support local music libraries, query tools, add / remove tracks form queue, etc. similar to the native controller.

Are you also planning to support other browsers and in particular mobile browsers.

This is a very nice tool and it seems to perform well and is quite flexible. Good job.
Userlevel 4
Badge +14
If I might ask, what are the future plans for this controller. Do you plan to support local music libraries, query tools, add / remove tracks form queue, etc. similar to the native controller.

Are you also planning to support other browsers and in particular mobile browsers.

This is a very nice tool and it seems to perform well and is quite flexible. Good job.


Well, my main goal was to implement the basic functionality for desktop browsers. That is pretty much there now, because I settled for only handling favorites to begin with. When I have made the final tweaks I will package it as version 1.0. I will not support additional browsers for the desktop version for the time being. Firefox and Chrome are available on all desktop/laptop platforms, including chromebook, so I consider that a waste.

Phase two was to implement a spotify-centric version of the web controller, that would use the spotify search engine and behave more like the spotify client when it comes to functionality. Mostly because I more or less use Sonos exclusively for Spotify streaming.

Some other loose ideas is party lockdown mode with a basic web controller for smartphones (only queue), a better mobile controller for all modern mobile platforms etc. When the basic functionality is there, all those other ideas wouldn't be that hard to implement.

Also note that the web controller functions has been prioritized based on my own usage pattern. For example, I never edit my queue, so that hasn't been a priority for me. If enough people think that it would be useful, I might prioritize that function. But the main focus will be to make it useful for as many as possible.

And lastly, I have done this in my spare time, by myself, and I don't have an infinite amount of time 😉. It has been fun and quite a challenge, but every now and then I need to rest as well. If people want to help out, I'll gladly accept pull requests.
Userlevel 2
Thanks for the info. I understand the magnitude of the tasks and the time it takes and certainly do appreciate the effort.

I will consider the pull request if I can wrap my head around the existing code and comm techniques early in the new year. I would like to be able to access my local music library and have search and queue up capabilities similar to those in the official controller as well as the old purple controller.

Thanks again. Yvon.
Userlevel 2
Hi Jishi,
On one of my Raspberry Pi running wheezy-raspbian I have installed NodeJS and the "Hello World" example is working. But I can't understand how to install the LIRC driver. The problem are due to my limited Linux skills. The IR code and Electronics isn't no problem for me.

So I plan to use FLIRC (emulates keyboards keys then IR is recivied) and wonder how difficult it's to change the Node code (sonos-remote-control) to act on some key pressed instead and were to start?

I'm a windows programmer but are not used with Java and WEB interface.
Userlevel 2
Hi Jishi,

I´m also interessted in the queue capability. So that we are able to queue an uri. I currectly write a module for "fhem" a opensource homeautomation system to support your node. Let you know about the progress.

Best regards

Sacha
Userlevel 4
Badge +14
Hi Jishi,
On one of my Raspberry Pi running wheezy-raspbian I have installed NodeJS and the "Hello World" example is working. But I can't understand how to install the LIRC driver. The problem are due to my limited Linux skills. The IR code and Electronics isn't no problem for me.

So I plan to use FLIRC (emulates keyboards keys then IR is recivied) and wonder how difficult it's to change the Node code (sonos-remote-control) to act on some key pressed instead and were to start?

I'm a windows programmer but are not used with Java and WEB interface.


I did a version that reads out keystrokes instead of data from the lirc socket. That could be used for flirc instead. However, you still need to program your flirc, and that requires a graphical UI as I understand it. I don't think it will be that much easier. Do you already have a flirc receiver or are you planning on buying it? If the keyboard mapping is stored in the dongle, then I can see the simplicity of it. Then you can program it on your laptop/desktop and then plug it into the rpi, is that the case?
Userlevel 2
I did a version that reads out keystrokes instead of data from the lirc socket. That could be used for flirc instead. However, you still need to program your flirc, and that requires a graphical UI as I understand it. I don't think it will be that much easier. Do you already have a flirc receiver or are you planning on buying it? If the keyboard mapping is stored in the dongle, then I can see the simplicity of it. Then you can program it on your laptop/desktop and then plug it into the rpi, is that the case?

Yes the flirc could (and should) esy be setup for any remote in Windows and then moved to the target platform, in this case the rpi.
http://flirc.tv/ I have ordered it but not got it yet. https://www.m.nu/flirc-universal-ir-remote-receiver-p-1116.html

Were could I find your early rpi version with keystroke instead of IR on the GIT hub?

Citate from this link http://thepihut.com/pages/flirc-a-review-of-the-ultimate-media-centre-companion

The key is that FLIRC reports itself to the operating system as a standard USB Human Interface Device (HID), so any modern OS will be able to read it as if it was a keyboard. Even if the device or OS you want to use FLIRC on has no included support for remote controls, you can program FLIRC to “press” keys that the system will understand. For example, I was able to program FLIRC to interpret the directional pad on my remote to standard keyboard arrow keys; I then plugged FLIRC into my Android device and was able to navigate through the applications with my remote.

This allows for incredible flexibility. Being able to program FLIRC from a different device than what you end up using it on and the fact that it emulates a USB keyboard means you can use FLIRC where other products would be useless.
Userlevel 4
Badge +14
No, I don't have that code in github. I will have to dig through my home laptop or the rpi to see if I still have it. It was merely a prototype and I could reproduce it once again probably.

See here:

http://forums.sonos.com/showpost.php?p=192751&postcount=13

And here (my question regarding it, a bit technical):

http://stackoverflow.com/questions/15742172/would-it-be-possible-to-read-out-physical-keyboard-strokes-in-node-js
Userlevel 2
No, I don't have that code in github. I will have to dig through my home laptop or the rpi to see if I still have it. It was merely a prototype and I could reproduce it once again probably.

See here:

http://forums.sonos.com/showpost.php?p=192751&postcount=13

And here (my question regarding it, a bit technical):

http://stackoverflow.com/questions/15742172/would-it-be-possible-to-read-out-physical-keyboard-strokes-in-node-js


Thanks Jishi,
I will do some test with the std rpi keyboard first before adding in the flirc. I supose it will work fine to run the remote node code on windows first just for me to learn how the code is working (after removing the lirc code)?
Userlevel 4
Badge +14
Thanks Jishi,
I will do some test with the std rpi keyboard first before adding in the flirc. I supose it will work fine to run the remote node code on windows first just for me to learn how the code is working (after removing the lirc code)?


Unfortunately, reading out keystrokes is highly linux-specific, so you won't be able to test that. This is because you want it to run in the background, and not be dependent on an active console session. It would be possible to enable auto login and a bash startup script but I don't think it would be a nice solution...
Userlevel 2
Unfortunately, reading out keystrokes is highly linux-specific, so you won't be able to test that. This is because you want it to run in the background, and not be dependent on an active console session. It would be possible to enable auto login and a bash startup script but I don't think it would be a nice solution...

Ok Thanks, I understand the difference between keyboard reading in Linux and Windows. The question was more if the rest of the remote code would execute in windows so I could do some tests by changing code and so on just to learn how it works. I assume the only code to deal with is sonos-lirc.js ?
Userlevel 4
Badge +14
Ok Thanks, I understand the difference between keyboard reading in Linux and Windows. The question was more if the rest of the remote code would execute in windows so I could do some tests by changing code and so on just to learn how it works. I assume the only code to deal with is sonos-lirc.js ?

Yes it would. I use Windows as my development platform so that isn't a problem. Node.js is fairly platform agnostic.
Userlevel 2
Jimmy:

Happy Holiday to you and yours and thank you for a amazing project......

Regards

Robert
Userlevel 4
Badge +14
Jimmy:

Happy Holiday to you and yours and thank you for a amazing project......

Regards

Robert


Thanks, I will be silent for 3 weeks but don't be alarmed, I haven't abandoned it, yet. :)

Happy holidays!
Userlevel 2
Hello,

i like install it on windwos 7 system.

- i have download the node and web zip files.
- i have installed the node this will be fine
- the zip file i have unzipped to a folder
- in cmd i have "npm install" but then i get this logfile

0 info it worked if it ends with ok
1 verbose cli [ 'C:\\Program Files (x86)\\nodejs\\\\node.exe',
1 verbose cli 'C:\\Program Files (x86)\\nodejs\\node_modules\\npm\\bin\\npm-cli.js',
1 verbose cli 'install' ]
2 info using npm@1.3.21
3 info using node@v0.10.24
4 verbose node symlink C:\Program Files (x86)\nodejs\\node.exe
5 error install Couldn't read dependencies
6 error package.json ENOENT, open 'C:\Program Files (x86)\nodejs\package.json'
6 error package.json This is most likely not a problem with npm itself.
6 error package.json npm can't find a package.json file in your current directory.
7 error System Windows_NT 6.1.7601
8 error command "C:\\Program Files (x86)\\nodejs\\\\node.exe" "C:\\Program Files (x86)\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "install"
9 error cwd C:\Program Files (x86)\nodejs
10 error node -v v0.10.24
11 error npm -v 1.3.21
12 error path C:\Program Files (x86)\nodejs\package.json
13 error code ENOPACKAGEJSON
14 error errno 34
15 verbose exit [ 34, true ]

Can somenoe help me what the error is, what can i do? And how can i install the web controller?

Thanks best regards Thomas