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

Raspberry Pi Google Assistant Hardware Required

You will need:

  1. Raspberry Pi with a fresh install of Raspbian on an SD card
  2. USB webcam or microphone.
  3. external speaker.
  4. Schematic from the Pi LED Tutorial (not necessary)
  5. 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.

Sound settings

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.

Check playback and recording 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.

asoundrec config file for microphone and speaker

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:

Make a test recording to check your settings

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» .

Create a new Google 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» .

Enable Google Assistant API

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

Changing your Google Activity settings

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.

Register your Pi as a model

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.

Copy Secret City ID to PI

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.

Generating an authorization link from the command line.

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.

Testing

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:

Terminal output for Google Assistant

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:

Activating the OnOff Assistant feature

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.

 pip install rpi.gpio 

Now navigate to the folder containing the script hotword.py .

 cd assistant-sdk-python/google-assistant-sdk/googlesamples/assistant/library 

Script modification

You will need to add a few lines to the hotword.py script, so open it up in the nano editor:

 nano hotword.py 

Under the import statement, add your own for RPi.GPIO.

 import RPi.GPIO as GPIO 

Looking for a method process_event . On line 66, remove or comment out the print statement and add an if statement to control the LED.

 #print('Do command', command, 'with params', str(params)) if command == "action.devices.commands.OnOff": if params['on']: print('---------------') print('Led turned on') print('---------------') GPIO.output(18, GPIO.HIGH) else: print('---------------') print('Led turned off') print('---------------') GPIO.output(18, GPIO.LOW) 

This logic controls the LED, but it is not yet set to output. Set it in a function main() before calling the process_event method.

 GPIO.setmode(GPIO.BCM) GPIO.setup(18, GPIO.OUT, initial=GPIO.LOW) 

The GPIO pin is now set to pin and is initialized low. Save and exit. You can run your modified script by passing the model id number (found in the action console) as an argument.

 python hotword.py --device-model-id YOUR-MODEL-ID-HERE 

The terminal output is the same as before and the helper will work as standard. However, now when you say «OK Google, turn it on» you will see a new output:

Terminal message indicating

Note. The above image has been cropped to show only the assistant who heard the request and the print statement added to the script.

You should see your LED turn on too!

The voice activated the LED.

Your own DIY Raspberry Pi Google Home Assistant

This project is a good introduction to using Google API services. Now that you have a Google Assistant device, try out some of the best commands we’ve covered some great mini-games

Похожие записи