Updated Sonos TTS (PHP)

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

Moderator: leecollings

Post Reply
NoeXie
Posts: 2
Joined: Saturday 07 November 2015 0:58
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Updated Sonos TTS (PHP)

Post by NoeXie »

For everyone interested, i updated the original Sonos class to work with TTS again. Since Google is blocking API Calls we can't use that anymore. I found an alternative service with 350 free requests a day. You need a Account and API key from: http://www.voicerss.org/

I got the update code posted on GitLab:
https://gitlab.com/J.Tocino/sonos-tts/tree/master

Readme must be helping with the setup:
https://gitlab.com/J.Tocino/sonos-tts/t ... ter#README

Got mine working now! I made a simple script that 'speaks' the weather of that day once i walk into my living room for the first time in the day. It is using Domoticz for the motion sensor. You can also make webcontrols for your Sonos with this script.

Image
rtna
Posts: 17
Joined: Monday 06 April 2015 12:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Updated Sonos TTS (PHP)

Post by rtna »

Sounds good! I will give it a try later this week and report back in this topic.
rtna
Posts: 17
Joined: Monday 06 April 2015 12:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Updated Sonos TTS (PHP)

Post by rtna »

I made a simple script that 'speaks' the weather of that day
Can you share this script?
User avatar
G3rard
Posts: 669
Joined: Wednesday 04 March 2015 22:15
Target OS: -
Domoticz version: No
Location: The Netherlands
Contact:

Re: Updated Sonos TTS (PHP)

Post by G3rard »

Thanks for sharing. Great to see some more Sonos options for Domoticz.

I have also changed from Google to voicerrs.org in the PHP scripts that I am using. I have posted these on Github https://github.com/gerard33/sonos.

When playing a TTS, this PHP stops the current playing radio/list, plays the message and then continues the previously played radio/list.
I also tried the PHP source that you have used, but I believe the stopping and continuing is not supported?

I have made a TTS function which can be used in LUA scripts, maybe it is useful for someone. I use the IDX number of the Sonos switch in Domoticz in the config file (config.php), so I can use this on the frontpage which sends commands based on the IDX.

Code: Select all

function PlayTTS(text, zone, volume)

--Sonos setting
sonoskantoor = 115
sonoskeuken = 116
vol_def = 4
zonetext = "onbekend"

	--example: PlayTTS('Hier de tekst', 'kantoor', 4)
	
	if (text == nil or text == "") then text = 'geen tekst ingevoerd' end
	if (zone == nil or zone == "") then zone = sonoskantoor end					--kantoor
	if (zone == 'kantoor' or zone == 'Kantoor') then zone = sonoskantoor end	        --kantoor
	if (zone == 'keuken' or zone == 'Keuken') then zone = sonoskeuken end		        --keuken
	if (volume == nil or volume == "") then volume = vol_def end
	
	if zone == sonoskantoor then zonetext = 'Kantoor' end						--replace number to text for print
	if zone == sonoskeuken then zonetext = 'Keuken' end						--replace number to text for print
	if zone == 'all' or zone == 'All' then zonetext = 'Kantoor en Keuken' end	                --play on all known Sonos boxes
	print('PlayTTS - Tekst: ' .. text .. ' | Zone: ' .. zonetext .. ' | Volume: ' .. volume)
	
	text = string.lower(text)											--lowercase needed for playing TTS
	text = string.gsub(text, "%s+", '%%20')									--replace spaces with %20 needed for playing TTS with this function
	
	if (zone == 'all') then
		local play = 'curl --url "http://<yourip>/sonos/index.php?zone=' .. sonoskantoor .. '&action=sendMessage&message=' .. text .. '&volume=' .. volume .. '" &'
		os.execute(play)
		local play2 = 'curl --url "http://<yourip>/sonos/index.php?zone=' .. sonoskeuken .. '&action=sendMessage&message=' .. text .. '&volume=' .. volume .. '" &'
		os.execute(play2)
	else
		local play = 'curl --url "http://<yourip>/sonos/index.php?zone=' .. zone .. '&action=sendMessage&message=' .. text .. '&volume=' .. volume .. '" &'
		os.execute(play)
	end

end
Not using Domoticz anymore
NoeXie
Posts: 2
Joined: Saturday 07 November 2015 0:58
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Updated Sonos TTS (PHP)

Post by NoeXie »

rtna wrote:Sounds good! I will give it a try later this week and report back in this topic.
Let me know please!
rtna wrote:Can you share this script?
When i have some time i can make a more usable version. It works with cronjobs and stuff right now, not really noob friendly :lol:
G3rard wrote:Thanks for sharing. Great to see some more Sonos options for Domoticz. I have also changed from Google to voicerrs.org in the PHP scripts that I am using. I have posted these on Github https://github.com/gerard33/sonos. When playing a TTS, this PHP stops the current playing radio/list, plays the message and then continues the previously played radio/list. I also tried the PHP source that you have used, but I believe the stopping and continuing is not supported?
Thank you, nice PHP script! Mine has some basic functions to play or stop the active queue but nothing special. Great idea to play the queue afterwards. Thanks for sharing the LUA script btw!
User avatar
G3rard
Posts: 669
Joined: Wednesday 04 March 2015 22:15
Target OS: -
Domoticz version: No
Location: The Netherlands
Contact:

