Tuesday, May 23, 2017

Google Assistant on Raspberry Pi

Google Assistant on Raspberry Pi 

On April 27, 2017, Google has announced general availability of Google Assistant SDK to the developers (public). What this means is that  anybody can now download and run the Google Assistant and related skillsets on any gadget - it can be run on a Raspberry pi or a laptop / desktop  etc..

Taking advantage of this, I decided to try work on a Google Assistant embedded Speaker (something like the Google Home). It came out to be a good working model. But unlike Google Home which runs without any issues after the initial setup, this one required some manual steps to be performed whenever the Raspberry Pi is rebooted. Obviously, it can be automated with some scripts to make get it running while Pi module is rebooted, though, this is out of scope of this project for now.

Lets get started with the steps involved in getting this accomplished:

Requirements:
1) Raspberry pi (I’ve used “Raspberry Pi 3” which has in-built wifi adapter)
2) Micro SD card (atleast 16 GB size)
3) USB mic
4) Speakers
5) USB Keyboard and Monitor (for initial setup - like OS install and Wifi connectivity)

Initial Steps:
1) Format the micro SD card make the card ready for OS install.
2) Download latest Raspbian Jessie OS from the site: https://www.raspberrypi.org/downloads/raspbian/
3) Install the OS into the micro SD card
4) Boot Raspberry Pi and login with “pi“ user.
5) As the world knows default password for “pi” user (default password is “raspberry” [without the quotes]), It is highly recommended that we first reset password for “pi” user with a strong password.
6) Connect to your Wifi network
7) Now enable SSH & VNC and do other configurations to run PI (headless) using the command:
# sudo raspi-config

8) Do a update & upgrade first using the following commands:
# sudo apt-get update
# sudo apt-get upgrade
once done, we need to reboot the server.

Now we are all set to start working on our “Google home on Raspberry Pi” project.

For the steps mentioned above, we needed a Keyboard and a monitor for accessing the OS on raspberry pi machine. Now that we have VNC enabled, we don’t need the keyboard or monitor as we can run our Pi headless.

Steps Further:
1) We can plug-in the USB mic onto the Raspberry Pi (R-Pi), first.
2) From your Laptop or Computer, you can access R-Pi via VNC.


3) Open browser & navigate to :
http://console.cloud.google.com/cloud-resource-manager


4) Create a project “Create Project” & give it a name [ any name of your pref.], and click “Create” button:

5) Click “menu icon” from top left corner “4 lines icon” and then Click “API Manager”


6) Click “Enable API”

7) Type “Google Assisant” in search box and click on that when found,

8) Then enable it (Click on “Enable)

9) Next create credentials for it.


10) Go to “OAuth consent screen” then Type in the Product name (project name we gave earlier – in my case “sri-gapi”) and then leave everything as default

11) Click “Save” click “create credentials”

Click “OAuth client ID” & choose “Other” Then type name “sri-gapi” (your project name)

and click “Create”


12) Click “OK” in the dialog box.

And download it (json file) by clicking download icon on right.


Now we’ll work on the Raspberry Pi

1) Type the following command, which will install python3 modules required for this to work:
# sudo apt-get install python3-dev python3-venv

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

# python3 -m venv env
# env/bin/pip install pip setuptools –upgrade

# source env/bin/activate

Now were are inside the environment, So we can run the below commands :
1) # python -m pip install google-assistant-sdk[samples]

The above will take a while depending on the internet speeds (for me, it took around 15 min).

2) Once it is complete, we can start google assistant as follows (we may have to use the client-secrets files (.json) as well:

# python -m googlesamples.assistant.auth_helpers --client-secrets /home/pi/Downloads/client_secret_<_______secret_here_____>.apps.googleusercontent.com.json

Pressing enter on the above screen takes us to a screen as shown below:
Select the account that you want to use.

This will take us to a website --> click on “Allow” button.
Once you click “Allow” it takes us to the following screen (if successful)

The final step before we test drive google assistant on Raspberry Pi, we need to activate some google activity controls in our account.

8) Let us enable those as well, as shown below:
Go to https://myaccount.google.com/activitycontrols

a. Turn “Web & App Activity” button “on”

b. Enable “Location History (paused)”
c. Enable “Device information”
d. Enable “Voice & Audio Activity”


Which completes authentication and it gives us back the command prompt on Pi console. We are all ready now to run google assistant and try our commands:

3) In the Pi console type the following :
# python -m googlesamples.assistant

If everything is setup correctly, your console would look as follows:

We are all set and Google Home (Google Assistant SDK) on Raspberry Pi is working now…
We can now ask any question to test.

On google API dashboard it will show the traffic (questions we asked can also be seen there) and other stats:

If the R-Pi is shutdown and restarted anytime, then run the following commands to get it working:
# source env/bin/activate
# python -m googlesamples.assistant

You may automate it using a script if needed, so that it starts up and is ready across reboots.

Have fun... !

No comments: