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

    Lyricist III
    January 5, 2014
    No worry Have a good vacation sorry to bother you let me know when your back ...;)
    jishiAuthor
    Headliner II
    January 5, 2014
    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


    You need to to stand in the unzipped folder before you invoke not install. Not your node folder.
    Lyricist I
    January 6, 2014
    Hello,

    I want to thank you for taking the time to write provide us with this project.

    I was able to install everything and have the node running and able to open up the player in a web browser, however, i have a quick quesetion - how do you invoke the http commands (could you please explain in layman's term?)

    Now you can control your system by invoking the following commands:

    http://localhost:5005/zones http://localhost:5005/lockvolumes http://localhost:5005/unlockvolumes http://localhost:5005/pauseall[/{timeout in minutes}] http://localhost:5005/resumeall[/{timeout in minutes}] http://localhost:5005/preset/{JSON preset} http://localhost:5005/preset/{predefined preset name} http://localhost:5005/{room name}/{action}[/{parameter}]

    Example:

    http://localhost:5005/living room/volume/15 (will set volume for room Living Room to 15%)

    http://localhost:5005/living room/volume/+1 (will increase volume by 1%)

    http://localhost:5005/living room/next (will skip to the next track on living room, unless it's not a coordinator)

    http://localhost:5005/living room/pause (will pause the living room)

    http://localhost:5005/living room/favorite/mysuperplaylist (will replace queue with the favorite called "mysuperplaylist")

    http://localhost:5005/living room/repeat/on (will turn on repeat mode for group)

    The actions supported as of today:

    play
    pause
    volume (parameter is absolute or relative volume. Prefix +/- indicates relative volume)
    groupVolume (parameter is absolute or relative volume. Prefix +/- indicates relative volume)
    mute / unmute
    groupMute / groupUnmute
    seek (parameter is queue index)
    next
    previous
    state (will return a json-representation of the current state of player)
    favorite
    lockvolumes / unlockvolumes (experimental, will enforce the volume that was selected when locking!)
    repeat (on/off)
    shuffle (on/off)
    pauseall (with optional timeout in minutes)
    resumeall (will resume the ones that was pause on the pauseall call. Useful for doorbell, phone calls, etc. Optional timeout)


    Am I supposed to open that as a link in a web browser? My player's room name is called "Living Room" and I'm trying to use the below command:
    http://localhost:5005/living room/volume/15

    If I paste the above in a web browser, I get a message saying I cannot connect to the server. I've also tried to replace 'localhost' with the server's IP address, same result.

    Sorry for the silly question, I'm just looking for ways to automate things from within my phone,
    Lyricist III
    January 6, 2014
    Sorry but i didn`t get it to work, when i will install the npm i get some errors...


    C:\Program Files (x86)\nodejs>npm install
    npm WARN package.json npm@1.3.21 No README data
    npm WARN package.json github-url-from-git@1.1.1 No repository field.
    npm WARN package.json ronn@0.3.8 'repositories' (plural) Not supported.
    npm WARN package.json Please pick one as the 'repository' field

    > npm@1.3.21 prepublish C:\Program Files (x86)\nodejs
    > node bin/npm-cli.js prune --prefix=. --no-global && rm -rf test/*/*/node_modul
    es && make -j32 doc


    module.js:340
    throw err;
    ^
    Error: Cannot find module 'C:\Program Files (x86)\nodejs\bin\npm-cli.js'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:902:3

    npm ERR! npm@1.3.21 prepublish: `node bin/npm-cli.js prune --prefix=. --no-globa
    l && rm -rf test/*/*/node_modules && make -j32 doc`
    npm ERR! Exit status 8
    npm ERR!
    npm ERR! Failed at the npm@1.3.21 prepublish script.
    npm ERR! This is most likely a problem with the npm package,
    npm ERR! not with npm itself.
    npm ERR! Tell the author that this fails on your system:
    npm ERR! node bin/npm-cli.js prune --prefix=. --no-global && rm -rf test/*/*
    /node_modules && make -j32 doc
    npm ERR! You can get their info via:
    npm ERR! npm owner ls npm
    npm ERR! There is likely additional logging output above.
    npm ERR! System Windows_NT 6.1.7601
    npm ERR! command "C:\\Program Files\\nodejs\\\\node.exe" "C:\\Program Files\\nod
    ejs\\node_modules\\npm\\bin\\npm-cli.js" "install"
    npm ERR! cwd C:\Program Files (x86)\nodejs
    npm ERR! node -v v0.10.24
    npm ERR! npm -v 1.3.21
    npm ERR! code ELIFECYCLE
    npm ERR!
    npm ERR! Additional logging details can be found in:
    npm ERR! C:\Program Files (x86)\nodejs\npm-debug.log
    npm ERR! not ok code 0

    Who can help me, thanks...
    Lyricist III
    January 6, 2014
    Hello,

    can i also work with the Janos Web Controller under an Xampp server?

    Do i need some other as Xampp?

    Thanks, best regards Thomas
    Lyricist II
    January 6, 2014
    Jishi,

    I wanted to thank you for dedicating the time and effort into this project. I had been searching Hi and Low for a solution that would allow me simple control of my Sonos system (Start Playing a Favorite, Skip Tracks) using my current remote system (a URC MSC400 with several remotes around the house).

    I had been using a Sonos plug-in for Homeseer but a whole Win7 PC dedicated for controlling Sonos was overkill.

    Thanks to you I now have a Raspberry Pi that I shoehorned into an empty Sonos Bridge. It required some modification but it just barely fits inside. I also placed an IR transmitter inside the case so it looks like a nice clean finished component. Just power, ir wire and a network cable out of the back of the bridge.

    Using the process described here: http://blog.rueedlinger.ch/2013/03/raspberry-pi-and-nodejs-basic-setup/ The RPi will boot and run your remote control node script.

    Its been working great for controlling Sonos via IR commands

    I have a couple of questions about issuing play commands and setting up direct control over zones (trying to avoid using the player.cordinator) but for the moment I just wanted to say thank you!
    Lyricist III
    January 13, 2014
    A open request :

    I am trying as I am not really a programmer ....

    To add a group of functions to play a tts mp3 file.

    Logic:
    Scan whole system remember the state of the system whats playing vol mute unmute etc.

    then create a mp3 file format from text file add it to the Que then play that file based on a preset created then return the system back to orig state.

    This could be used as a weather alert system doorbell anything that may come across even text messages. from your cell . I want to set it up with my Google calender

    Anyone interested in creating this?

    Regards

    Robert
    jishiAuthor
    Headliner II
    January 16, 2014
    Hello,

    I want to thank you for taking the time to write provide us with this project.

    I was able to install everything and have the node running and able to open up the player in a web browser, however, i have a quick quesetion - how do you invoke the http commands (could you please explain in layman's term?)


    The HTTP commands are a separate component than the web controller. You can run them at the same time, even using the same backend, but that requires some modifications to the files. The preferred way is to extend the web controller with the http api, but it will be harder to maintain atm. I might distribute a zip with them both active, for those who wants to use it.
    jishiAuthor
    Headliner II
    January 16, 2014
    Hello,

    can i also work with the Janos Web Controller under an Xampp server?

    Do i need some other as Xampp?

    Thanks, best regards Thomas


    I don't know what you want to achieve. My implementation is completely standalone and doesn't require anything else than node.js. Don't confuse it with the Janos application.

    Look at the first post, I have updated it with a downloadable link with all dependencies already met. You just unzip it to a folder and start it with:

    node server.js

    And you should be good to go.
    Lyricist III
    January 20, 2014
    Jishi
    Thanks to you I now have a Raspberry Pi that I shoehorned into an empty Sonos Bridge. It required some modification but it just barely fits inside. I also placed an IR transmitter inside the case so it looks like a nice clean finished component. Just power, ir wire and a network cable out of the back of the bridge.


    Hi Nice solution with the Raspberry Pi in a Bridge box. I have think to do the same but just run the Rasberry Pi unboxed so far.
    But I love to just use my spare Apple TV remote to pause/change volum then the phone ring.
    This is a thing that Sonos totally have missed to mount a simple IR recivier in all Sonos speakers.

    And agin great work Jishi!