/balena-google-assistant

Docker build files for deploying the "google assistant" client on a raspberry pi 3 with Balena

Primary LanguageShellEclipse Public License 2.0EPL-2.0

balena-google-assistant

Introduction

Runs the google assistant ("OK Google") on a raspberry pi.

In other words it turns your raspberry pi into a "google home" device

Hardware requirements

  1. speaker (or headphone) that is connected to the audio jack of the raspberry pi
  2. USB microphone (or webcam with microphone) that is connected to one of the USB ports of the raspberry pi.

STEP 1: Configure Google Project and Register Device Model

This repository is based on the instructions specified at: https://developers.google.com/assistant/sdk/guides/library/python/

So first you need to perform the following steps of these instructions:

  1. Configure a Developer Project and Account Settings. Write down the Project ID generated by google as you need it in STEP 2.
  2. Register the Device Model - you don't need to transfer the downloaded client secret to the raspberry pi. You will need it for setting the service variable "GOOGLE_ASSISTANT_CLIENT_SECRET" in the next step. Also write down the device model ID generated by google as you will also need this in next step.

STEP 2: deploy this balena application.

  1. Follow the standard instructions to create and deploy a balena application = amongst others: (fork and) clone this github repository, create the git remote for this balena application and then do a git push balena master (or similar command)
  2. As you have not set any service variables you will see errors in the log files.
  3. Set the following service variables using the balena dashboard.
Service Name Description
google_assistant GOOGLE_ASSISTANT_PROJECT_ID The project ID generated by google in previous step. Don't confuse this with the project name that you have specified.
google_assistant GOOGLE_ASSISTANT_DEVICE_MODEL_ID The device model id generated by google in previous step
google_assistant GOOGLE_ASSISTANT_CLIENT_SECRET The complete client secret (= json string) retrieved in previous step
  1. When the GOOGLE_ASSISTANT service is restarted you should see something like the following in the balena Logs (this also means that you can continue with the next step):
24.06.19 17:56:33 (+0200) Starting service 'google_assistant sha256:c392950370c392295ee322780bd29f97aead2dc2e1d739977b2f401b172d3c8d'
24.06.19 17:56:34 (+0200)  google_assistant  The credentials (/root/.config/google-oauthlib-tool/credentials.json) are not set !
24.06.19 17:56:34 (+0200)  google_assistant  ... Creating /client_secret.json based on the contents of device service variables GOOGLE_ASSISTANT_CLIENT_SECRET...
24.06.19 17:56:34 (+0200)  google_assistant  **********************************************************************
24.06.19 17:56:34 (+0200)  google_assistant  * You can now create the google credentials by launching the script: *
24.06.19 17:56:34 (+0200)  google_assistant  *                 /create_credentials.sh                             *
24.06.19 17:56:34 (+0200)  google_assistant  * in a balena terminal for the service 'google_assistant'.           *
24.06.19 17:56:34 (+0200)  google_assistant  **********************************************************************
24.06.19 17:56:34 (+0200) Started service 'google_assistant sha256:c392950370c392295ee322780bd29f97aead2dc2e1d739977b2f401b172d3c8d'

STEP 3: Create the (google assistant) credentials

  1. In Balena dashboard open a Terminal for the service "GOOGLE_ASSISTANT" and enter the command /create_credentials.sh You should see something like (I partially masked it):
root@f0bc010:/# /create_credentials.sh
Please visit this URL to authorize this application: https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=3983XXXXXXX84fb.apps.googleusercontent.com&redirect_uri=urn%3AietXXXXXXXh%3A2.0%3Aoob&scope=https%3A%2XXXXXXXapis.com%2Fauth%2Fassistant-sdk-prototype+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fgcm&state=d720mXXXXXXXXXXXXXXXXXXXXXXXXXX&code_challenge=GXXXXXXXJs&code_challenge_method=SXXXXXXX6&prompt=consent&access_type=offline
Enter the authorization code:
  1. Copy paste above URL in a browser and follow the instructions as outlined in step 3. of Generate Credentials.
  2. copy paste the authorization code in the balena terminal window and you should get something like:
...
Enter the authorization code: 4/cgElC5XXXXXXXXXXXXXXXXXXXXXXXXXXXXXDe3iQ4U
credentials saved: /root/.config/google-oauthlib-tool/credentials.json
The credentials =
{"scopes": ["https://www.googleapis.com/auth/assistant-sdk-prototype", "https://www.googleapis.com/auth/gcm"], "client_id": "3983XXXXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com", "token_uri": "https://accounts.google.com/o/oauth2/token", "client_secret": "KiXXXXXXXXXXXXXXXXXXXc7", "refresh_token": "1/oohXXXXXXXXXXXXXXXXXXXXXX4s"}
You should now restart the google_assistant service.
root@f0bc010:/# 
  1. That is it. You can now restart the google_assistant service within the balena dashboard and you should see something like:
17.06.19 00:25:25 (+0200) Starting service 'google_assistant sha256:47e2a96dfd306851d3c4934cdf05eae46ab5d21b89c14303b44d37d5fd1c99a1'
17.06.19 00:25:26 (+0200)  google_assistant  Creating the /root/.config/google-oauthlib-tool/credentials.json using device service variable GOOGLE_ASSISTANT_CREDENTIALS ...
17.06.19 00:25:26 (+0200)  google_assistant  Starting googlesamples-assistant-hotword ...
17.06.19 00:25:26 (+0200) Started service 'google_assistant sha256:47e2a96dfd306851d3c4934cdf05eae46ab5d21b89c14303b44d37d5fd1c99a1'
17.06.19 00:25:31 (+0200)  google_assistant  device_model_id: pi3thrXXXXXXXXXXXXX44
17.06.19 00:25:31 (+0200)  google_assistant  device_id: B12XXXXXXXXXXXXX64E3
17.06.19 00:25:31 (+0200)  google_assistant  
17.06.19 00:25:36 (+0200)  google_assistant  Registering...Done.
17.06.19 00:25:36 (+0200)  google_assistant  
17.06.19 00:25:36 (+0200)  google_assistant  ON_MUTED_CHANGED:
17.06.19 00:25:36 (+0200)  google_assistant   {"is_muted": false}
17.06.19 00:25:36 (+0200)  google_assistant  ON_START_FINISHED
17.06.19 00:25:36 (+0200)  google_assistant  ON_MEDIA_STATE_IDLE

ALTERNATIVE STEP 3: Set google credentials through device service variable.

It is also possible to set the google credentials through the device service variable GOOGLE_ASSISTANT_CREDENTIALS. Of course in that case you must know the google credentials. This step is mainly there to be backwards compatible with version v1.0.0

Service Name Description
google_assistant GOOGLE_ASSISTANT_CREDENTIALS The complete credentials (= json string). If this environment variable is set then the google assistant at startup will first overwrite the contents of the file /root/.config/google-oauthlib-tool/credentials.json with the contents of this variable.

Troubleshooting & Issues

See also issues reported in this github repository (feel free to add your own issues)

1. (Very) Noisy audio

If the audio jack is not plugged correctly you will hear a lot of noise. In that case move the audio jack a bit more in or out until the noise is gone.

2. ERROR "google_assistant [FATAL:audio_input_stream.cc(47)] Input device could not be opened: default"

This indicates that the default audio input stream (= microphone) is not properly configured on your raspberry pi.

Miscellaneous

Testing the audio

You can test the speakers (audio) by running the following command in a terminal for the service "GOOGLE_ASSISTANT":

speaker-test -c2 -t wav

You can change the volume by running the command:

 alsamixer