Step by step guide: Sonos integration

In this subforum you can show projects you have made, or you are busy with. Please create your own topic.

Moderator: leecollings

DomoRies
Posts: 14
Joined: Friday 25 November 2016 22:46
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Step by step guide: Sonos integration

Post by DomoRies »

Hi al,
Trying to install the program and running npm start i get the following code. What am i doing wrong?
I installed it on my raspberry pi with raspbian jessie
node version 7.2.0
npm version 3.10.9

Code: Select all

pi@raspberrypi:~/node-sonos-http-api $ npm start

> [email protected] start /home/pi/node-sonos-http-api
> node server.js

module.js:472
    throw err;
    ^

Error: Cannot find module 'request'
    at Function.Module._resolveFilename (module.js:470:15)
    at Function.Module._load (module.js:418:25)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at /home/pi/node-sonos-http-api/node_modules/request-promise/lib/rp.js:26:24
    at Object.<anonymous> (/home/pi/node-sonos-http-api/node_modules/request-promise/lib/rp.js:33:3)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/home/pi/node-sonos-http-api/node_modules/sonos-discovery/lib/services/deezer.js:2:17)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)

npm ERR! Linux 4.4.32-v7+
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "start"
npm ERR! node v7.2.0
npm ERR! npm  v3.10.9
npm ERR! code ELIFECYCLE
npm ERR! [email protected] start: `node server.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] start script 'node server.js'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the sonos-http-api package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node server.js
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs sonos-http-api
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls sonos-http-api
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     /home/pi/node-sonos-http-api/npm-debug.log
Ok it's working now :D
Last edited by DomoRies on Tuesday 13 December 2016 12:54, edited 2 times in total.
josimd
Posts: 59
Joined: Saturday 09 January 2016 19:47
Target OS: -
Domoticz version:
Contact:

Re: Step by step guide: Sonos integration

Post by josimd »

Hi, I'm running domoticz on Synology...somehow it looks like the program hangs after npm starts.... any suggestions?

~# cd node-sonos-http-api
root@iSIM_NAS:~/node-sonos-http-api# npm start

> [email protected] start /root/node-sonos-http-api
> node server.js

2016-11-26T19:30:28.418Z INFO no settings file found, will only use default settings
2016-11-26T19:30:34.097Z INFO Presets loaded: { example:
{ players:
[ { roomName: 'WOONKAMER', volume: 10 },
{ roomName: 'STUDEERKAMER', volume: 10 },
{ roomName: 'Office', volume: 10 },
{ roomName: 'Bedroom', volume: 10 },
{ roomName: 'TV Room', volume: 15 } ],
playMode: { shuffle: true, repeat: 'all', crossfade: false },
pauseOthers: false,
favorite: 'My example favorite' } }
2016-11-26T19:30:34.262Z INFO http server listening on port 5005
mabo21
Posts: 8
Joined: Saturday 07 January 2017 17:17
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Step by step guide: Sonos integration

Post by mabo21 »

Hi Holland,
thanks for the great solution! But i have a few issues and hope you can help me in any way... I have done everything you described, but when i start npm i get some errors.

Code: Select all

ash-4.3# npm start

> [email protected] start /volume1/homes/admin/node-sonos-http-api
> node server.js

2017-01-08T14:55:13.489Z INFO no settings file found, will only use default settings
2017-01-08T14:55:14.669Z INFO Presets loaded: { example: 
   { players: 
      [ { roomName: 'Bathroom', volume: 10 },
        { roomName: 'Kitchen', volume: 10 },
        { roomName: 'Office', volume: 10 },
        { roomName: 'Bedroom', volume: 10 },
        { roomName: 'TV Room', volume: 15 } ],
     playMode: { shuffle: true, repeat: 'all', crossfade: false },
     pauseOthers: false,
     favorite: 'My example favorite' } }
2017-01-08T14:55:15.281Z ERROR Port 5005 seems to be in use already. Make sure the sonos
-http-api isn't 
    already running, or that no other server uses that port. You can specify an alternat
ive http port 
    with property "port" in settings.json

npm ERR! Linux 3.10.77
npm ERR! argv "/volume1/@appstore/Node.js_v4/usr/local/bin/node" "/usr/local/bin/npm" "s
tart"
npm ERR! node v4.4.2
npm ERR! npm  v4.0.5
npm ERR! code ELIFECYCLE
npm ERR! [email protected] start: `node server.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] start script 'node server.js'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the sonos-http-api package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node server.js
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs sonos-http-api
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls sonos-http-api
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     /volume1/homes/admin/node-sonos-http-api/npm-debug.log
ash-4.3# 
Server.js is still running.
Node.js version is 4.4.2
npm version is 4.0.5

