Google's Artificial Intelligent Assistant comes to the Raspberry Pi 3 with the AIY Projects Voice Hat. Now you can control your GPIO projects with Voice controls.
With issue 57 of The MagPi magazine a Voice HAT was given away that uses Googles AIY Projects service to bring voice controls to Raspbery Pi Projects. The Kit contained a GPIO breakout circuit board, microphone board, a speaker, a button, led and connectors. Once the kit is built and the Google Assistant API has been setup using a Google Account on the Google API Console you can press the button and ask a question to receive and answer. In the same way OK Google, Apples Siri or Amazon's Echo works.
The setup uses python scripts to interact with the google software which is where the Voice HAT's various connections come into use.
Using Python you can setup voice commands to control devices, sensors, servos, LED's etc connected to the HAT.
The Raspberry PI's GPIO port has been broken out across the board so you can use any project you currently have using the GPIO connectors including I2C, SPI and UART.
The guide in The MagPi and the guide at aiyprojects.withgoogle.com/voice/ use a Raspbian OS image with the Google Assistant SDK setup which needs to be written to a new SD card before you start.
Though once you know what you are doing it could be setup on an existing Raspbian image.
With the setup there are several python scripts that can be used to get voice commands to control GPIO connected devices. There is also a guide in The Magpi but the action.py script warns that there is an error in that guide so use the info in the script.
Google API Console
As part of the setup you will need to create the API's to use the google software at console.cloud.google.com. The Voice HAT can use the google Assistant API or the Speech API. The Speech API is more featured and has to have a billing account setup because if you use it for more than 60 minutes a month you will be charged for each 15 seconds at $0.0016.
If you just use the Assistant API you will not need a billing account as there will be no charges and you can still create your own commands. So it's best not to create a Speech API unless you wish to use those features. When you use your own commands with the Assistant API the voice is not so realistic. With the Speech Api the voice will be better
On the Desktop is an icon to test Cloude Services. This uses the Speech Api and will fail if no billing has been set up, so again ignore this if you just use the Assistant API.
Android Things is a new Operating system designed for connected devices and is fully compatible with the AIY Projects and other google services for development. This can be used as an alternative OS to Raspbian. developer.android.com/things/hardware/raspberrypi.html
I missed the Voice HAT give away but like to buy one
Where can I buy a Voice HAT kit? If you didn't manage to get a copy of The Magpi with the free AIY Voice HAT kit you can still get your hands on one. At the time of writing they are not for sale yet but you can add your name to a mailing list to be notified when they are available for sale. Buy a AIY Projects Voice HAT for the Raspberry Pi
Well I need to go and try a few things out with this kit and see what projects I can make use of. So I will add to this as I get use to it.
in the meantime googles site has some additional information on how to use it and command options.
AIY Voice HAT GPIO Pin Positions
This is the GPIO pin positions. The 'AIY Project Voice Kit' link above also contains a GPIO pin diagram but there is a mistake as it has two 09 pins. The one for SPI-MOSI should be 10.
The Voice Hat is intended to be used with the RPI3 as it is the fastest Pi but I decided to give it a go with a Raspberry Pi Zero W and it works fine. It is a bit slower than a RPi3 to get a response but it does seem to work fine.
See a the video on YouTube Here
Add Voice command to Shutdown or Reboot
As there is no way to shut the Raspberry Pi down if you are not using ssh or a display I have added Shutdown and Reboot commands to the action.py script.
The file is in the src folder and is used to add your own commands.
The full path is /home/pi/voice-recognizer-raspi/src/action.py
Toward the bottom of the file is were you can add your own commands and GPIO controls.
for the shutdown and reboot commands locate the banner:
# Makers! Add your own voice commands here.
and add the following commands directly below the banner but above the command "return actor"
actor.add_keyword(_('system reboot'),SpeakShellCommandOutput(say, "sudo reboot",_("Reboot Failed")))
actor.add_keyword(_('system shutdown'),SpeakShellCommandOutput(say,"sudo shutdown -h now",_("Shutdown Failed")))
and save the file.
Now when you say the commands "system shutdown" or "system reboot" you will get exacly that.
Shutdown command with feedback
The above version will quietly shut the system down. If you use a class it will respond to your command.
So under the banner
# Makers! Implement your own actions here.
add the OS library with
then add the following class
class systemoff(object): """System shutdown and reboot""" def __init__(self,say,command,speak): self.say = say self.command = command self.speak=speak def run(self,cmd): if self.command=="reboot": self.say(self.speak) os.system("sudo shutdown -r now") elif self.command=="shutdown": self.say(self.speak) os.system("sudo shutdown -h now")
Now at the bottom of the file under # Makers! Add your own voice commands here, enter:
actor.add_keyword(_('system reboot'),systemoff(say,'reboot','Rebooting, be back soon')) actor.add_keyword(_('system shutdown'),systemoff(say,'shutdown','Shutting down, goodbye'))
now when you say "system shutdown" it will respond with "Shutting Down GoodBye" and with "system reboot" it responds with "Rebooting be back soon"
Get Readings from a Temperature Sensor
I have a Temperature sensor that uses the 1 wire device connection on GPIO 4. The Voice HAT has a bank of connectors under the heading Drivers. Driver 0 uses GPIO 4. If you haven't already you will need to solder the header pins to the Voice HAT using the strip of pins in the kit.
I have connected the 1 wire temperature sensor to driver 0 and then edited the /home/pi/voice-recognizer-raspi/src/action.py file with the script below.
Under the banner
# Makers! Implement your own actions here.
I added the following code
import os import glob import time def localtemp(): """get reading from 1 wire temperature sensor""" os.system('modprobe w1-gpio') os.system('modprobe w1-therm') base_dir = '/sys/bus/w1/devices/' dev_folder = glob.glob(base_dir + '28*') dev_file = dev_folder + '/w1_slave' f = open(dev_file,'r') lines = f.readlines() f.close() while lines.strip()[-3:] != 'YES': time.sleep(0.2) lines = ReadTemp() pos = lines.find('t=') if pos != -1: temp_str = lines[pos+2:] temp_c = float(temp_str)/ 1000 result = str(int(temp_c)) + "degrees centigrade" return result
then under the next banner
# ========================================= # Makers! Add your own voice commands here. # =========================================
I have added the following code
actor.add_keyword('local temperature', SpeakAction(say,localtemp()))
Now when you say "local temperature" it will speak the sensors readings
To test the code stop the background service and run main.py manually so you can see if there are any errors such as tab/space formatting.
sudo systemctl stop voice-recognizer
then run main.py also in src folder.
If your test goes well exit the script with CTRL+c and activate the background service again
sudo systemctl start voice-recognizer
(note if you remove the Temperature sensor the voice hat will error and not accept commands. I will update this soon to add error checking. I suggest you make a backup of the action.py script before you try this out)