Easy Sonos

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

Moderator: leecollings

marmo
Posts: 20
Joined: Saturday 14 February 2015 16:22
Target OS: Linux
Domoticz version: 2020.1
Location: Madrid, Spain
Contact:

Easy Sonos

Post by marmo »

Likely because I havent used the right search or because everyone here is pretty skilled on these matters to write about the "obvious", thing is I´ve tried several times to find here info on how to integrate my sonos in domotics and almost gave up. It is one of the last pieces in my migration from Fibaro HC2, and an important one in my setup.

And after all those tries to connect the dots, to understand that thread which speaks about native sonos plugin, that soco thing, node-sonos-http-api and related beasts, planets aligned and got it so easily integrated that I´m almost embarrashed to make this public.

Just in case there´s any other colleague there in the same situation, here it goes what I did. Requisite was to make it simple, then simple, and finally simple. No compiles, no install this and that and end up with my little raspberry filled with components I dont know about.

Merit is for monsier Fabien here http://www.planete-domotique.com/blog/2 ... -leedomus/

1. Downloaded from https://www.github.com/DjMomo/sonos the file sonos.class.php and copied it to my scripts directory
2. It looked like no PHP in the PI so sudo apt-get install php5-cli
3. it complains because of some curl mambojambo, hopefully sudo apt-get install php5-curl fixed it
4. To make an easy test, created two separated scripts for PLAY (sonos.play.php) and PAUSE (sonos.pause.php), both similar except in the command itself. As follows:

Code: Select all

<?php
$IP_sonos_1 = "192.168.1.20";
require("sonos.class.php");
$sonos_1 = new SonosPHPController($IP_sonos_1);
$sonos_1 = $sonos_1->Play();
?>
5. End of the story. In a blink I´m controlling my sonos just by executing the command line "php sonos.play.php"

Ready now for the fun this weekend to make something more sophisticated like TTS which the class apparenlty also implements.

-j
Last edited by ThinkPad on Saturday 18 April 2015 10:25, edited 1 time in total.
Reason: Added code tags
marmo
Posts: 20
Joined: Saturday 14 February 2015 16:22
Target OS: Linux
Domoticz version: 2020.1
Location: Madrid, Spain
Contact:

Re: Easy Sonos

Post by marmo »

Amazing. TTS working like a charm. And coming from Fibaro where the widely used sonos VD had a significant delay until the message was played, this french gentleman had the geniality to store the messages as MP3s so only the first time a message is used it needs to go out to convert the text to voice, next time it will reproduce from the stored archive. Messages are spoken now at the speed of light.

Side effect, I had to install samba. But it´s worth it. The mini TTS script I´m using just in case.

<?php
require("sonos.class.php");
$IP_sonos_1 = "192.168.1.20";

$force_unmute = 0;
$message = $argv[1];
$volume = $argv[2];
if (empty($volume)) $volume = 0;

$sonos_1 = new SonosPHPController($IP_sonos_1);
$sonos_1->PlayTTS($message,"192.168.1.106/sonos",$volume,$force_unmute,"es");
?>
User avatar
StanHD
Posts: 347
Joined: Friday 12 July 2013 16:09
Target OS: Windows
Domoticz version:
Location: East Sussex, UK
Contact:

Re: Easy Sonos

Post by StanHD »

Hi, could this be made to work under Domoticz in Windows?
User avatar
G3rard
Posts: 669
Joined: Wednesday 04 March 2015 22:15
Target OS: -
Domoticz version: No
Location: The Netherlands
Contact:

Re: Easy Sonos

Post by G3rard »

Cool, that works like a charm! Thanks for sharing :)
And indeed an easy solution.

@StanHD, just add the PHP files on a web server. I am using my Synology NAS. Then make a dummy switch and in the On Action you can call the PHP play file.
Not using Domoticz anymore
User avatar
StanHD
Posts: 347
Joined: Friday 12 July 2013 16:09
Target OS: Windows
Domoticz version:
Location: East Sussex, UK
Contact:

Re: Easy Sonos

Post by StanHD »

G3rard wrote:Cool, that works like a charm! Thanks for sharing :)
And indeed an easy solution.

@StanHD, just add the PHP files on a web server. I am using my Synology NAS. Then make a dummy switch and in the On Action you can call the PHP play file.
My Domoticz is running on a Windows 7 NUC PC. So do I need to add a web server there? How do I do that?