Please can you take a look and help me?

Thanks a lot and best,
Mario
mabo21
Posts: 8
Joined: Saturday 07 January 2017 17:17
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Step by step guide: Sonos integration

Post by mabo21 »

I have uninstall everything and started again. Now it runs without issues.

Best,
Mario
mabo21 wrote:Hi Holland,
thanks for the great solution! But i have a few issues and hope you can help me in any way... I have done everything you described, but when i start npm i get some errors.

Code: Select all

ash-4.3# npm start

> [email protected] start /volume1/homes/admin/node-sonos-http-api
> node server.js

2017-01-08T14:55:13.489Z INFO no settings file found, will only use default settings
2017-01-08T14:55:14.669Z INFO Presets loaded: { example: 
   { players: 
      [ { roomName: 'Bathroom', volume: 10 },
        { roomName: 'Kitchen', volume: 10 },
        { roomName: 'Office', volume: 10 },
        { roomName: 'Bedroom', volume: 10 },
        { roomName: 'TV Room', volume: 15 } ],
     playMode: { shuffle: true, repeat: 'all', crossfade: false },
     pauseOthers: false,
     favorite: 'My example favorite' } }
2017-01-08T14:55:15.281Z ERROR Port 5005 seems to be in use already. Make sure the sonos
-http-api isn't 
    already running, or that no other server uses that port. You can specify an alternat
ive http port 
    with property "port" in settings.json

