GioExperience
IoT, computer technology and other things
Not hobby, real stuff



Proudly made by myself
Welcome, my name is Giovanni, a 35 years old italian engineer that would like share his experience in IoT, electronic, raspberry, arduino, electrotechnic, and others things. In this blog i will try to realize a different approch to these stuff. It’s not a hobby or a toys…. I will try to explain my practical, pragmatical and REAL approch for building REAL, SAFE, and RELIABLE utilities.
all posts



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/\" title="https://ifttt.com/\" target="_blank">https://ifttt.com/\" target=\"_blank\" rel=\"noopener\" style=\"box-sizing: border-box; border: 0px; font-variant: inherit; font-stretch: inherit; line-height: inherit; font-family: inherit; font-style: inherit; font-weight: inherit; outline: 0px; vertical-align: baseline; color: rgb(38, 38, 38);\">https://ifttt.com/ and my Assistant Silvano (on RPI3) (look http://www.gioexperience.com/the-silvano-idea-the-beginning/index.html\" target=\"_blank\" rel=\"noopener\" style=\"box-sizing: border-box; border: 0px; font-variant: inherit; font-stretch: inherit; line-height: inherit; font-family: inherit; font-style: inherit; font-weight: inherit; outline: 0px; vertical-align: baseline; color: rgb(38, 38, 38);\">this post and http://www.gioexperience.com/1-05-a-quick-webapp-for-your-thermostat/index.html\" target=\"_blank\" rel=\"noopener\" style=\"box-sizing: border-box; border: 0px; font-variant: inherit; font-stretch: inherit; line-height: inherit; font-family: inherit; font-style: inherit; font-weight: inherit; outline: 0px; vertical-align: baseline; color: rgb(38, 38, 38);\">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 http://www.gioexperience.com/google-home-hack-hidden-rest-api/index.html\" target=\"_blank\" rel=\"noopener\" style=\"box-sizing: border-box; border: 0px; font-variant: inherit; font-stretch: inherit; line-height: inherit; font-family: inherit; font-style: inherit; font-weight: inherit; outline: 0px; vertical-align: baseline; color: rgb(38, 38, 38);\">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 http://www.gioexperience.com/initially-setup-your-raspberrypi/index.html\" target=\"_blank\" rel=\"noopener\" style=\"box-sizing: border-box; border: 0px; font-variant: inherit; font-stretch: inherit; line-height: inherit; font-family: inherit; font-style: inherit; font-weight: inherit; outline: 0px; vertical-align: baseline; color: rgb(38, 38, 38);\">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….