Apologies for the basic questions :oops:
User avatar
G3rard
Posts: 669
Joined: Wednesday 04 March 2015 22:15
Target OS: -
Domoticz version: No
Location: The Netherlands
Contact:

Re: Easy Sonos

Post by G3rard »

StanHD wrote:
My Domoticz is running on a Windows 7 NUC PC. So do I need to add a web server there? How do I do that?

Apologies for the basic questions :oops:
The PHP files don't have to be on the same server as your Domoticz is running on. Do you have a Raspberry pi or NAS you can use as web server?
Otherwise you can check http://www.easyphp.org/ for installing on your NUC PC.
Not using Domoticz anymore
User avatar
StanHD
Posts: 347
Joined: Friday 12 July 2013 16:09
Target OS: Windows
Domoticz version:
Location: East Sussex, UK
Contact:

Re: Easy Sonos

Post by StanHD »

I have a WHS 2011 (Server 2008 R2) Always on.
User avatar
G3rard
Posts: 669
Joined: Wednesday 04 March 2015 22:15
Target OS: -
Domoticz version: No
Location: The Netherlands
Contact:

Re: Easy Sonos

Post by G3rard »

I have made PHP files for volume up and down, maybe someone is interested :)

Volume up:

Code: Select all

<?php
$IP_sonos_1 = "192.168.1.1"; // Sonos IP
 
require("sonos.class.php");
$sonos_1 = new SonosPHPController($IP_sonos_1);
$volume = $sonos_1->GetVolume();
echo "current volume: $volume<br>";
// if ($volume > 0)
$volume = $volume + 2;
echo "new volume: $volume";
$sonos_1 = $sonos_1->SetVolume($volume);
?>
Volume down:

Code: Select all

<?php
$IP_sonos_1 = "192.168.1.1"; // Sonos IP
 
require("sonos.class.php");
$sonos_1 = new SonosPHPController($IP_sonos_1);
$volume = $sonos_1->GetVolume();
echo "current volume: $volume<br>";
if ($volume > 1)
$volume = $volume - 2;
echo "new volume: $volume";
$sonos_1 = $sonos_1->SetVolume($volume);
?>
Next step for me is to implement these on my frontpage :D
Not using Domoticz anymore
User avatar
G3rard
Posts: 669
Joined: Wednesday 04 March 2015 22:15
Target OS: -
Domoticz version: No
Location: The Netherlands
Contact:

Re: Easy Sonos

Post by G3rard »

StanHD wrote:I have a WHS 2011 (Server 2008 R2) Always on.
I don't have WHS, but you could try http://www.wegotserved.com/2010/05/09/h ... rver-2011/.
Not using Domoticz anymore
fountside
Posts: 7
Joined: Tuesday 13 May 2014 18:05
Target OS: Linux
Domoticz version:
Contact:

Re: Easy Sonos

Post by fountside »

Hi, another way to control Sonos is to use https://github.com/jishi/node-sonos-http-api by jishi. I installed on a Raspberry Pi alongside Domoticz and can control all functions (including playlists, favourites and crossfading etc) via commands similar to http://192.168.x.x:5005/Kitchen/Volume/+5.
You need node for this to work, but it's pretty small footprint.
Hector
Posts: 2
Joined: Monday 27 April 2015 11:29
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Easy Sonos

Post by Hector »

i followed the instructions, and it works when using the command "php sonos.pause.php " in the terminal. So far, so good, however there is an issue running the PHP file from the switch (On Action). When called with "http://home/pi/domoticz/scripts/sonos.pause.php" this does not work.

EDIT:
got it ! :

put "#!/usr/bin/env php" before the "<?php" statement in sonos.pause.php
chmod u+x sonos.pause.php
call it with "script://home/pi/domoticz/scripts/sonos.pause.php"
dennis_montanje
Posts: 7
Joined: Tuesday 04 November 2014 20:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Easy Sonos

Post by dennis_montanje »

Hi Marmo,

Thank you for sharing this. I've also been trying for long to get functionality like this working. The play and pause script work perfectly. The ultimate goals is to get the TTS working. Unfortunately I run into some issues that I can't get solved:

