loading...
Cover image for Build a Google Home assistant with Android Things

Build a Google Home assistant with Android Things

w4ilun profile image w4ilun ・5 min read

(Note: these posts are migrated from my previous medium.com blog)

After playing around with Android Things for the first time, 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, Bluetooth audio, and even one on TensorFlow.

At around the same time, Google announced their AIY Voice Kit: 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

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

Me: sad

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, 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

speech-to-text captured in the logs

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 (not needed if your board has audio jacks for in/out)

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

  • Microphone

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

The button is connected to IO4

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 or use the official guide. 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, **which I forked from the official repo; 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)

    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
    AUDIO_USE_I2S_VOICEHAT_IF_AVAILABLE = false

I’m using Android Studio 2.3.2

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 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 pretty useful .

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

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

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:

$ 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.

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? :)

Posted on by:

w4ilun profile

w4ilun

@w4ilun

I make things and write about them (╯°□°)╯︵ ┻━┻

Discussion

pic
Editor guide