Google Home Pi : Raspberry Pi Powered Virtual Assistant

The Google Home is useful to have around the home.It is a beautiful device with built-in Google Assistant — A state of the art digital personal assistant by Google. It can play media, save your reminders and notes, tell you the length of your commute, do home automation.It can be placed anywhere at your home and it will do some amazing things for you, but, it’s an expensive proposition if you’re not sure you’ll use it. Good news though, you can make a fully-functional one using a Raspberry Pi.

By the end of this guide, you’ll have a fully functioning Google Home that responds to your voice commands. Otherwise, it’s an Assistant with all the features of Google Home. Which means it can do unit conversions, play media, check scores, read audio books to you, check the weather, and tons more. It will also work with a variety of home automation devices like smart light bulbs, so you can control them with your voice. Like the real Google Assistant, your DIY Google Home can be linked up to add more features, like adding to-dos to Evernote or to get a notification on your phone when the timer goes off.

Step 1: Things You Need

You'd Need:

  • Raspberry Pi 3 or 2 with Raspbian installed, and Wi-Fi setup.
  • Power Supply and MicroUSB Power Cable. (Minimum 5V, 2A)
  • MicroSD Card. (Minimum 8GB)
  • A USB Microphone. (You will get plenty of how to setup's over the internet, also here...)
  • Speakers
  • A Keyboard and a Mouse for setup
  • A LED and couple of wires to connect

All the things gathered, connected and plugged in, Let's get it started.

Step 2: Setting Up USB Mic.

  • The Pi doesn’t have microphones inbuilt. You need to attach a USB microphone if you want to record audio.
  • Plug your USB microphone into any of the USB slots of your Pi.
  • Type the following command in the terminal.
arecord -l
  • This command will list all the available audio record devices. It will be empty if your USB mic in connected. You should get following output.
pi@raspberrypi:~ $ arecord -l
**** List of CAPTURE Hardware Devices ****
card 1: Device [USB PnP Sound Device], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

You can see that your USB device is attached to card 1 and the device id is 0. Raspberry Pi recognizes card 0 as the internal sound card, that is, bcm2835 and other external sound cards as external sound cards named card 1, card 2 and following...

  • Now, we'll have to change the audio configs. To edit the asound.conf file, type the following command.
sudo nano /etc/asound.conf
  • Add below lines in the file. Then press Ctrl+X and after that Y to save the file.
pcm.!default {
  type asym
  capture.pcm "mic"
  playback.pcm "speaker"
}
pcm.mic {
  type plug
  slave {
    pcm "hw:1,0"
  }
}
pcm.speaker {
  type hw
  card 0
}
ctl.!default {
 type hw card 0
}

This will set your external mic (pcm.mic) as the audio capture device (pcm!.default) and your inbuilt sound card (card 0) as the speaker device.

  • Create a new file named .asoundrc in the home directory (/home/pi) by issuing following command and paste above configurations (which were added in /etc/asound.conf file.) to this file.
sudo nano .asoundrc.

Step 3: Setting Up Your Speaker Output.

  • Connect your speaker to 3.5mm headphone jack of the Raspberry Pi.
  • Run below command to open the pi's configuration screen.
sudo raspi-config
  • Go to Advanced Options > Audio and select the output device.(3.5mm jack or HDMI)

Step 4: Test the Mic and Speakers.

  • To test your speaker run the following command in the terminal. This will play a test sound. Press Ctrl+C to exit. If you are not able to hear the test sound check your speaker connection and power. The test sounds like-

Front Left, Front Right

speaker-test -t wav
  • To test your mic, run following command. This will record a short audio clip of 5 seconds. If you get any error check previous steps again.
arecord --format=S16_LE --duration=5 --rate=16k --file-type=raw out.raw
  • Play the recorded audio and confirm everything works properly by typing following command.
aplay --format=S16_LE --rate=16k out.raw

Our hardware is set.

Step 5: Download Required Packages and Configure Python Environment:

  • First, Update your operating system by running the commands one by one in the terminal.
sudo apt-get update
sudo apt-get upgrade
  • Running the commands one by one in the terminal will create Python 3 environment (The Google Assistant library runs on Python 3 only) in your Pi and install required items.
sudo apt-get install python3-dev python3-venv
$ python3 -m venv env
$ env/bin/python -m pip install --upgrade pip setuptools
  • Activate the python environment. This will bring a "(env)" text in front of your Pi's command terminal.
source env/bin/activate
  • Install the Google Assistant SDK package, which contains all the code required to run the Google Assistant on the Pi. It should download the Google Assistant Library and the significance.
python -m pip install --upgrade google-assistant-library

Step 6: Enabling the Google Assistant Cloud Project.

  • Open the Google Cloud Console and create a new project. (Name it anything.) The account with which you sign in will be used to send queries to Google Assistant and get your personalized response.
  • Head over to API manager and enable the Google Assistant API.
  • Make sure that you enable Web & App Activity, Device Information and Voice & Audio Activity in Activity Controls for the account.
  • Go to “Credentials" and set up OAuth Content Screen.
  • Go to “Credentials” tab and Create new OAuth client ID
  • Select application type as “Other” and give the name of the key.
  • Download the JSON file that stores the OAuth key information and keep it saved.

Step 7: Authenticating the Raspberry Pi

  • Install authorization tool by running below command.
(env) python -m pip install --upgrade google-auth-oauthlib[tool]
  • Run the tool by running following command. Make sure you provide correct path for the JSON file you downloaded in step 6.
(env) google-oauthlib-tool --client-secrets "JSON_FILE_PATH" --scope https://www.googleapis.com/auth/assistant-sdk-prototype --save --headless
  • It should display as shown below. Copy the URL and paste it into a browser. If instead, it displays:

InvalidGrantError

then an invalid code was entered. Try again.

Please go to this URL: https://...
Enter the authorization code:

Step 8: Setting Up the LED Indicator.

  • Connect your LED between GPIO pin 25 and ground.
  • We are going to set the GPIO pin 25 as the output pin.
  • Google Assistant SDK provides a callback EventType.ON_CONVERSATION_TURN_STARTED when the conversion with the Google Assistant begins. At that point, we are going to set the GPIO 25 to glow the LED.
  • Whenever the conversation terminates EventType.ON_CONVERSATION_TURN_FINISHED callback will be received. At that point, we will reset the GPIO 25 to turn off the LED.

Step 9: Initialise on Boot Complete:

  • Whenever your Pi completes booting, we will run a python script that will verify and introduce the Google Assistant on boot.
  • First add RPi.GPIO package to add GPIO support using following command.
pip install RPi.GPIO
  • Run the steps one by one. Go to the user directory. Create new python file main.py.
cd /home/pi
sudo nano main.py
  • Write the linked script and save the file.
  • Now create one shell script that will initialize and run the Google Assistant.
sudo nano google-assistant-init.sh
  • Paste below lines into the file and save the file.
#!/bin/sh
/home/pi/env/bin/python3 -u /home/pi/main.py
  • Grant the execute permission.
sudo chmod +x google-assistant-init.sh

You can run google-assistant-init.sh to initiate the Google Assistant any time.

Attachments

Step 10: Starting the Google Assistant While Booting

  • To enable Google Assistant on Boot there are two ways. Let’s see each of them.

1. Autostart with Pixel Desktop on Boot:

  • This will start the Google Assistant as soon as Pixel desktop boots up. Make sure you have “Desktop” boot selected in Raspberry Pi configurations.
  • Type below command.
sudo nano /home/pi/.config/lxsession/LXDE-pi/autostart
  • Add the following after @xscreensaver -no-splash
@lxterminal -e "/home/pi/google-assistant-init.sh"
  • Save and exit by pressing “Ctrl+X” and then “Y.

2. Autostart with CLI on Boot:(I personally used this, though autostart works quit fine.)

  • This will start the Google Assistant if you have set CLI boot. Make sure you have “CLI” boot selected in Raspberry Pi configurations.
  • Type below command.
sudo nano /etc/profile
  • Add below line at the end of the file.
sudo /home/pi/google-assistant-init.sh
  • Save and exit by pressing “Ctrl+X” and then “Y”.

Step 11: Conclusion

What exactly is different about this Home Pi? Nothing, except expenses. The end result is basically the same, you can activate your DIY Google Home by saying the wake word “Ok Google/ Hey Google,” and the device works just like a real Assistant.You can do many daily activities with your Google Home. If you want to perform your custom tasks like turning off the light, checking the door, you can do it with integrating Google Actions in your Google Assistant. If you have any trouble with starting the Google Assistant, leave a comment below. I will try to resolve them as much as I can.