Re: Updated Sonos TTS (PHP)

Post by G3rard »

Another script that I am using is a LUA time script which checks if the Sonos is still on/off so the status in Domoticz can be updated. If you switch the Sonos on/off with the button on the Sonos, the status in Domoticz is updated with this script.

You need to use the PHP files from https://github.com/gerard33/sonos and the following files which still needs to be edited (path, names etc).

script_time_sonos.lua

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 /<your path>/sonos/domo_status.php','r')
--print(str)

--aan='<b style="color:green">aan</b>'
--uit='<b style="color:orange">uit</b>'
aan="<font color='forestgreen'>aan</font>"
uit="<font color='sandybrown'>uit</font>"

if string.find(str, "Kantoor: aan") 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(str, "Keuken: aan") 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
domo_status.php

Code: Select all

<?php
$kantoor = file_get_contents("http://<your ip>/sonos/index.php?zone=<your zone>&action=GetTransportInfo", "r");
if ($kantoor == 1) { #1=Playing, 2=Pause, 3=Stopped
	echo "Kantoor: aan";
} else {
	echo "Kantoor: uit";
}
#echo $kantoor;
echo PHP_EOL;
$keuken = file_get_contents("http://<your ip>/sonos/index.php?zone=<your zone>&action=GetTransportInfo", "r");
if ($keuken == 1) { #1=Playing, 2=Pause, 3=Stopped
	echo "Keuken: aan";
} else {
	echo "Keuken: uit";
}
#echo $keuken;
?>
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: Updated Sonos TTS (PHP)

Post by G3rard »

NoeXie wrote:For everyone interested, i updated the original Sonos class to work with TTS again. Since Google is blocking API Calls we can't use that anymore. I found an alternative service with 350 free requests a day. You need a Account and API key from: http://www.voicerss.org/

I got the update code posted on GitLab:
https://gitlab.com/J.Tocino/sonos-tts/tree/master

Readme must be helping with the setup:
https://gitlab.com/J.Tocino/sonos-tts/t ... ter#README

Got mine working now! I made a simple script that 'speaks' the weather of that day once i walk into my living room for the first time in the day. It is using Domoticz for the motion sensor. You can also make webcontrols for your Sonos with this script.

Image
Can you share your dashboard code? I would like to reuse some parts.
Not using Domoticz anymore
frizby66
Posts: 33
Joined: Friday 13 November 2015 17:36
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Updated Sonos TTS (PHP)

Post by frizby66 »

Sorry, am a bit of a novice on PHP.

Have Domoticz running on a Pi2, am also running Homebridge on the PI and using domoticz.js to do voice control of lighting via home kit and it works well.

My key goal is to have the weather forecast for Edinburgh played each morning at a set time in my house (have wunderground virtual weather enabled in Domoticz).

Am 99% sure it is possible by reading various other posts on the site but am struggling to join the dots before making a start (don't want to kill my working system).

Is there a straight forward wiki I can follow?

Questions I have are:-

Should I or can I run PHP on the PI2 along with Domoticz & Homebridge?
How do I get the forecast from wunderground to TTS to Sonos (assuming I get Sonos running)?

Any help would be appreciated :-)
--------------------
Raspberry Pi2, RTX433e USB, Philips Hue, Nest, LightwaveRF.
HansLe
Posts: 25
Joined: Wednesday 13 January 2016 21:12
Target OS: Raspberry Pi / ODroid
Domoticz version: V2.3530
Location: Netherlands
Contact:

Re: Updated Sonos TTS (PHP)

Post by HansLe »

Hi, I tried to approach my Sonos system via this option. And to be honest. It works great on a windows system!!!!

I installed XAMPP before so the webserver and PHP were available. The basis functions play and stop are working, TTS to go. I hope you can help me with TTS. I'm struggling with these two lines:
'messagePath' => '//<yourpath>/sonos/spraak/',
'messageStorePath' => '//<yourpath>/sonos/spraak/',
I tried several options but I don't know what to enter?!?!?

I have a localhost with a webdirectory sonos.
I have a directory D:\xampp\htdocs\sonos\spraak with a MP3 file, 1.mp3. I've also defined this directory as a music libary in my sonos system.

==== Edit ====

I discovered wich path to use. Start SONOS on your PC. Via "Selecteer een muziekbron" , select "Muziekbiblioteek", select "mappen" and then you see the names. For exampe "//MYPC/spraak". Use as path "//MYPC/spraak".
Raspberry Pi B+ | Domoticz v3.4834 | RFXtrx433E | Action sockets and remote control (a cheap solution to start with)
Sonos device: control via Action remote control and EasySonos (via Domoticz scripts)
2 x Chresta TX320, Kaku sensoren, D-link camera
pollefinario
Posts: 21
Joined: Wednesday 10 February 2016 12:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Updated Sonos TTS (PHP)

Post by pollefinario »

I've now also included the PHP tool to get TTS support. Got it to only after figuring out that "special characters" don't get passed thru the song for sonos. Any tips?
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests