Page 3 of 5

Re: Easy Sonos

Posted: Tuesday 21 July 2015 9:30
by G3rard
Php files can be called from a switch, I am using that but you have to run the php from a web server like a Synology and use http:// instead of script://.

Re: Easy Sonos

Posted: Tuesday 21 July 2015 10:37
by trekker25
G3rard wrote:Php files can be called from a switch, I am using that but you have to run the php from a web server like a Synology and use http:// instead of script://.
got it all working. Created 2 dummy switches for each Sonos. And in Blocky sent a command when the doorbel is rang!

Even when the both speakers are streaming radio in a Group, when the doorbell rings, the sonos speakers stop each nicely, play the doorbell mp3 and afterwards continue to play the radio stream!!!

Cheers for the solution from this German guy!!! And thanks G3rard for pointing me to the solution!

Re: Easy Sonos

Posted: Tuesday 21 July 2015 14:31
by thorbj
G3rard wrote: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?
I found the solution to this, somehow I had downloaded the html of the github, instead of the sonos.class.php :lol: :lol:

Re: Easy Sonos

Posted: Tuesday 21 July 2015 15:29
by thorbj
G3rard wrote:You can take a look at https://github.com/ThomasTr/sonos. That has the possibility to stop current playing radio/list, plays a mp3, continues previously played radio/list. Maybe you can use that for the doorbell mp3.
I'm having difficulties getting this to work.
After having downloaded the files from Git, and the PHPSonos.inc.php, and put then on my webserver, I get the following error:
Fatal error: Uncaught exception 'Exception' with message 'Missing zone' in /home/pi/sonos/sonos.php:70 Stack trace: #0 /home/pi/sonos/sonos.php(58): Sonos->_assertZone('') #1 /home/pi/sonos/sonos.php(10): Sonos->_main() #2 /home/pi/sonos/index.php(7): Sonos->__construct() #3 {main} thrown in /home/pi/sonos/sonos.php on line 70
I have added my sonos speakers IP adresses to the config.php.

Code: Select all

<?php
$config = array(
	'zones' => array(
                'Stue'    => '10.10.10.139',
                'Stue' => '10.10.10.119',
                'Stue'  => '10.10.10.137',
				),
	'radiostations' => array(
		'DasDing'  => 'swr-mp3-m-dasding.akacast.akamaistream.net/7/588/137139/v1/gnl.akacast.akamaistream.net/swr-mp3-m-dasding',
                '1Live'    => '1live-diggi.akacast.akamaistream.net/7/965/119435/v1/gnl.akacast.akamaistream.net/1live-diggi',           
		'SWR3'     => 'mp3-live.swr3.de/swr3_m.m3u',
                'Donau3FM' => 'server1.webradiostreaming.de:2640',
	),
        'messagePath'   => '/home/pi/sonos/messages/',
        'messageStorePath'   => '/home/pi/sonos/messages/',
        'messageLang'   => 'no',
        'currentRadio'  => 'currentRadio.txt',
        'filePhpSonos'  => 'PHPSonos.inc.php',
        'logging'       => false,
        'logfile'       => 'log.txt',
);
?>
Does anyone know what can be wrong here?

-Thanks!

Easy Sonos

Posted: Tuesday 21 July 2015 15:48
by G3rard
First try it with one zone in the config.php file or otherwise give the 3 zones a different name.

Re: Easy Sonos

Posted: Tuesday 21 July 2015 16:30
by trekker25
G3rard wrote:First try it with one zone in the config.php file or otherwise give the 3 zones a different name.
my first guess indeed. as you need to send a message/command to 1 zone.
That's they only negative of this solution: doesn't recognize grouped speakers, but for the rest it works charmless.

Finished by doorbell project and i get a Prowl message and a doorbell ring on both of my Sonos speakers! Now it's time to install my Foscam IP camera and i'm done!

Re: Easy Sonos

Posted: Wednesday 22 July 2015 8:03
by thorbj
G3rard wrote:First try it with one zone in the config.php file or otherwise give the 3 zones a different name.
Thanks for the reply!

I have now tried with only one zone, but I still get the same error.

Here is the edited config.php:

Code: Select all

<?php
$config = array(
	'zones' => array(
                'Stue'    => '10.10.10.139',
				),
	'radiostations' => array(
		'DasDing'  => 'swr-mp3-m-dasding.akacast.akamaistream.net/7/588/137139/v1/gnl.akacast.akamaistream.net/swr-mp3-m-dasding',
                '1Live'    => '1live-diggi.akacast.akamaistream.net/7/965/119435/v1/gnl.akacast.akamaistream.net/1live-diggi',           
		'SWR3'     => 'mp3-live.swr3.de/swr3_m.m3u',
                'Donau3FM' => 'server1.webradiostreaming.de:2640',
	),
        'messagePath'   => '/home/pi/sonos/messages/',
        'messageStorePath'   => '/home/pi/sonos/messages/',
        'messageLang'   => 'no',
        'currentRadio'  => 'currentRadio.txt',
        'filePhpSonos'  => 'PHPSonos.inc.php',
        'logging'       => false,
        'logfile'       => 'log.txt',
);
?>
- Thanks!

