Google Home Hack, send voice programmaticaly with Python

In this post we can see How to send voice command to Google Home without human interactation. Programaticaly you can send voice command to your Google Home or Google Home mini (you have to know the IP), and its speak. I use this system with IFTTT https://ifttt.com/ and my Assistant Silvano (on RPI3) (look this post and other post) for know home temperature and other things.

Install python3 on your RPI3

python3 is installed by default on Raspian, but if you have not…

sudo apt-get install python3

now install pip3 and other packages we need

sudo apt-get install python3-pip

pip3 install pychromecast
pip3 install gtts

Control Google Home volume

with this simple script you can control volume on your Google Home. You can use this as Web API…
For use this script you need to know the ip of your google home. You can read this post.

#! /usr/bin/python3.5

#
# change volume on GoogleHome, or read the current volume
#
# use for setting: ./ghome_volume [ghome_ip] [volume_from_0_to_100]
# use for reading: ./ghome_volume [ghome_ip]
#
#

import sys
import pychromecast

ip=sys.argv[1];
vol=-1;

try:
   vol=sys.argv[2]; #from 0 to 100
except:
   pass

castdevice = pychromecast.Chromecast(ip)
castdevice.wait()

if vol==-1 :
   vol_prec=castdevice.status.volume_level
   print(round(vol_prec*100))
else :
   castdevice.set_volume(float(vol)/100)

#*************

Put the executive bit to 1 (chmod +x scriptname) and copy this file on /usr/bin/

Send command voice to your Google Home

Now i write a script that send text string, and this text string is transformed in voice to your google home. Before do that, there is a couple of things.
We use Google Home like a chromecast audio… like the spotify app, or youtube app. For play a mp3 on chromecast, you have to set a URL with the resource (the mp3). For doing that I use apache on my RPI3 (read this post for first configuration). The mp3 will be created by the GTTS package that use Google Translate and saved (cached) in /www/mp3_cache. Obviously the RPI3 with the apache is the same where we call this script.

Now, let’s go

#! /usr/bin/python3.5

#
# spoke something on GoogleHome
#
# use: ./ghome_say [ghome_ip] [text_to_say]
#
#

import sys
import pychromecast
import os
import os.path
from gtts import gTTS
import time
import hashlib

ip=sys.argv[1];
say=sys.argv[2];

#********* retrieve local ip of my rpi3
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
local_ip=s.getsockname()[0]
s.close()
#**********************

fname=hashlib.md5(say.encode()).hexdigest()+".mp3"; #create md5 filename for caching

castdevice = pychromecast.Chromecast(ip)
castdevice.wait()
vol_prec=castdevice.status.volume_level
castdevice.set_volume(0.0) #set volume 0 for not hear the BEEEP

try:
   os.mkdir("/var/www/html/mp3_cache/")
except:
   pass

if not os.path.isfile("/var/www/html/mp3_cache/"+fname):
   tts = gTTS(say,lang='it')
   tts.save("/var/www/html/mp3_cache/"+fname)

mc = castdevice.media_controller
mc.play_media("http://"+local_ip+"/mp3_cache/"+fname, "audio/mp3")

mc.block_until_active()

mc.pause() #prepare audio and pause...

time.sleep(1)
castdevice.set_volume(vol_prec) #setting volume to precedent value
time.sleep(0.2)

mc.play() #play the mp3

while not mc.status.player_is_idle:
   time.sleep(0.5)

mc.stop()

castdevice.quit_app()

#************************************

Put the executive bit to 1 (chmod +x scriptname) and copy this file on /usr/bin/

 

Now we can use it like a Web Api or API REST… or using IFTTT

In my case I create on IFTTT an application the use Google Assistant and WebHook. The spoke question are “what’s the temperature at home?”, and it call a WebHook on my RPI3. My RPI3 read the temperature and use the script for sending response to google home. Look this article for more information by my project on temperature at home.

<?php
......

exec("./ghome_say 192.168.1.101 Temperature is $t");

.....
?>

I use it also for have feedback when switch-on my stove. Yes, i switch-on my stove with voice….

Please follow and like us:

8 thoughts on “Google Home Hack, send voice programmaticaly with Python

  • 19/01/2019 at 04:31
    Permalink

    Thanks for this. I was looking for something like this so that I could ask google to tell me what my charcoal grill/smoker temperature is as reported by the arduino/pi zero controller I built for it. This will work but it’s to cold to do much grilling right now so no real world testing yet. I hooked up a nodeMCU board with a magnetic switch attached to monitor my garage door. Now I can say “hey google garage” and with the help of IFTT it will speak back “The garage door is open/closed”.

    Something I found was that on my Pi 2 this is fairly slow to build the mp3 of each word of the response. To speed things up I used the TTS to generate all the possible words needed so they are in the cache. I have the python script concatenate needed words into a single mp3 that can be played for the response. This sounds less choppy.

    I can make this work with the Mini’s I have but not my Google Home Hub. It plays the announcement tone but not the message.

    Reply
  • Giovanni
    19/01/2019 at 23:02
    Permalink

    Hi, thanks for the comment. I don’t know why with Google Hub not works… : (. You can try to execute a downloaded mp3, instead of create gtts mp3.

    Reply
  • 02/06/2019 at 22:54
    Permalink

    Hi, good work¡¡¡¡ I’ve tried many times with this script, I heard the beep at the Google Home Mini before de message but nothing more. The script creates the *.mp3 good, but I can’t hear nothing. I’ve tried directly to my Chromecast, the TV shows the sound bar of Chromecast but I can hear nothing. Can you help me?, thanks

    Reply
    • Giovanni
      07/06/2019 at 21:30
      Permalink

      Hi, i re-tryed my script exactly in the mode i wrote and for me works. I tried it on a old laptop with Ubuntu 18.04 LTS and a Google Mini. If you hear the “Beep”, it means that you have commented the line “castdevice.set_volume(0.0)” right? This is ok… but make sure that the volume are >0.5 every time… I told you this because for me, sometime, the volume go to 0.0. You can try using this line “castdevice.set_volume(0.5)” and don’t touch the volume in other line.

      Reply
  • 13/07/2019 at 21:35
    Permalink

    Valuable information. Fortunate me I discovered уour websіte by
    cһance, and I ɑm stunned why this twist of fate didn’t happened earlier!
    I bookmarkeⅾ it.

    Reply
  • 09/09/2019 at 11:57
    Permalink

    Hi, very good solution. I’m using that with Google Home Mini and Raspberry Pi 3+, it works good al lot of times but sometimes the scripts stops waiting something and nothing happens and I don’t know why ¿do you know where could be the problem or what I can to try? thanks a lot

    Reply
  • 21/09/2019 at 13:46
    Permalink

    Hey ! You saved my day, and probably more ! Many, many thanks 🙂

    Reply
  • 05/10/2019 at 09:42
    Permalink

    Hi,

    This is perfect for announcing phone calls that I wrote a script for.

    Apart from one thing, my home seems to be broadcasting in Portuguese, i’d very much like it to be in English 😉

    any idea where I can change that?

    many many thanks for your work.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fork me on GitHub