Make a Google AIY Assistant without the kit on the Raspberry Pi 2 and 3

In mid-2017 Google released a kit for use with the Raspberry Pi under the AIY brand, it came with a Voice HAT, Voice HAT microphone board, a 3″ speaker, a cardboard box, some wires and some lovely arcade buttons. This kit was surpassed with the V2 release that used the Pi Zero WH.

But you don’t need the kit to take advantage of the image that they have provided. All we need to do is make a few changes to some configuration files to get the Google AIY to work without the Voice Hat stuff.

I will be using a USB sound card (This is the one I bought from Amazon UK – US) instead of the Voice Hat along with an external speaker and a 3.5mm jack microphone. Wanna follow along? Take a look below.

Where to begin

I will be using Windows for this process, steps are similar but Linux and Mac users will have to muddle through I am afraid.

Step 1 – Building Blocks
This is probably a little obvious to most of you, connect all your components together but hold off on power for now.

You will need to connect the button up to GPIO23, you can still use this without the button if you don’t have one. Using your breadboard, connect one side of your button to GPIO23 and the other side to GND, the pins are mark in pink and blue on the image.

Easy
Here is where things get a little messy, depending upon what you have available. If you have a monitor, keyboard and mouse available then you can set up your WiFi in the GUI environment that this image will boot straight into. This is likely the easiest method to get up and running quickly. I won’t go into the specifics here, setting up the WiFi should be easy as Pi in the GUI these days. You can achieve everything in this tutoral through the GUI interface but I am going to go the rest of the way with a terminal window.

Medium
If you don’t have want to mess up your desktop setup or you are using an all-in-one, you should connect through Ethernet to your home network. This is my preferred method to set up my Raspberry Pi’s.

Hard
Grab yourself a USB to serial converter, and hook it up to the Pi’s RX and TX lines. You will still need an internet connection so this is not the best route to take but it can get you in to connect up your WiFi module.

We are going to connect to the Pi for the first time using SSH or through a serial interface. To do this I am going to use Putty but feel free to use your terminal/serial console of choice, there are even browser based ones these days.

When you open Putty you can choose between SSH or Serial (choose Serial if you are using that interface and select the correct COM port) at this point you will need to know the IP address of your Pi if running the SSH route.

Now under 2. Network Option we will need to connect to WiFi if you are using it. You will need to know your home network SSID (The name that pops up when searching for WiFi) and the password.

Edit audio settings and test

Now we need to delve into some of the audio configuration files, these are used by the Pi (Specifically ALSA) to determine what device to use as the default for audio input and output.

You will need to run two commands which will open files in the terminal, delete the contents of that file and paste in the Audio Configuration code listed below.

Command 1

sudo nano /etc/asound.conf

Command 2

sudo nano ~/.asoundrc

Audio Configuration codeThis code tells the OS which card you want to use, here it is specified as a USB PnP Sound Device. If your USB sound card reports to the OS differently you can alter the title, use the command cat /proc/asound/cards to see how your appears.

Now we need to get that onto the Pi, the easiest way is just to create a file in the terminal, open the downloaded file on your PC and copy and paste the contents to the terminal window inside the JSON file we will create:

sudo nano /home/pi/assistant.json

Now to activate!

cd AIY-voice-kit-python

The run this:

src/examples/voice/assistant_grpc_demo.py

It is going to ask you to visit a website, bit of a pain because copying the URL from the terminal will close the Python script. Copy the long URL and paste it into your desktop browser and follow the authorisation steps to get your key. Copy the response key from your browser and then run that command again in the terminal, this time you can paste the response in and it will activate your device.

Getting it to run on boot

Copy this example src/examples/voice/assistant_library_with_button_demo.py using this command:

Now we need to create a service, this is something that runs at boot and remains running. To do this we need to create a ‘service’ file that describes our service, the environment it will run in and the location of the Python file. We have called out file main.py but you can call it what every you like, just be sure to change it in the service too.

Now that we have created our service file we will need to move it to the right folder with this command:

sudo mv pi-assistant.service /lib/systemd/system/

Next we will need to enable the service so that it will work on boot with this command:

sudo systemctl enable pi-assistant.service

At this point I had a few problems with the user (Pi) being able to run the script, now the next set of commands are not entirely necessary but if you get permission errors with your service this should solve them:

If all has gone well and once your Pi has fully booted, you should just be able to press the button or call out ‘Hey Google’ to speak to the assistant.

If it hasn’t and your service doesn’t start, run these commands separately and then reboot.

chown -R pi /run/user/1000
chmod 755 /run/user/1000

What to do next?

Adding a power button for a headless setup is really useful to safely shutdown the pi and to reboot it.

I have implemented rpi-shutdown from Halofx on GitHub and it is working great. One press of the button will shutdown the Pi and once shutdown a second press will reboot it. To get this set up we’ll need a button on GPIO 3, I have marker the pins for you:

Download the code from GitHub and SFTP it over to your Pi or just create a new file with sudo nano /home/pi/shutdown_pi.py and paste the code in.

Now lets edit a file which will run our script at boot, type – sudo nano /etc/rc.local

You will see a file that looks like this:

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
printf "My IP address is %s\n" "$_IP"
fi
exit 0

No you will need to add our Python file to rc.local file like this:

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
printf "My IP address is %s\n" "$_IP"
fi
sudo python /home/pi/shutdown_pi.py &amp;
exit 0

Hopefully and there are no guarantees, you should be up and running. Audio configurations can be fiddly, Python and Linux are quite sensitive to syntax and spacing for commands, you may have a very different USB sound card to me, there are lots of things that can make you efforts fail. I am happy to help in the comments or on YouTube but I am by no means a Linux expert.

Problems?

Yeah, unfortuately that is pretty likely, things can go wrong. Often times it is a case of going back and repeating some of the steps which change settings, permissions or just plain starting again.

Run the command speaker-test to see if you get output on your speakers

You service isn’t running! Run the command sudo service pi-assistant status to see what error message it is flagging up. If it is to do with failing to create a directory repeat the chmod and chown steps

Python is sensitive to spacing and specifically tabs, make sure your code looks like it does above

My audio output is really low! Try ‘Hey Google, what’s the volume?’ then set is higher with – ‘Hey Google, set volume to *1-10 or 0-100%*’

Sure, you can use the RPi.GPIO to turn on an off LEDs. You should be able to play WAV files via aplay or VLC if you wish. I imagine aplay is probably easier. You could put it all in the conditional statements within the main python program. For example, I use the EventType.ON_ALERT_STARTED flag from AIY to tun set a GPIO pin high.

You’d want to handle the exception a little differently if you’re not running a GUI. Restart_sound_file_name is passed as a command line parameter, so I have to make sure that I didn’t mistype it before using it. Less paranoia _may_ be okay if you’re building a production package and you’re “certain” it’s okay 😉

To play the sound:
pygame.mixer.music.load(self.alert_sound_file_name)
pygame.mixer.music.play(0)

Note that my alert sounds are ones that I generated with the same sample rate and in mono. If you’re using multiple sounds with different sample rates and/or number of channels, you probably need to reinitialize the mixer for each.

When asking the assistant to call me by my name, it replies that it does not have permissions set in the assistant settings to allow it to do so… It canot say my name or get my location etc…On my Google account all permissions is granted! I guess it must be a setting in Raspbian?
Please help!

Hi, when you ask Google to tell you your name it will tell you about the permissions. This should trigger something on you phone (assuming you have a Google account on your phone) and it will prompt you to change some settings in the app. I didnt run into any problems so it isn’t something I have had to troubleshoot.

Hi, i install aiyprojects-2018-08-03 image to raspberry pi 3B+.
I doing step by step following your tutorial.
after run command: sudo service pi-assistant start
everything ok.
but when i reboot raspberry pi.
nothing response when i say “Ok google” or push button

The operating system is the same, the AIY image just has all of the code preloaded along with it’s dependencies. These instructions seem to detail installing the Google Assistant over Raspbian – https://github.com/t1m0thyj/AssistantPi