npm ERR! Linux 3.10.77
npm ERR! argv "/volume1/@appstore/Node.js_v4/usr/local/bin/node" "/usr/local/bin/npm" "s
tart"
npm ERR! node v4.4.2
npm ERR! npm  v4.0.5
npm ERR! code ELIFECYCLE
npm ERR! [email protected] start: `node server.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] start script 'node server.js'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the sonos-http-api package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node server.js
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs sonos-http-api
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls sonos-http-api
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     /volume1/homes/admin/node-sonos-http-api/npm-debug.log
ash-4.3# 
Server.js is still running.
Node.js version is 4.4.2
npm version is 4.0.5

Please can you take a look and help me?

Thanks a lot and best,
Mario
Trigun
Posts: 390
Joined: Wednesday 30 November 2016 11:58
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Contact:

Re: Step by step guide: Sonos integration

Post by Trigun »

Hi all,

sorry but I am a complete noob out here.
I really like to use this feature for my domoticz setup but I am not getting any further.
so this is what I did.

>checked that I did have node version 4.0.0
>did the clone
>cd node-sonos-http-api
>sudo npm install --production
>> it installed without any error, assuming that I did the right thing.

but when I enter PM2 start server.js I get the following: command not found.

when I enter: npm start it works perfectly and I even get a page when entering domoticzip:5005

creating a switch is done correctly

But this is where I get stuck.
I have the feeling that I need to setup a config file where I confige the rooms and sonos Ip etc.
but have no idea how to get any further.

I hope someone cn help me out here.

Thanks in advance!
Holland
Posts: 179
Joined: Friday 12 July 2013 13:53
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta Ch
Location: The Netherlands
Contact:

Re: Step by step guide: Sonos integration

Post by Holland »

@Trigun

You need to jump into the sonos-http-api directory; then you can issue: pm2 start server.js
Trigun
Posts: 390
Joined: Wednesday 30 November 2016 11:58
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Contact:

Re: Step by step guide: Sonos integration

Post by Trigun »

Hi Holland, thank you for your reply. but so far no luck. I did what you told but it doesn't seem to start.
below a screenshot of what I did.

Can you please help me out here?
2017-01-25 20_59_15-Clipboard.png
2017-01-25 20_59_15-Clipboard.png (184.31 KiB) Viewed 6882 times
Thanks in advance!
User avatar
G3rard
Posts: 669
Joined: Wednesday 04 March 2015 22:15
Target OS: -
Domoticz version: No
Location: The Netherlands
Contact:

Re: Step by step guide: Sonos integration

Post by G3rard »

If you are on on of the latest beta's then there also is a Python plugin available for Sonos.
Check http://www.domoticz.com/forum/viewtopic ... 3&p=116118 for more information.
Not using Domoticz anymore
Holland
Posts: 179
Joined: Friday 12 July 2013 13:53
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta Ch
Location: The Netherlands
Contact:

Re: Step by step guide: Sonos integration

Post by Holland »

@Trigun;

Have you installed PM2? It's not part of the Domoticz sd image, or Jessie Light

See http://pm2.keymetrics.io/docs/usage/quick-start/
Trigun
Posts: 390
Joined: Wednesday 30 November 2016 11:58
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Contact:

Re: Step by step guide: Sonos integration

Post by Trigun »

Holland wrote:@Trigun;

Have you installed PM2? It's not part of the Domoticz sd image, or Jessie Light

See http://pm2.keymetrics.io/docs/usage/quick-start/
Hi Holland, I got it working!

NPM start did the job! (since I don't have PM2 running ;) )

next thing is to keep it running when I close the session.

sorry I am a huge noob with commandline. do you perhaps have any suggestions?

Thanks again !
mvzut
Posts: 443
Joined: Thursday 12 November 2015 10:55
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: Step by step guide: Sonos integration

Post by mvzut »

Hi guys,

Are you aware that there is a new way to integrate hardware in Domoticz, via Python plugins, and that there is already an excellent Sonos plugin created in this way? See viewtopic.php?t=15543
I had it running within a few minutes, without installing anything. There is one prerequisite though, and that is that you use the latest beta.
Raspberry Pi 4 - RFXtrx433 - CC2531 Zigbee - Opentherm Gateway - P1 smart meter - Netatmo - Philips Hue - ELV Max! - ESP8266 DIY water meter - 6 x Sonos - 4 x IP cameras - Wall mounted tablet + Dashticz - Google Home integration - MANY switches/sensors
Trigun
Posts: 390
Joined: Wednesday 30 November 2016 11:58
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Contact:

Re: Step by step guide: Sonos integration

Post by Trigun »

mvzut wrote:Hi guys,

Are you aware that there is a new way to integrate hardware in Domoticz, via Python plugins, and that there is already an excellent Sonos plugin created in this way? See viewtopic.php?t=15543
I had it running within a few minutes, without installing anything. There is one prerequisite though, and that is that you use the latest beta.
Hi mvzut, thanks for you reply,

I have to say that the solution with NPM also works fantastic!

If I manage to get it working after closing the putty session, it works like a charm!
I am aware that it is basic Linuc knowledge for this solution but hey, I'm just starting/beginning :)

if there is anyone who would like to help a noob out here, thanks in advance!
Trigun
Posts: 390
Joined: Wednesday 30 November 2016 11:58
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Contact:

Re: Step by step guide: Sonos integration

Post by Trigun »

@ Holland,

thanks for the solution! I installed PM2 and its working perfectly!

what specifi things do I need to edit in de Lua script to get the player state?

Thanks again!
Holland
Posts: 179
Joined: Friday 12 July 2013 13:53
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta Ch
Location: The Netherlands
Contact:

Re: Step by step guide: Sonos integration

Post by Holland »

@Trigun,

I use the following lua to check a couple of things related to the Sonos group in the livingroom(woonkamer), e.g the status, but also set Sonos to pauze when tv is switched on etc.

Code: Select all

json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()

local sonos=assert(io.popen('curl http://ip:5005/eetkamer/state'))
local status = sonos:read('*all')
sonos:close()
local jsonStatus = json:decode(status)

local tv_woonkamer_status = 'TV Woonkamer System Alive'
local sonos_woonkamer = 'Sonos Woonkamer/Eetkamer'

--print("Eerste JSON stappen met LUA")
--print(status)
-- print(jsonStatus)

--playerstate = jsonStatus['playerState']
playbackState = jsonStatus['playbackState']
artist = jsonStatus['currentTrack']['artist']
title = jsonStatus['currentTrack']['title']
album = jsonStatus['currentTrack']['album']
type = jsonStatus['currentTrack']['type']
--streaminfo = jsonStatus['currentTrack']['streamInfo']
	
commandArray = {}

if (playbackState == "STOPPED" or playbackState == "PAUSED_PLAYBACK" or playbackState == "PLAYING") then
	if (playbackState == "PLAYING") then
		if type == "radio" then
			print('Woonkamer/Eetkamer Sonos status: ' ..playbackState.. ' ' ..type.. ' Titel: '..title)
		else
			if artist ~= nil then
				print('Badkamer Sonos status: ' ..playbackState.. ' Artiest: ' ..artist.. ' Titel: '..title)
			else
				print('Badkamer Sonos status: ' ..playbackState.. ' Titel: '..title)
			end	
		end
		if (uservariables["SonosWoonkamerStatus"] == "PAUSED") then
			commandArray['Variable:SonosWoonkamerStatus']='PLAYING'
			print("Uservar SonosWoonkamer updated to PLAYING")
		end
	end
	if (playbackState == "STOPPED" or playbackState == "PAUSED_PLAYBACK") then
		print('Woonkamer/Eetkamer Sonos Status: ' ..playbackState)
		if (uservariables["SonosWoonkamerStatus"] == "PLAYING") then
			commandArray['Variable:SonosWoonkamerStatus']='PAUSED'
			print("Uservar SonosWoonkamer updated to PAUSED")
		end	
	end
else
	--print("2")
	print('<font color="red">Woonkamer/Eetkamer: SONOS-HTTP-API is off-line</font>')
end	

if (playbackState == "STOPPED" or playbackState == "PAUSED_PLAYBACK") then
	switch_status_sonos_woonkamer_eetkamer = otherdevices["Sonos Woonkamer/Eetkamer"]
	print('Sonos Woonkamer/Eetkamer Switch Status: ' ..switch_status_sonos_woonkamer_eetkamer)
	if otherdevices["Sonos Woonkamer/Eetkamer"] == 'Pauze' then
		print('Sonos Woonkamer/Eetkamer Switch Status is correct, should be PAUZE: ' ..switch_status_sonos_woonkamer_eetkamer)
	else
		print('Sonos Woonkamer/Eetkamer Switch Status not correct, should be PAUZE: ' ..switch_status_sonos_woonkamer_eetkamer)
		commandArray['UpdateDevice'] = '43|20|20'
	end
end	

if (playbackState == "PLAYING") then
	switch_status_sonos_woonkamer_eetkamer = otherdevices["Sonos Woonkamer/Eetkamer"]
	print('Sonos Woonkamer/Eetkamer Switch Status: ' ..switch_status_sonos_woonkamer_eetkamer)
	if otherdevices["Sonos Woonkamer/Eetkamer"] == 'Play' then
		print('Sonos Woonkamer/Eetkamer Switch Status is correct, should be PLAY: ' ..switch_status_sonos_woonkamer_eetkamer)
	else
		print('Sonos Woonkamer/Eetkamer Switch Status not correct, should be PLAY: ' ..switch_status_sonos_woonkamer_eetkamer)
		commandArray['UpdateDevice'] = '43|10|10'
	end
end	

if devicechanged[tv_woonkamer_status] == 'On' then 
 	print("TV Woonkamer is net aangezet, status Sonos Woonkamer onderzoeken")
	if (playerstate == "PLAYING" or playbackState == "PLAYING") then
		print("Sonos woonkamer heeft status PLAYING, Sonos Woonkamer/Eetkamer wordt uitgeschakeld")
		commandArray[sonos_woonkamer]='Set Level 20'
	end	
end


return commandArray
markiemajax
Posts: 17
Joined: Monday 18 May 2015 15:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.4834
Contact:

Re: Step by step guide: Sonos integration

Post by markiemajax »

I got it partly working, but what ip do i use?

i got 3 sonos speakers (1 playbar, 2 sonos 1)

Or is it the ip of my domoticz (raspberry)?
Holland
Posts: 179
Joined: Friday 12 July 2013 13:53
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta Ch
Location: The Netherlands
Contact:

Re: Step by step guide: Sonos integration

Post by Holland »

Just use the ip address of your sonos player. If you have grouped sonos players, use the ip address of the zone coordinator.
markiemajax
Posts: 17
Joined: Monday 18 May 2015 15:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.4834
Contact:

Re: Step by step guide: Sonos integration

Post by markiemajax »

This is the log i get. It is still not working.
I put my script in attechment.
2017-03-17 10_51_27-Domoticz.png
2017-03-17 10_51_27-Domoticz.png (205.28 KiB) Viewed 6591 times
Attachments
script_device_sonos_tv_woonkamer.zip
(1.12 KiB) Downloaded 101 times
Holland
Posts: 179
Joined: Friday 12 July 2013 13:53
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta Ch
Location: The Netherlands
Contact:

Re: Step by step guide: Sonos integration

Post by Holland »

Do you get anything in return if you do this;

http://ip:5005/name of your sonos/state
markiemajax
Posts: 17
Joined: Monday 18 May 2015 15:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.4834
Contact:

Re: Step by step guide: Sonos integration

Post by markiemajax »

Hello,

Yes i get status "error". I use the ip of my domoticz. If i use the ip of my sonos playbar the site is not available.

Something is wrong, but i don't know what.


Holland wrote:Do you get anything in return if you do this;

http://ip:5005/name of your sonos/state
markiemajax
Posts: 17
Joined: Monday 18 May 2015 15:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.4834
Contact:

Re: Step by step guide: Sonos integration

Post by markiemajax »

Hello,

If i use http://ip domoticz:5005/state, i get this.
2017-03-19 11_26_20-10.100.75.113_5005_state.png
2017-03-19 11_26_20-10.100.75.113_5005_state.png (17.53 KiB) Viewed 6558 times
Holland wrote:Do you get anything in return if you do this;

http://ip:5005/name of your sonos/state
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest