Build your own Raspberry Pi Google Assistant

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.

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,

 

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.

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.

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.

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.

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.

Now play the recorded file using this command.

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.

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,

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.

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

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

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

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

Install package system dependencies.

Install Library and Samples using pip.

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.

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

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.

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.

Copy and paste the following to google_autostart.sh file.

 

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.

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

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,

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.

Copy the following contents to this autostart file.

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

Leave a Reply

Your email address will not be published. Required fields are marked *