Build your own Raspberry Pi Google Assistant

Back to Blog
Google-Assistant-Factory-Forward

Build your own Raspberry Pi Google Assistant

Google Home, Alexa and Cortona are many Home assistants that are popular in recent days. These Assistants are still in learning process. Hence the makers of Google Assistant and Alexa provided free access to developers for updating its knowledge day by day. Anyone can install these voice assistants on their linux computers such as Raspberry Pi and Add more features to improve these Assistants.

In this tutorial, we will see how to make our own Google Assistant using Raspberry Pi 3. This step by step tutorial covers registering the Device on Google API, Setting up hardware, Download and Install Google Assistant SDK and Create a Shell Script to Autostart Google Assistant on Boot up.

Components Required:

Create and Configure Google API

Before getting started, we need to create a project in Actions on Google (for developers to extend the Google Assistant).

Tip: It will be easy to use your Pi Browser for the following steps as it needs to download credential files and device model ID. You can use your PC also but you need to transfer these files to the Pi later.

Step 1:  Sign in to your Google Account.

Visit Actions on Google here. Or copy paste the following URL to visit.

https://console.actions.google.com

Step 2: After visiting the URL you will get this screen. Select ‘Add/Import’ Project.

1.Console

Step 3: Give a name to your project and choose your country. Here I gave ‘FactoryForward’ and set the country as ‘India’. Once done, click ‘Create Project’.

2.ProjectName-Country

Step 4: In a new tab, Visit Google Developer Console and enable the Google Assistant API.

On the top left drop-down, select ‘your project’ that you gave on the previous step. We gave Project Name as ‘FactoryForward’. Then click the Enable Button.

3.Enable-Google-Assistant-API

Step 5: Now back to the other tab and then scroll down to the bottom. Here you can see ‘Device Registration’.

 4.Device Registration

Here you can see a new screen with a ‘Register Model’ Button. Click on it

5.Register-new-device

Step 6: Here you can give any name you like (Just need to fill it). Here I gave,

Product Name: Pi Google Assistant

Manufacturer Name: FactoryForward

Device Type: Speaker

Please note down the Device Model ID and store it in a text file. We will need it later so that it will be easy to pick.

 

6.device-detailsv2

Now click on the Register Model button, it will ask you to download the credentials file. Download and Keep it somewhere. We will need this later.

7.Credentials

Click Next, No need to change anything here. Click on ‘Save Traits’.

8.Save Traits

Once done, you will get the project dashboard screen.

9.successfully createdv2

All we need to do now is just go ahead and enable the activity controls to make the Google Assistant work properly. Visit the URL to change the activity controls.

https://myaccount.google.com/activitycontrols

The following activity must need to be enabled.

  • Web & App Activity
  • Location History
  • Device Information
  • Voice & Audio Activity

Configure and Test Audio:

Let’s Configure and test our Mic and Speaker are working properly. It would save our time in case of any troubleshooting needed on the Audio part.

Step 1: First connect the USB Mic and Speaker. Here we used USB Mic and 3.5mm Jack for Speakers. Let’s see how to find the speakers and mics connected with our Raspberry Pi.

To find Recording Devices like Mic, enter the following code in the terminal. Note down your USB Mic’s Card number and Device Number.

arecord -l

For USB Mic the card number will be 1 and Device number will be 0 (it will change according to the number of USB Mic connected on your Pi).

Next find our Speakers, To list the speakers connected to Raspberry Pi use the following command,

aplay -l

 

10.device-listv2

 

Here for USB Mic:-

Card number: 1

Device Number: 0

 

For 3.5mm Audio Jack Speakers:-

Card Number: 0

Device Number: 0

 

There is another device number for Speaker is available called ‘device 1’. That is for HDMI Monitors which has inbuilt Speakers. We are not using it here. So we stick with the above settings.

Step 2: Now we need to create a configuration file and modify the card number and device number. Create a new file named .asoundrc in the home directory (/home/pi). Make sure it has the right slave definitions for microphone and speaker; use the configuration below but replace <card number> and <device number> with the numbers you wrote down in the previous step. Do this for both pcm.mic and pcm.speaker.

File Template:

This is the content of .asoundrc file. Replace it with your device number and Card number.

pcm.!default {
type asym
capture.pcm "mic"
playback.pcm "speaker"
}
pcm.mic {
type plug
slave {
pcm "hw:<card number>,<device number>"
}
}
pcm.speaker {
type plug
slave {
pcm "hw:<card number>,<device number>"
}
}

Screenshot of Modified File as per our settings (USB Mic and 3.5mm Audio Jack Speaker):

11.device settings updatedv2

Press Ctrl+X and Y to Save and Exit.

To avoid default audio setting we can set the 3.5mm Audio Jack in raspi config settings also. Type the following command on your terminal.

sudo raspi-config

Navigate to Advanced options>Audio Options. Here change the Force Audio options to 3.5mm Jack instead of HDMI/Auto (as per our settings). Otherwise, It will play a crackling sound (because it plays audio on HDMI as well as 3.5mm Jack and increases the Audio Load as per our custom settings).

 

12b Audio options

Verify the Recording and Playback Works properly:

Let’s verify our settings are working properly.

Use the following command to open alsamixer to adjust the volume and sensitivity settings. If it is low use the specified function keys to adjust.

alsamixer

Speaker Test:

Enter the following command in terminal. It will play some person speaking sound. Press Ctrl+C once done. If you don’t hear anything, check your speaker connection.

speaker-test -t wav

Mic Test:

Here we will test our MIC by recording a 5-second audio and play it back. Run the command and speak something on the MIC.

arecord --format=S16_LE --duration=5 --rate=16000 --file-type=raw out.raw

Now play the recorded file using this command.

aplay --format=S16_LE --rate=16000 out.raw

If everything works fine, then we can go ahead and install our Google assistant SDK.

Download and Setting Up Google Assistant:

Step 1: Update our Raspberry Pi package list.

sudo apt-get update

Step 2: Create a folder and create a credential.json file and copy the contents of the JSON file which we downloaded earlier. Use the following command,

mkdir ~/googleassistant
nano ~/googleassistant/credentials.json

Once the file is opened, Copy and Paste the content of the credential file (It is Downloaded in earlier steps) to this file. You can use ‘Ctrl+A’ and ‘Ctrl+C’ to copy and right click on the editor and select ‘Paste’.

We can save the file by pressing ‘Ctrl+X’ and then ‘Y’.

Step 3: Let’s install Python 3 and Python 3 Virtual Environment on our Raspberry Pi.

sudo apt-get install python3-dev python3-venv

Step 4: Enable our Python3 Virtual Environment using this command.

python3 -m venv env

Step 5: We will check the pip version and upgrade it. Run the following command.

env/bin/python -m pip install --upgrade pip setuptools --upgrade

Step 6: Now we will enter into Python Virtual Environment to install our Library and SDK Samples.

source env/bin/activate

Step 7: Install the packages here. It contains, all code required to Get Google Assistant Running on our Pi.

Install package system dependencies.

sudo apt-get install portaudio19-dev libffi-dev libssl-dev libmpg123-dev

Install Library and Samples using pip.

python -m pip install --upgrade google-assistant-library
python -m pip install --upgrade google-assistant-sdk[samples]

Generate and Approve Credentials:

Step1: To generate and approve the credentials, we need to install Google Authorization tool. To do that, use the following command.

python -m pip install --upgrade google-auth-oauthlib[tool]

Step 2: Now we need to run the tool to generate auth key and save it. Use the following command.

google-oauthlib-tool --client-secrets ~/googleassistant/credentials.json \

--scope https://www.googleapis.com/auth/assistant-sdk-prototype \

--scope https://www.googleapis.com/auth/gcm \

--save --headless

It will show a long URL with the text ‘Please visit this URL to authorize this application:’ you need to copy the URL by Right click on the URL and choose Copy URL. Then paste it in the Web browser and hit enter. Make sure the correct Google Account is logged in.

It will ask for permission ‘allow it’. Now it will give a lengthy authorization code. Copy it and paste it on the terminal where it shows ‘Enter Authorization code’.

After entering the code, it will show ‘credentials saved’.

16.oauth terminalv2

Now it’s done. Let’s try to run our sample code.

Make sure you are in the virtual environment. It will be denoted as (env) in front of the pi username.

Modify and Run the following command. Make these following changes before proceeding.