Re: Easy Sonos

Posted: Wednesday 22 July 2015 9:18
by trekker25
thorbj wrote:
G3rard wrote:First try it with one zone in the config.php file or otherwise give the 3 zones a different name.
Thanks for the reply!

I have now tried with only one zone, but I still get the same error.

Here is the edited config.php:

Code: Select all

<?php
$config = array(
	'zones' => array(
                'Stue'    => '10.10.10.139',
				),
	'radiostations' => array(
		'DasDing'  => 'swr-mp3-m-dasding.akacast.akamaistream.net/7/588/137139/v1/gnl.akacast.akamaistream.net/swr-mp3-m-dasding',
                '1Live'    => '1live-diggi.akacast.akamaistream.net/7/965/119435/v1/gnl.akacast.akamaistream.net/1live-diggi',           
		'SWR3'     => 'mp3-live.swr3.de/swr3_m.m3u',
                'Donau3FM' => 'server1.webradiostreaming.de:2640',
	),
        'messagePath'   => '/home/pi/sonos/messages/',
        'messageStorePath'   => '/home/pi/sonos/messages/',
        'messageLang'   => 'no',
        'currentRadio'  => 'currentRadio.txt',
        'filePhpSonos'  => 'PHPSonos.inc.php',
        'logging'       => false,
        'logfile'       => 'log.txt',
);
?>
- Thanks!
are you running it on your Pi? That doesnt work if you haven't installed a seperate webserver.


my config:
<?php

$config = array(
'zones' => array(
'Woonkamer' => '172.16.2.166',
'Keuken' => '172.16.3.164',

),
'radiostations' => array(
'DasDing' => 'swr-mp3-m-dasding.akacast.akamaistream.net/7/588/137139/v1/gnl.akacast.akamaistream.net/swr-mp3-m-dasding',
'1Live' => '1live-diggi.akacast.akamaistream.net/7/965/119435/v1/gnl.akacast.akamaistream.net/1live-diggi',
'SWR3' => 'mp3-live.swr3.de/swr3_m.m3u',
'Donau3FM' => 'server1.webradiostreaming.de:2640',
),
'messagePath' => '//172.16.0.3/Public/MEDIA/MP3/',
'messageStorePath' => '/c/music/_Sprachmeldungen/',
'messageLang' => 'du',
'currentRadio' => 'currentRadio.txt',
'filePhpSonos' => 'PHPSonos.inc.php',
'logging' => true,
'logfile' => 'log.txt',
);


so message path is a full name to a server.

Also i'm running it own my Qnap NAS.

Re: Easy Sonos

Posted: Wednesday 22 July 2015 9:42
by thorbj
I'm running on an Apache-install on my pi. That shouldn't be a problem. Or am I wrong?
I copied your configuration, but added my on IP for the sonos speaker, still same error. I have tried with the IP of all my speakers, even the BRIDGE.

Re: Easy Sonos

Posted: Wednesday 22 July 2015 14:02
by trekker25
thorbj wrote:I'm running on an Apache-install on my pi. That shouldn't be a problem. Or am I wrong?
I copied your configuration, but added my on IP for the sonos speaker, still same error. I have tried with the IP of all my speakers, even the BRIDGE.
maybe a chmod issue?

and did you try opening it just in a browser and what url do you use?

Re: Easy Sonos

Posted: Wednesday 22 July 2015 14:37
by thorbj
All php files have 755 rights, and the filder has 777.
I've tried accessing it via a browser, that's when the error appears. http://ServerIP:port/index.php

Re: Easy Sonos

Posted: Wednesday 22 July 2015 19:59
by trekker25
http://172.16.0.3/sonos/index.php?zone= ... &volume=80
try something like this.

The index.php demands to have a GET variable called zone.

Re: Easy Sonos

Posted: Wednesday 22 July 2015 22:58
by G3rard
trekker25 wrote:http://172.16.0.3/sonos/index.php?zone= ... &volume=80
try something like this.

The index.php demands to have a GET variable called zone.
Indeed and I would start with just the play command, because in my setup playing messages was not so easy (due to config I had to do).

Code: Select all

http://<ip>/sonos/index.php?zone=Stue&action=Play

Re: Easy Sonos

Posted: Thursday 23 July 2015 8:10
by thorbj
G3rard wrote:
trekker25 wrote:http://172.16.0.3/sonos/index.php?zone= ... &volume=80
try something like this.

The index.php demands to have a GET variable called zone.
Indeed and I would start with just the play command, because in my setup playing messages was not so easy (due to config I had to do).

Code: Select all

http://<ip>/sonos/index.php?zone=Stue&action=Play
Ah, so I need to issue commands with the index.php address? I will try playing with that later when I'm home again :) Tank you!!

Re: Easy Sonos

Posted: Thursday 23 July 2015 8:38
by trekker25
thorbj wrote:
G3rard wrote:
trekker25 wrote:http://172.16.0.3/sonos/index.php?zone= ... &volume=80
try something like this.

The index.php demands to have a GET variable called zone.
Indeed and I would start with just the play command, because in my setup playing messages was not so easy (due to config I had to do).

Code: Select all

http://<ip>/sonos/index.php?zone=Stue&action=Play
Ah, so I need to issue commands with the index.php address? I will try playing with that later when I'm home again :) Tank you!!
Play

http://yourserver/sonos/index.php?zone= ... ction=Play
Stop

http://yourserver/sonos/index.php?zone= ... ction=Stop
Toggle between play and stop

http://yourserver/sonos/index.php?zone= ... lePlayStop
Volume up / down

http://yourserver/sonos/index.php?zone= ... n=VolumeUp
http://yourserver/sonos/index.php?zone= ... VolumeDown
Mute

http://yourserver/sonos/index.php?zone= ... ction=Mute
Next radio from list

http://yourserver/sonos/index.php?zone= ... =nextRadio
Play message

http://yourserver/sonos/index.php?zone= ... &volume=20


The Google TTS service doesn't work anymore gives me HTTP 404 errors?? But i just downloaded my own doorbell mp3. You need to name it 0.mp3 (and use messageID1) and place it in the correct locations.

Seems every message had a 0 status (start action) and a 1 status (action stopped). For a doorbell you just need 0.mp3 and messageId1

Re: Easy Sonos

Posted: Monday 21 December 2015 11:35
by wmn79
I have created an overview of this topic and some other Sonos related topics on this page on the wiki: http://www.domoticz.com/wiki/Sonos
Feel free to add info or improve it so we have one place with all Sonos related information.

Re: Easy Sonos

Posted: Wednesday 30 December 2015 10:55
by wmn79
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;
?>
I am using this script and it is working nicely, I only found out that when two Sonos devices are grouped and you pause the group. Only the initial device, so the device of which the playlist is used gets turned off. The other device still keeps the status "Playing" and therefore the virtual switch is not switching off, does someone else also see this behaviour?

Re: Easy Sonos

Posted: Sunday 17 January 2016 19:21
by HansLe
Hi, I've modified the stop and play scripts. You can use these scripts with an IP as parameter. These scripts also writes a line in the log file.

Place the scripts in: /home/pi/domoticz/scripts
Make the scipts executable: chmod +x <filename>

You can start them via a switch.
sonos-switch.JPG
sonos-switch.JPG (53.05 KiB) Viewed 38193 times

Code: Select all

#!/usr/bin/php
<?php
// Play SONOS device with given IP adres
//  f.e. in Domoticz switch enter by on action : script:///home/pi/domoticz/scripts/sonos.play.php 172.16.0.11
//
//  Uses curl. Maybe you havbe to install curl: sudo apt-get install php5-curl
//
require ("PHPSonos.inc.php");  
// check number of arguments; 1 is always the php filename
if ($argc < 2 ) {
	$msg = "SONOS%20play,%20no%20IP%20as%20argument!" ;  // change spaces to %20!
}
else {
	$ip = $argv[1];
	$sonos = new PHPSonos($ip);     // no check on valid IP address!
	$sonos = $sonos->Play();
	$msg = "SONOS%20$ip%20started";   // changes spaces to %20!
}
//
// Now write $msg to the Domoticz log file via json
//
// After http:// enter your Domoticz server url
//
$url = "http://xxx.xx.x.xx:pppp/json.htm?type=command&param=addlogmessage&message=$msg"; 
// open connection
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE);
// execute post
$result = curl_exec($ch);
// close connection
curl_close($ch);
?>

Code: Select all