1. When starting my test script I get the following: 'Undefined offset: 1 /home/pi/sonos.class.php on line 47'.
2. In my Sonos App I get the following error: 'Can't play "TTS- .mp3". Share //192.168.1.9/web does not exist.'.

Did you had the same problems?

THX
User avatar
gizmocuz
Posts: 2352
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

Re: Easy Sonos

Post by gizmocuz »

Looks good, the PHP should not be to hard to translate to c++, maybe someone wants to implement this ? (i dont have a sonos system)
Quality outlives Quantity!
User avatar
G3rard
Posts: 669
Joined: Wednesday 04 March 2015 22:15
Target OS: -
Domoticz version: No
Location: The Netherlands
Contact:

Re: Easy Sonos

Post by G3rard »

Would be nice if this is integrated in Domoticz.
I have a Sonos, but unfortunately no knowledge of c++ :?

I got a script from ab1976 to update the status of a virtual Sonos switch in Domoticz if the Sonos is switched on/off with another controller.
I altered it a bit and it is running as a LUA time script on a Synology.

LUA script

Code: Select all

function execute(command)
        -- returns success, error code, output.
    local f = io.popen(command..' 2>&1 && echo " $?"')
    local output = f:read"*a"
    return output
end

commandArray = {}

str=execute('php /volume1/web/domoticz/sonos/sonos.status-kantoor.php','r')
str2=execute('php /volume1/web/domoticz/sonos/sonos.status-keuken.php','r')

if string.find(str, "PLAYING") then
	print("Sonos kantoor aan")
	--print(str)
	if otherdevices['Sonos kantoor']=='Off' then
		commandArray['Sonos kantoor']='On'
	end
else
	print("Sonos kantoor uit")
	--print(str)
	if otherdevices['Sonos kantoor']=='On' then
		commandArray['Sonos kantoor']='Off'
	end
end

if string.find(str2, "PLAYING") then
	print("Sonos keuken aan")
	--print(str2)
	if otherdevices['Sonos keuken']=='Off' then
		commandArray['Sonos keuken']='On'
	end
else
	print("Sonos keuken uit")
	--print(str2)
	if otherdevices['Sonos keuken']=='On' then
		commandArray['Sonos keuken']='Off'
	end
end

return commandArray
PHP page

Code: Select all

<?php
$IP_sonos_1 = "192.168.1.1";
require("sonos.class.php");
$sonos_1 = new SonosPHPController($IP_sonos_1);
$transInfo= $sonos_1->GetTransportInfo();
echo $transInfo;
?>
Not using Domoticz anymore
thorbj
Posts: 113
Joined: Thursday 20 November 2014 22:11
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Norway
Contact:

Re: Easy Sonos

Post by thorbj »

Hi
Sonehow I can't get this to work.
When I try to run sonos.play.php from cli I get the following error:

Code: Select all

PHP Fatal error: Class 'SonosPHPController' not found in /home/pi/domoticz/scripts/php/sonos.play.php on line 5
Here's my sonos.play.php:

Code: Select all

#!/usr/bin/env php
<?php
$IP_sonos_1 = "10.10.10.139";
require("sonos.class.php");
$sonos_1 = new SonosPHPController($IP_sonos_1);
$sonos_1 = $sonos_1->Play();
?>
I've followed @marmo's guide, and I've added

Code: Select all

"#!/usr/bin/env php
in the top of every php file and issued

Code: Select all

chmod u+x sonos.play.php
to get the right permissions.

What am I missing?

Thanks!
Last edited by thorbj on Tuesday 07 July 2015 8:09, edited 1 time in total.
2xRaspberry Pi Model 2 w/RaZberry z-wave chip (master/slave)|Fibaro Wall Plug|Fibaro Universal Dimmer 500W|Aeon Labs Multisensor|RFXtrx433E|Nexa WMR-1000|Nexa Pe-3|Nexa Remote|Nexa LGDR3500|Lightify Gateway|Lightify RGBW bulb
marmo
Posts: 20
Joined: Saturday 14 February 2015 16:22
Target OS: Linux
Domoticz version: 2020.1
Location: Madrid, Spain
Contact:

Re: Easy Sonos

Post by marmo »

