In the “Run Google Assistant on Android Things using the Any Voice Kit” tutorial, we learned how to integrate the AIY Projects Voice Kit with the Raspberry Pi 3 Model B and imported the Google Assistant API sample for Android Things into Android Studio. In today’s conclusion, we’ll configure the Google API Project so that we can converse with Google Assistant.
Make sure you enable I2S by changing the following line in config.txt on the first SD card partition. Otherwise, the microphone will not work. Open the dev terminal and type “sudo leafpad /boot/config.txt” (using leafpad). Then, remove the “#” in front the the line “dtoverlay=generic-i2s” and insert a “#” in front of the “dtoverlay=pwm-2chan-with-clk,pin=18,func=2,pin2=13,func2=4” line, as shown in the following code snippet:
# comment or remove this line: # dtoverlay=pwm-2chan-with-clk,pin=18,func=2,pin2=13,func2=4 # # uncomment or add this line: dtoverlay=generic-i2s
Configuring the Google API Project
To use Google Assistant, we need to create an account and enable the Google Assistant API.
In your browser, navigate to Google’s My Activity page. My Activity is a central place to view and manage activities such as searches you’ve done, Web sites you’ve visited, and videos you’ve watched. When you use certain Google services—like Search, YouTube, or Chrome—your activity can be saved as data to your account. My Activity helps make your experience on Google faster and more useful. The kinds of activities that show up in My Activity depend on which Google products you use and which Activity controls are turned on.
Click the “Activity controls” link in the left-hand pane:
Figure 1: Selecting the “Activity controls” link
That will bring you to the Activity controls page. There, enable the following Activity controls in the Google Account you plan to use with the Assistant by dragging the sliders from left (off) to right (on):
- Web & App Activity
- Device Information
- Voice & Audio Activity
Figure 2: The Activity controls slider
Go to the Google Cloud Platform Projects page. Create a new project, or feel free to use an existing one if you’ve already created one.
Figure 3: The “Create Project” button
Enable the Google Assistant API on the project you selected.
Figure 4: Enabling the Google Assistant API
On the Google Assistant API details page, click the Enable button:
Figure 5: Ready to click the Enable button
The next step is to Create an OAuth Client ID. From the Cloud Console, click the Credentials link on the left.
On the OAuth consent screen tab, give the product a name (don’t use “Google” in the name) and a description.
Figure 6: Naming and describing the product
Once saved, the site should immediately bring you to the Credentials page. There, open the Create Credentials dropdown and select “OAuth client ID” from the list:
Figure 7: Opening the Create Credentials dropdown
Select the Other radio button and give the client ID a name, such as “Android Things AIY Voice Kit Client.”
Click the Create button. A dialog box appears; it shows you a client ID and secret. There’s no need to remember or save this, so you can close the dialog:
Figure 8: Closing the dialog
Click the “arrow” button at the far right of screen for the client ID to download the client secret JSON file. It will be named something like “client_secret_[long number].json”.
Installing the google-auth-lib Command Line Tool
Open a terminal and install the google-auth-lib command line tool:
$ pip install google-auth-oauthlib[tool] --user
Navigate to your top-level project directory.
Use the google-oauthlib-tool command line tool to grant the permission to use the Assistant API to your application and create a new credentials.json file in your app resource directory where “path/to/client_secret_NNNN.json” is the path of the JSON file you downloaded:
$ cd <project-directory-name> $ google-oauthlib-tool --client-secrets path/to/client_secret_NNNN.json --credentials app/src/main/res/raw/credentials.json --scope https://www.googleapis.com/auth/ assistant-sdk-prototype --save
Running the Demo
It’s time for the moment of truth.
Perform the following steps from within Android Studio:
- Deploy the app to the device by selecting Run -> Run 'app' from the menu.
- Select the Android Monitor tab (typically at the bottom of the window) to see the logcat output inside Android Studio.
You’ll know that the project has successfully launched on the device once you can see the following startup message in the log:
... D AssistantActivity: Starting Assistant demo
To ask a question:
- Press a button to start recording your request; for instance, “What is the weather like today?” Release the button when finished talking.
- The Google Assistant answer should play back the following on the speaker:
“The forecast for today is twenty degrees Celsius, mainly sunny skies with a ten percent chance of showers late in the afternoon.”
In today’s article, we configured the Google API Project so that we can interact with Google Assistant using the AIY Projects Voice Kit. For more information and detailed instructions, be sure to visit the Google Codelabs Android Things Assistant tutorial and the Google Assistant SDK page.
Rob Gravelle resides in Ottawa, Canada, and has built Web applications for numerous businesses and government agencies. Email him for a quote on your project.
Rob’s alter-ego, “Blackjacques”, is an accomplished guitar player, that has released several CDs. His band, Ivory Knight, was rated as one of Canada’s top hard rock and metal groups by Brave Words magazine (issue #92).