I changed the script a little bit to use google translate as TTS. Also used tasks-spooler to get multiple commands at about the same time queued, so nothing is lost. The original script is not working fine when executed when the first execution is still running.
Jingles is gone but I think you can add this by yourself again.
I use Ubuntu.
Install taskspooler: sudo apt install task-spooler
Install
https://github.com/pndurette/gTTS : pip install gTTS (not needed if you don't speak more then 150 characters)
Put the following scripts in /home/john/stream2chromecast:
(replace john with your user)
notification_chromecast_batch.sh
Code: Select all
#!/bin/bash
message=$1 # text message
volume=$2 # 0=auto 0.1=10% 1=100%
# ------ parameters ---------
# number of arg correction
case "$#" in
"1")
;;
"2")
;;
*)
echo "Usage: notification_chromecast_batch.sh <text> [volume 0..1]"
exit 1
;;
esac
# queue commands ; don't run at the same time twice
# run under user john otherwise python module cannot be found: gtts-cli
sudo -u john tsp -n bash -c "/home/john/stream2chromecast/notification_chromecast.sh \"$1\" $2 > /tmp/out 2>&1"
notification_chromecast.sh:
Code: Select all
#!/bin/bash
# notification_google_home.sh Script de notification de message vocal sur la Google Home
# by JS Martin - 11/02/2018 - version 0.1
# changed by glsf91
message=$1 # text message
volume=$2 # 0=auto 0.1=10% 1=100%
jingle=$3 # jingle track number (0=no track 1=default track)
# ------ parameters ---------
# Autoset volume if volume=0
Start_day="0700"
Start_night="2200"
Night_vol="0.2"
Day_vol="0.7"
# number of arg correction
case "$#" in
"1")
volume="0"
jingle="1"
;;
"2")
jingle="1"
;;
*)
echo "Usage: notification_google_home <text> [volume 0..1]"
exit 1
;;
esac
# IP Google Home
IPGH="192.168.1.173"
echo "Notification : "$message
if [ $volume != "0" ]; then
echo "Volume : "$volume
else
CUR_TIME=`date +%H%M`
if [ $CUR_TIME -ge $Start_day -a $CUR_TIME -le $Start_night ]; then
echo "Day volume"
volume=$Day_vol
else
echo "Night volume"
volume=$Night_vol
fi
echo "Volume = automatique - set to "$volume
fi
# Exit if chromecast not online
ping -c1 -W 2 $IPGH >/dev/null
if [ $? -eq 0 ]
then echo "Chromecast online"
else echo "Chromecast offline"
exit 0
fi
if [ -f /tmp/message.mp3 ]
then
rm /tmp/message.mp3
fi
#Text to MP3; naturalreaders
#curl -s -G "http://api.naturalreaders.com/v4/tts/macspeak" --data "apikey=<apikey>&src=pw&r=22&s=1" --data-urlencode "t=$message" -o /tmp/message.mp3
# use google translate
if [ ${#message} -lt 150 ]
then
curl -s -G "http://translate.google.com/translate_tts" --data "ie=UTF-8&total=1&idx=0&client=tw-ob&&tl=nl-NL" --data-urlencode "q=$message" -A "Mozilla" --compressed -o /tmp/message.mp3
else
/home/john/.local/bin/gtts-cli "$message" -l 'nl' -o /tmp/message.mp3
fi
if [ -f /tmp/message.mp3 ]
then
#Set Google Home volume
sudo python /home/john/stream2chromecast/stream2chromecast/stream2chromecast.py -devicename $IPGH -setvol $volume
#MP3 to Google Home
sudo python /home/john/stream2chromecast/stream2chromecast/stream2chromecast.py -devicename $IPGH /tmp/message.mp3
fi
if [ -f /tmp/message.mp3 ]
then
rm /tmp/message.mp3
fi
I have installed stream2chromecast in /home/john/stream2chromecast/stream2chromecast. Adjust the path if necessary.
Also change the ip address of your chromecast device.
Use in Domotizc:
Code: Select all
os.execute('/home/john/stream2chromecast/notification_chromecast_batch.sh "'..sentence..'" &')
sentence is the variable with text.
You can also use naturalreaders.com instead of google. Language is dutch now (change the r=22). There is a limit of 20 minutes per day if using dutch. Only 1 english language is free above 20 minutes.