Re: Easy Sonos
Posted: Tuesday 21 July 2015 9:30
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!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://.
I found the solution to this, somehow I had downloaded the html of the github, instead of the sonos.class.phpG3rard 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'm having difficulties getting this to work.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 have added my sonos speakers IP adresses to the config.php.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
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',
);
?>
my first guess indeed. as you need to send a message/command to 1 zone.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!G3rard wrote:First try it with one zone in the config.php file or otherwise give the 3 zones a different name.
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',
);
?>
are you running it on your Pi? That doesnt work if you haven't installed a seperate webserver.thorbj wrote:Thanks for the reply!G3rard wrote:First try it with one zone in the config.php file or otherwise give the 3 zones a different name.
I have now tried with only one zone, but I still get the same error.
Here is the edited config.php:
- Thanks!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', ); ?>
maybe a chmod issue?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.
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).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.
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!!G3rard wrote: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).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.Code: Select all
http://<ip>/sonos/index.php?zone=Stue&action=Play
Playthorbj wrote: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!!G3rard wrote: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).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.Code: Select all
http://<ip>/sonos/index.php?zone=Stue&action=Play
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?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 scriptPHP pageCode: 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
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; ?>
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¶m=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¶m=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
// 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¶m=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);
?>