Page 1 of 5
Easy Sonos
Posted: Thursday 09 April 2015 23:24
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
Re: Easy Sonos
Posted: Friday 10 April 2015 22:51
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");
?>
Re: Easy Sonos
Posted: Thursday 16 April 2015 20:13
by StanHD
Hi, could this be made to work under Domoticz in Windows?
Re: Easy Sonos
Posted: Thursday 16 April 2015 20:43
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.
Re: Easy Sonos
Posted: Thursday 16 April 2015 21:09
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
Re: Easy Sonos
Posted: Thursday 16 April 2015 21:36
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
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.
Re: Easy Sonos
Posted: Thursday 16 April 2015 21:42
by StanHD
I have a WHS 2011 (Server 2008 R2) Always on.
Re: Easy Sonos
Posted: Thursday 16 April 2015 21:44
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
Re: Easy Sonos
Posted: Thursday 16 April 2015 21:48
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/.
Re: Easy Sonos
Posted: Sunday 26 April 2015 13:37
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.
Re: Easy Sonos
Posted: Monday 27 April 2015 11:41
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"
Re: Easy Sonos
Posted: Saturday 16 May 2015 20:00
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
Re: Easy Sonos
Posted: Monday 25 May 2015 7:56
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)
Re: Easy Sonos
Posted: Monday 01 June 2015 22:18
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;
?>
Re: Easy Sonos
Posted: Monday 06 July 2015 11:43
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
in the top of every php file and issued
to get the right permissions.
What am I missing?
Thanks!
Re: Easy Sonos
Posted: Monday 06 July 2015 21:10
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?
Re: Easy Sonos
Posted: Tuesday 07 July 2015 8:09
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.
Re: Easy Sonos
Posted: Tuesday 07 July 2015 8:13
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?
Re: Easy Sonos
Posted: Tuesday 07 July 2015 19:51
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?
Re: Easy Sonos
Posted: Tuesday 07 July 2015 19:55
by G3rard
And you can also uncomment --print(str). Maybe that shows some more info in the log of Domoticz.