sorry for the obvious but as I'm coming from Windows and not knowing Linux enough, I became paranoid of letter case issues. The message error points to a 'SonosPHpController' with a lowercase 'p'. The sonos.play.php script though shows the proper name. Could it be the reason?
thorbj
Posts: 113
Joined: Thursday 20 November 2014 22:11
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Norway
Contact:

Re: Easy Sonos

Post by thorbj »

marmo wrote:sorry for the obvious but as I'm coming from Windows and not knowing Linux enough, I became paranoid of letter case issues. The message error points to a 'SonosPHpController' with a lowercase 'p'. The sonos.play.php script though shows the proper name. Could it be the reason?
Thank you for pointing this out. It could be a big problem, but unfortunately it was just a typing-error that occured while typing the error message into the post.
2xRaspberry Pi Model 2 w/RaZberry z-wave chip (master/slave)|Fibaro Wall Plug|Fibaro Universal Dimmer 500W|Aeon Labs Multisensor|RFXtrx433E|Nexa WMR-1000|Nexa Pe-3|Nexa Remote|Nexa LGDR3500|Lightify Gateway|Lightify RGBW bulb
thorbj
Posts: 113
Joined: Thursday 20 November 2014 22:11
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Norway
Contact:

Re: Easy Sonos

Post by thorbj »

G3rard wrote:Would be nice if this is integrated in Domoticz.
I have a Sonos, but unfortunately no knowledge of c++ :?

I got a script from ab1976 to update the status of a virtual Sonos switch in Domoticz if the Sonos is switched on/off with another controller.
I altered it a bit and it is running as a LUA time script on a Synology.

LUA script

Code: Select all

function execute(command)
        -- returns success, error code, output.
    local f = io.popen(command..' 2>&1 && echo " $?"')
    local output = f:read"*a"
    return output
end

commandArray = {}

str=execute('php /volume1/web/domoticz/sonos/sonos.status-kantoor.php','r')
str2=execute('php /volume1/web/domoticz/sonos/sonos.status-keuken.php','r')

if string.find(str, "PLAYING") then
	print("Sonos kantoor aan")
	--print(str)
	if otherdevices['Sonos kantoor']=='Off' then
		commandArray['Sonos kantoor']='On'
	end
else
	print("Sonos kantoor uit")
	--print(str)
	if otherdevices['Sonos kantoor']=='On' then
		commandArray['Sonos kantoor']='Off'
	end
end

if string.find(str2, "PLAYING") then
	print("Sonos keuken aan")
	--print(str2)
	if otherdevices['Sonos keuken']=='Off' then
		commandArray['Sonos keuken']='On'
	end
else
	print("Sonos keuken uit")
	--print(str2)
	if otherdevices['Sonos keuken']=='On' then
		commandArray['Sonos keuken']='Off'
	end
end

return commandArray
PHP page

Code: Select all

<?php
$IP_sonos_1 = "192.168.1.1";
require("sonos.class.php");
$sonos_1 = new SonosPHPController($IP_sonos_1);
$transInfo= $sonos_1->GetTransportInfo();
echo $transInfo;
?>
Hi @G3rard, thanks for sharing this!
In my setup the Sonos system (playbar) seems to always display as ON. Is this normal?
2xRaspberry Pi Model 2 w/RaZberry z-wave chip (master/slave)|Fibaro Wall Plug|Fibaro Universal Dimmer 500W|Aeon Labs Multisensor|RFXtrx433E|Nexa WMR-1000|Nexa Pe-3|Nexa Remote|Nexa LGDR3500|Lightify Gateway|Lightify RGBW bulb
User avatar
G3rard
Posts: 669
Joined: Wednesday 04 March 2015 22:15
Target OS: -
Domoticz version: No
Location: The Netherlands
Contact:

Re: Easy Sonos

Post by G3rard »

Hi thorbj, that's not normal. When you switch off the Sonos (eg with the Sonos app) then this should be visible in the status of the switch. That can take a minute due to the time script, but it should change to off.

What does the php show if you open it in the browser and the Sonos is off?
Not using Domoticz anymore
User avatar
G3rard
Posts: 669
Joined: Wednesday 04 March 2015 22:15
Target OS: -
Domoticz version: No
Location: The Netherlands
Contact:

Re: Easy Sonos

Post by G3rard »

And you can also uncomment --print(str). Maybe that shows some more info in the log of Domoticz.
Not using Domoticz anymore
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest