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

    jishiAuthor
    Headliner II
    October 12, 2013
    I'm well aware that clock speed is only part of the story, but at 180MHz I wonder if it is fast enough for large JavaScript programs.


    Yes, that part worries me too. The raspberry at 700 works pretty good but it can still choke when it has a lot to deal with.

    However, it is running a full blown Debian setup, and could probably be stripped down quite a bit. Would be interesting to see how well it would behave.
    jishiAuthor
    Headliner II
    October 15, 2013
    Now, as part of the 4.2 upgrade, the players now remember their queue and queue position it had prior to being grouped with other players.

    This is a good thing, since that gives you the possibility to use different coordinators for different presets, and that way, you will be able to switch presets and keep the old queue, if each preset has a unique coordinator.
    Lyricist I
    October 19, 2013
    Yes, you are running 0.6 of node.js, they changed it in 0.7 from "path" to "fs". I haven't run it under 0.6 in a long time, and I focused on 0.8 when I started. It might work, you can try and change this row (however, you might get other problems down the line):

    code:
    var fs = require('fs');


    to

    code:
    var fs = require('path');


    I assume you installed it using apt-get under debian/ubuntu, correct? If you don't want to compile and install it yourself, you can check this out: http://ypcs.fi/howto/2013/03/23/nodejs-debian/


    Thanks! I updated to a newer nodejs, and now it works!

    Looking forward to experimenting with this :)

    Andreas
    jishiAuthor
    Headliner II
    October 20, 2013
    Thank you for your pointers, some bugs that was introduced in my last commits.

    I have fixed them and pushed to github now. Enjoy!
    Lyricist III
    October 21, 2013
    Muting is working, thanks!
    A longer message is awaiting approval...
    jishiAuthor
    Headliner II
    October 21, 2013
    Seems like radio favorites needed to be handled differently than queue favorites. I added simple fix for it, but it might not work for custom radios (TuneIn should be fine though).

    I also identified a few bugs in the preset system that I introduced in a rewrite, should work correctly now again.

    "npm install" doesn't seem to update dependencies from git repos, so it's usually best to remove the node_modules folder and invoke "npm install" in order to get the latest version of sonos-discovery module.

    Let me know if you have any more problems and I will try and sort it out.
    jishiAuthor
    Headliner II
    October 21, 2013
    For those who are interested, I pushed a new repository with a proof of concept for IR remote controlling your Sonos using a raspberry pi and a simple IR receiver.

    https://github.com/jishi/node-sonos-remote-control

    I use this myself with a left over IR remote, that controls the volume for my living room, and I've mapped the number keys to different presets and the media keys for play/pause/prev/next. Very neat.
    jishiAuthor
    Headliner II
    October 22, 2013

    adding player to coordinator Bathroom with streamurl x-rincon:RINCON_00000000000000000


    I added some entity encoding stuff to support the favorites, I guess there are some scenarios where this will pass null that I need to take into account.

    However, that streamurl points to a device with no MAC-adress, which is kind of odd? It should never be all zeros like that.
    jishiAuthor
    Headliner II
    October 22, 2013

    On my wish list:
    - Queue management, add/remove/clear spotify tracks and/or playlists.
    - Control shuffle/repeat/crossfade
    - No autoplay when changing presets

    My goal is to have a simple mobile GUI with some presets buttons;
    Morning - Group all players, set volumes, start radio.
    Evening - Group some players, set volumes, set shuffle, set repeat, set crossfade, replace queue with spotify playlist.
    Party - Same as above but different playlist and maybe moving items in queue to play select item as next song.
    Mute - Muting or changing volume in selected rooms.
    Stop all - Stop all players with a single click (useful when leaving the home).

    I've tried some of the unsupported APIs and this is the best so far, keep up the good job!


    Control shuffle/repeat etc is next on my todo.
    You can disable Auto-play on the preset by adding the following to your preset:

    code:

    { "state": "stopped",
    players:
    [ { roomName: 'Bathroom', volume: 11 },
    { roomName: 'Kitchen', volume: 11 },
    { roomName: 'Master Bedroom', volume: 11 },
    { roomName: 'Living Room', volume: 10 } ] }


    It's not part of my example, but should be.
    Party mode is also an idea I've had for a long time. I started a .NET based app for it, but I never finished it. I will probably do a web-based version instead based on this API, that handles multiple clients (think, smartphone control).

    You might want to check out the web controller-project that I started in your case, since the http-api is just a wrapper around sonos-discovery. If you plan on hosting a web client, you should probably skip that layer. https://github.com/jishi/node-sonos-web-controller

    The web controller isn't working, but you might get the idea on how you can create and host the web client within node completely.

    Oh, and finally, I did a commit to fix the null-check as well, but I'm not sure that it will resolve your issue completely. Let me know what kind of error you receive. Oh, I just struck me that you probably anonymized the streamurl, DOH! 🙂
    Lyricist III
    October 22, 2013
    Thanks, yes I removed my real Mac-address... ;)

    Will continue my testing tonight. The Web-controller looks very cool but I think it might be a bit heavy weight for me. Just want to have a few predefined buttons that should trigger some custom actions like your new IR remote project.

    Looks like I can get Morning, Mute and Stop All buttons running quite easily now.