Note:

  1. Replace the ‘my-dev-project’ with your Project ID (You can find it in Actions Console). Choose gear icon and project settings.
  2. Replace ‘my-model’ with the name of your model ID. This one we mentioned to copy paste it in a notepad file during device registration. If you lost the file you can find it in Actions Console, Click on your project and choose device registration.
googlesamples-assistant-hotword --project-id my-dev-project --device-model-id my-model

Say ‘OK Google’ or ‘Hey Google’followed by your query. Some of the queries like,

Ok Google, What’s up?

Ok Google, Will it rain today?

Hey Google, Tell me a Joke.

Hey Google, Give me a riddle.

Autostart during Pi Boots up:

There are different methods available to do this. Here we will be creating a script file called google_autostart.sh and add that script file in autostart file in LXDE Terminal.

Step 1: Create a script file with the commands we need to execute. So let’s create a script file using the following command.

sudo nano google_autostart.sh

Copy and paste the following to google_autostart.sh file.

#!/bin/sh

source env/bin/activate

/home/pi/env/bin/google-assistant-demo&

 

17.Google Autostart file

 

The source env/bin/activate is to activate the virtual environment. The /home/pi/env/bin/google-assistant.demo& is for run the google assistant demo file in background.

Step 2: Now we need to set permissions for the google_autostart.sh file.

sudo chmod +x google_autostart.sh

Now we set the permissions. You can check the permissions details using this command (optional).

ls -l google_autostart.sh

Step 3: Now we need to find the autostart file inside LXDE Terminal and add the path to the script file in it. Follow the commands,

cd .config
cd lxsession//LXDE-pi/
ls -l

Now the ls –l file will show the list of files in the current folder. The autostart file is the one we want to edit. So use the following command to open it in nano text editor.

sudo nano autostart

Copy the following contents to this autostart file.

@lxpanel --profile LXDE-pi

@pcmanfm --desktop --profile LXDE-pi

@xscreensaver -no-splash

@point-rpi

/home/pi/google_autostart.sh

Pro Tip: You can copy the path URL by right-clicking on the file and choose ‘Copy Path(s)’ and paste it. It will give you the exact file path without finding it manually.

18.Copy Path

That’s it save the file by pressing Ctrl+X and Y.

Restart the Pi and it will run during boot up automatically.

All Autostart Commands:

17a.Google Autostart commands

 

Check out the working of this Project on Youtube: Google Assistant using Raspberry Pi | FactoryForward

Want to make your own Amazon Alexa. Check this tutorial,

DIY Amazon Alexa using Raspberry Pi | Standalone | May 2018

How to Enable New Alexa Skills in Android App

Share this post

Comments (6)

  • Kim Mu Su

    hi!
    I’m Kim Mu-su, a Korean.
    I read your kind words and followed them well.
    However, copy the auto start part during the pie boot.
    Step 3: Enter “cd lxsession//LXDE-pi/” to output “No such file or directory”. How do we put this together?

    September 14, 2019 at 12:23 PM
    • Sharath

      Sorry for the typo. Remove the additional ‘/’ in that line.

      Correct command is,

      cd lxsession/LXDE-pi/

      September 14, 2019 at 12:38 PM
      • kim mu su

        Thank you for your quick reply.
        Entering as an answer also displays “No such file or directory”.
        And the directory “lxsession” is not visible in “[email protected]: ~/ .config $ls”.
        I’d like a solution, please.
        Good work!

        September 16, 2019 at 7:43 AM
        • Sharath

          The blue texts actually represents the file path. So make sure you are in the right path. In the screenshot you can verify everything is correctly mentioned.

          You can use ‘cd’ to change the directory.

          Have a look at Linux commands & its functions, you may get an idea – https://www.factoryforward.com/frequently-used-raspberry-pi-linux-commands/

          I didn’t tested it in Raspbian Buster. So there might be any changes as it is completely revamped version for Pi 4.

          September 17, 2019 at 6:30 PM
          • 김무수

            Thank you again for your prompt reply.
            However, I cannot find the directory “/lxsession/LXDE-pi/” in my raspberry pie. Please give me the route I’m looking for.
            Have nice day!

            September 18, 2019 at 7:56 AM
  • PUVVADA GOPALA KRISHNA

    i think u used the pi 3 for connecting microphone
    now pi4 in advanced option audio related information not there
    how to configure
    Please suggest me

    September 14, 2021 at 9:39 PM

Leave a Reply

Back to Blog