Build a Google Home assistant with Android Things
Tags: android, googlecloud, iot, java
Published: 2020-08-22 21:26:44

***(Note: these posts are migrated from my previous [medium.com](https://medium.com/@monkeytypewritr) blog)***

After playing around with [Android Things for the first time](https://dev.to/w4ilun/android-things-getting-to-blinky-3b2m), I’ve decided to explore the APIs and try out all the other cool things you could do with this platform. Though blinking an LED was only fun for about 5.2 seconds, that accomplishment prepared me with knowledge to tackle some of the more advanced projects. There are quite a few examples on GitHub, ranging from toggling [GPIOs](https://github.com/androidthings/sample-simplepio), [Bluetooth audio](https://github.com/androidthings/sample-bluetooth-audio), and even one on [TensorFlow](https://github.com/androidthings/sample-tensorflow-imageclassifier).

At around the same time, Google announced their [AIY Voice Ki](https://aiyprojects.withgoogle.com/)t: a DIY AI assistant using a Raspberry Pi. I got super excited and couldn’t wait to build one myself; it looked like a fun little project, and I’ve always wanted to compare Google’s experience with the Amazon Echo’s.

![The kit came bundled with issue #57 of The MagPi](https://cdn-images-1.medium.com/max/2770/1*UtMJt1a89jwnwI5b5e_h3g.png)

Much to my dismay, the kits were sold out everywhere, and scalpers were selling them for upwards of $90 on eBay :(

![Me: sad](https://cdn-images-1.medium.com/max/2000/1*Y7CfklX6tWBTzAB8HZc6PA.png)

The kit was essentially a cardboard box with a Voice HAT that connects the button, speaker, and microphones to the Raspberry Pi; the magic was within the [Google Assistant SDK](https://developers.google.com/assistant/sdk/overview), which provides the APIs for the voice-based AI interactions.

Luckily for me, the Google Assistant APIs are supported on Android Things! Would it work on the Edison?

![The LED on pin 13 blinks when playing an audio response](https://cdn-images-1.medium.com/max/2000/1*sPEsGK9fhPtsqAE527KFwA.gif)

![speech-to-text captured in the logs](https://cdn-images-1.medium.com/max/2008/1*6E4n5sTY4NgTv_FxtSjaUw.png)

It works! Now let’s see how to put it together…

## Things You’ll Need

* Intel Edison with Arduino Breakout (though any Android Things supported device should work)

* [USB sound card with mic input](http://www.ebay.com/itm/USB-3d-Audio-Sound-Card-Microphone-Headset-Adapter-/152202347054) (not needed if your board has audio jacks for in/out)

* Speakers (I used my headphones because I don’t have any handy)

* [Microphone](http://www.ebay.com/itm/Tube-Hands-Free-Microphone-Lavalier-Lapel-For-SmartPhone-Laptop-PC-Notebook-/152393037334)

* Arduino Shield + Button (you could also wire a momentary switch directly to a GPIO pin)

![The button is connected to IO4](https://cdn-images-1.medium.com/max/7680/1*JAQfkKPgBq7Owi7jWwiDzA.jpeg)

## Running Google Assistant on Android Things

The first thing you’ll need to do is to flash Android Things onto your device, you can follow my [previous article](https://medium.com/all-technology-feeds/android-things-getting-to-blinky-8c37dd36b259) or use the [official guide](https://developer.android.com/things/hardware/edison.html). It might be a good idea to at least get to blinky first, so you know that you have a functioning Android Things device.

Once that’s done, you can then open the Google Assistant sample in Android Studio. You can find the sample code [**here](https://github.com/w4ilun/sample-googleassistant), **which I forked from the [official repo](https://github.com/androidthings/sample-googleassistant); I’ve made some minor changes to make it work with my setup, namely:

* Changed the button to activate on high instead of low

    Button.LogicState.***PRESSED_WHEN_HIGH***

* Mapped button to IO4 (in BoardDefaults.java)

```java
    public static String getGPIOForButton() {
        switch (getBoardVariant()) {
            case DEVICE_EDISON_ARDUINO:
                return "IO4";
```

* Default to USB sound card instead of the Voice HAT for the Raspberry Pi

```java
    AUDIO_USE_I2S_VOICEHAT_IF_AVAILABLE = false
```

![I’m using Android Studio 2.3.2](https://cdn-images-1.medium.com/max/2978/1*bpZcUTjERGaFJUsKKq3cjQ.png)

Before you build and deploy onto the device, you’ll also need setup your credentials so your device can use the Google Assistant APIs, and grant permissions via Oauth:

* Follow the instructions [here](https://developers.google.com/assistant/sdk/prototype/getting-started-other-platforms/config-dev-project-and-account) to enable Google Assistant APIs and get your client secret JSON file. You can then rename that file to **client_secret_NNNN.json **to keep it short (if you want to). I also found this [guide](https://aiyprojects.withgoogle.com/voice/#users-guide-1-2--turn-on-the-google-assistant-api) pretty useful .

* Copy that file over to your projects root directory, then run Google’s oauthlib tool to generate the credentials.json:

```shell
pip install google-auth-oauthlib[tool]
google-oauthlib-tool --client-secrets client_secret_NNNN.json \
                     --credentials app/src/main/res/raw/credentials.json \
                     --scope https://www.googleapis.com/auth/assistant-sdk-prototype \
                     --save
```

All set! Now click run let the app build and deploy. It should build successfully and you’ll be greeted with:

![woohoo](https://cdn-images-1.medium.com/max/2000/1*sY_v-rq4dZ0q6FpOA1xCsw.png)

Yay! Except nothing works, because the USB sound card is not connected…what?!

The Edison Arduino breakout board is setup in a way that there’s a tiny toggle switch between the larger USB-A port and the micro USB port. You can only use one or the other, by moving the toggle switch. Which means you can **either** use the micro USB port for the ADB shell connection to deploy and debug your application, **or **you can switch and use the larger USB host port for your sound card, **but not both at the same time :(**

Thankfully, the solution is simple: you’ll need to connect a 12V power supply so you can keep the USB host port active for the sound card (i.e. tiny toggle switch facing up). However, you’ll lose the micro USB connection and need to use [ADB shell over Wi-Fi instead](https://stackoverflow.com/questions/4893953/run-install-debug-android-applications-over-wi-fi):

    $ adb tcpip 5555
    $ adb connect <DEVICE_IP_ADDRESS>:5555
    $ adb shell

Now deploy and run your application again. Your device will start listening when you press the button, and stop when you release it. Your voice query gets sent over via the Google Assistant API, and you get a response almost instantaneously. Neat! Since the service is linked to your Google account, you can even tell it to add events to your Calendar and access other information. Ask for a joke, find out tomorrow’s weather, and when you run out of ideas, check out this [comprehensive list](http://www.androidauthority.com/google-home-commands-727911/).

Well, that’s it. Again, this has been a lot of fun and it’s great to see how easy it is these days to sprinkle some AI into any project. I still haven’t fully explored the capabilities of my DIY Google Assistant yet, but I think there’s a lot of potential for interesting projects here. I’d love to find out if it can control other IoT products as well, [perhaps a spoofed WeMo switch?](https://medium.com/@monkeytypewritr/amazon-echo-esp8266-iot-a42076daafa5) :)