Voice assistants quickly became commonplace. Many homes have Alexa or Google Home, which controls everything from lighting to media and even timing.
The technology that powers these devices is available—at least in part—to everyone. The Google Assistant SDK allows you to use the service on your devices. This tutorial will show you how to set up Google Assistant on your Raspberry Pi and how to voice activate an LED via GPIO pins.
Hardware Required for Raspberry Pi Home Assistant
You will need:
- Raspberry Pi with a fresh install of Raspbian on an SD card
- USB webcam or microphone.
- external speaker.
- Schematic from the Pi LED Tutorial (not necessary)
- The browser is signed into your Google account.
Note . The equipment for this lesson may vary slightly. I used a spare USB webcam solely for her microphone. Any compatible webcam or microphone should work well, and there’s an extensive list of Pi-compatible devices to help.
Also, I use the 3.5mm output for audio. HDMI and other output sources will also work, although this will require you to change the audio settings below.
Plug in your USB webcam and speaker and set up the LED circuit if you are using one.
This tutorial can be followed directly on the Pi or via an SSH connection to the Pi.
Both methods start in the terminal to check the sound settings. Use Commands arecord -l and aplay -l to view a list of available devices.
The image above shows a USB webcam as cards 1 and devices 0 . Write down the card and device numbers for microphone and speaker output.
Now after making sure you are in the directory /home/pi create a new file and open it in nano:
sudo nano .asoundrc
The contents of this directory will differ depending on which card and device numbers you are using. Here you can select HDMI output instead of 3.5mm if you like.
When you have entered your version of the code above, click ctrl+X, to save and exit.
Test your settings by recording and playing back a short clip:
Optional step: if you want to change the input volume of your microphone, open alsamixer and press F6 to switch between devices.
This is it! The sound is set.
Create a Google project
Open the Pi browser, or if you’re connected via SSH, open the browser locally. Go to the Google Actions Console and click » New project» .
This may take several minutes. When you’re done, leave the window active and open a new tab — we’ll get to that shortly.
Enable Google Assistant API
There are a few online settings that you will need to make in order to proceed. Go to the Google Assistant API website and click » Turn on» .
The project also requires an activity permit. Go to the Actions Control Panel and make sure the following actions are enabled:
- Web and App Activity (including Chrome History checkbox)
- Device info
- Voice and sound activity
Now you can proceed to device registration.
Registering Your Raspberry Pi
Back in the action console, select Device registration on the left panel. Under Product create an easy-to-remember name for your device. Manufacturer name doesn’t matter (but should be there) and select » Auto» for the device type.
Click Register Model and on the next screen click Download OAuth 2.0 Credentials . This will download the JSON file to your computer. If you are not familiar with JSON files, don’t worry but learn how to use JSON worth doing for the future!
Google’s official guide recommends moving the file to /home/pi so open your file manager and do it now.
Additional step for SSH users:
If you use SSH, you will download the JSON file to your local computer instead of the Pi. To transfer it, open a separate terminal window without an SSH connection. From this window, copy the client’s secret JSON file with this command:
scp ~/Downloads/client_secret_client-id.json pi@raspberry-pi-ip-address:/home/pi/
Replace «raspberry-pi-ip-address» with your Pi’s IP address, and don’t forget the colon before the path. If you uploaded the JSON file to a different location, change the local path to reflect this. Enter the password when prompted and the file will be copied to the Pi’s home directory.
Return to the SSH terminal and navigate to /home/pi . Enter ls-l, to get a list of files in a directory. You should see the passed client secret JSON file.
Installing the SDK
Google recommends working in a Python virtual environment. Create a new virtual environment named env .
If you’ve never done this before, this tutorial will help you learn how to use Python virtual environments.
Install the latest versions of Pip, Setuptools and Wheel and activate your virtual environment:
env/bin/python -m pip install --upgrade pip setuptools wheel source env/bin/activate
Google Assistant has some dependencies that you must install in the virtual environment.
sudo apt-get install portaudio19-dev libffi-dev libssl-dev libmpg123-dev
Finally, install the Google Assistant SDK, samples, and the OAuth tool.
python -m pip install --upgrade google-assistant-library python -m pip install --upgrade google-assistant-sdk[samples] python -m pip install --upgrade google-auth-oauthlib[tool]
That’s all it takes to get started. If any of the installations fail, carefully check the spelling and spacing.
Raspberry Pi authentication
Use google-auth-oauthlib [инструмент] with the previously uploaded JSON file with credentials to authenticate your Raspberry Pi.
google-oauthlib-tool --scope https://www.googleapis.com/auth/assistant-sdk-prototype \ --scope https://www.googleapis.com/auth/gcm \ --save --headless --client-secrets /home/pi/YOUR_CLIENT_SECRET_ID.json
You need to replace YOUR_CLIENT_SECRET_ID with the downloaded file, so copy the file name first. This client ID must be correct. Don’t change the filename!
You should receive a message with a link asking you to paste the authorization code.
Clicking on the link opens a browser. You will be prompted to enable the device in your Google account. Copy the following authorization code and paste it back into the terminal window.
You should receive confirmation in the form Credentials Saved: /home/pi… indicating that the Pi is successfully logged in with your Google account.
Now that you’re all set, it’s time to test your Pi Google Assistant. Start the helper with this command:
googlesamples-assistant-hotword --project-id my-dev-project --device-model-id my-model
You will need to replace my-dev-project to your project ID (located under the «Settings» cog in the action console). Your model ID device is listed in the Device Registration section of the Action Console.
Try it! Say «OK Google» and ask a question. You can see the output of the program in the terminal by hearing the response:
This is it! Your Raspberry Pi is now running Google Assistant. Note that if the audio volume is a bit low, you can change it by saying: «Hey Google, turn up the volume to 80%.»
Bonus: GPIO Voice Activation
You can control the light with Arduino and Siri, but there is an easier way. If you’ve customized the LED, you can use Google Assistant to control it with your voice.
Setting up Google Assistant to work with GPIO pins is relatively easy, but requires some additional steps. Go to the Google Actions Console and find your device under Device registration. Click on it and open the trait menu:
Turn on the dash On Off and press Save.
Now, after making sure you are in a virtual environment env clone the SDK version to your Pi with git:
git clone https://github.com/googlesamples/assistant-sdk-python
Since this is a virtual environment, you need to install RPi.GPIO before moving on.