#!/usr/bin/php
<?php
// Stop SONOS device with given IP adres
//  f.e. in Domoticz switch enter by off action : script:///home/pi/domoticz/scripts/sonos.stop.php 172.16.0.11
//
//  Uses curl. Maybe you havbe to install curl: sudo apt-get install php5-curl
//
require ("PHPSonos.inc.php");  
// check number of arguments; 1 is always the php filename
if ($argc < 2 ) {
	$msg = "SONOS%20stop,%20no%20IP%20as%20argument!" ;  // change spaces to %20!
}
else {
	$ip = $argv[1];
	$sonos = new PHPSonos("$ip");     // no check on valid IP address!
	$sonos = $sonos->Stop();
	$msg = "SONOS%20$ip%20stopped";   // changes spaces to %20!
}
//
// Now write $msg to the Domoticz log file via json
//
// After http:// enter your Domoticz server url
//
$url = "http://xxx.xx.x.xx:pppp/json.htm?type=command&param=addlogmessage&message=$msg"; 
// open connection
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE);
// execute post
$result = curl_exec($ch);
// close connection
curl_close($ch);
?>

Re: Easy Sonos

Posted: Monday 18 January 2016 22:33
by HansLe
Hi, a developed an alarm script for the Sonos. You can use these scripts with an IP as parameter. This scripts also writes a line in the log file.

Place the scripts in: /home/pi/domoticz/scripts
Make the scipts executable: chmod +x <filename>

You can download free sounds on the web. Save one of them to f.e. as alarm.mp3

A webdirectory on the RPI is necessary. See the attachement for a example
sonos-alarm.JPG
sonos-alarm.JPG (23.14 KiB) Viewed 38156 times
xxx.xx.x.xx in the script is the IP address of your RPI (Domoticz). pppp the port number.

Code: Select all

#!/usr/bin/php
<?php
// Play given MP3 file at a SONOS device with given IP adres
//  f.e. in Domoticz ALARM switch enter by on action : script:///home/pi/domoticz/scripts/sonos.alarm.php 172.16.0.11
//                   enter by off action f.e.  my stop script (see other post)
//
//  Uses a webserver directory; define /var/www/sonos and /var/www/sonos/spraak
//  Uses curl. Maybe you havbe to install curl: sudo apt-get install php5-curl
//
$volume = 50;   					// define your own volume, 100 is max.
$url = 'http://xxx.xx.x.xx/sonos/spraak/alarm.mp3';  	// define your own mp3 file; dont forget to copy it to the directory

//
require ("PHPSonos.inc.php");
// check number of arguments; 1 is always the php filename
if ($argc < 2 ) {    
	$msg = "SONOS%20play,%20no%20IP%20as%20argument!" ;  // change spaces to %20!
	exit;
}
else {
	$ip = $argv[1];
	$sonos = new PHPSonos($ip);     // no check on valid IP address!
	$msg = "SONOS%20$ip%20alarm%20started";   // changes spaces to %20!
//
// save informations
//
	$saveVolume	  = $sonos->GetVolume();
	$savePositionInfo = $sonos->GetPositionInfo();
	$saveMediaInfo    = $sonos->GetMediaInfo();
	$radio     	  = (strpos($saveMediaInfo['CurrentURI'], "x-")) !== false; //playlist in php starts with x-rincon-mp3radio
	$oldti		  = $sonos->GetTransportInfo();
//
// set AVT to message
//
	$sonos->SetVolume($volume);
	$sonos->SetAVTransportURI($url);
	$sonos->Play();
//
//	wait until message is done
//
	while ($sonos->GetTransportInfo() == '' || $sonos->GetTransportInfo() == 1)
	    {
	        sleep(0);    // dont spent time
	    }
//
// set old queue
//
	if ($radio) {
		$sonos->SetRadio($saveMediaInfo['CurrentURI'], $saveMediaInfo['title']);
		 }
	else {
		$sonos->SetAVTransportURI($saveMediaInfo['CurrentURI'], $saveMediaInfo['CurrentURIMetaData']);
		 }
	try {
// seek TRACK_NR
        	 $sonos->Seek("TRACK_NR", $savePositionInfo['Track']);
// seek REL_TIME
	         $sonos->Seek("REL_TIME", $savePositionInfo['RelTime']);
       	}
	catch (Exception $e) {
       	}
//
// play if previous list was playing
	if ($oldti == 1)
	       {
		 $sonos->Play();
		 }

	$sonos = $sonos->SetVolume($saveVolume);
//
}
//
// Now write $msg to the Domoticz log file via json
//
// After http:// enter your Domoticz server url
//
$url = "http://xxx.xx.x.xx:pppp/json.htm?type=command&param=addlogmessage&message=$msg"; 
// open connection
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE);
// execute post
$result = curl_exec($ch);
// close connection
curl_close($ch);
?>

Re: Easy Sonos

Posted: Monday 18 January 2016 22:38
by geezerrr
Cool!! I put on my to do list