Introduction: DIY Google Home With Bluetooth Speaker on Raspberry Pi Zero Docking Hub

We have an instructable on DIY Amazon Echo Alexa - Alexa Voice Assistant on Raspberry Pi Zero Docking Hub. This time we want to show you how to build a DIY Google Home. In this instructable, we will show you how to install and setup Google Assistant on a Pi Zero W with MakerSpot's Raspberry Pi Zero Docking Hub and Bluetooth speaker.

Let's get started.

Step 1: Get These Parts

These are the parts you need:

  1. 1x Raspberry Pi Zero W
  2. 1x Raspberry Pi Zero Docking Hub
  3. 1x HDMI monitor
  4. 1x HDMI cable (please note Pi Zero W requires a mini-HDMI connector)
  5. 1x 5v USB 1 A Power Adaptor
  6. 1x micro USB cable
  7. 1x USB keyboard
  8. 1x USB mouse
  9. 1x Mini-Microphone For Mobile Phone/Tablet
  10. 1x Bluetooth Speaker
  11. 1x 8G micro SD card
  12. PC (for flashing the SD card with Raspbian OS image)

Step 2: Prepare SD Card With the Latest Raspbian OS (Stretch)

It would be a good idea to start with a fresh Raspbian OS. There are a few ways to prepare a new Raspbian OS on an SD card. But I found that using Etcher with a full Raspbian image is efficient and less error-prone.

  1. Download and install Etcher (https://etcher.io/) for your host PC.
  2. Download the latest Raspbian (Stretch) image from http://downloads.raspberrypi.org/raspbian/images/... the SD into your PC
  3. Open Etcher, select the downloaded image, then the SD card drive, and press Flash!

Once the image is prepared, eject the card safely and get ready for next step.

Step 3: Setup the Pi and Docking Hub

You need to install your Pi Zero W on the Raspberry Pi Zero Docking Hub. There are 4 sets of screws and standoffs and it will take less than a minute to assemble.

Insert the prepared SD card into the Pi Zero W. Connect your monitor to the Pi Zero W's HDMI port (must be done before powering up the Pi), connect the USB keyboard and mouse and finally connect the microphone. We are using the neat Saramonic Mini Directional Microphone for Smart Phone.

To power up, connect the 5v USB power cable to the power port on the docking hub (NOT THE PWR PORT ON THE PI). You should see the normal Raspbian OS coming up on the monitor.

Step 4: Configure the Pi

Setup WiFi.

Left mouse click over the WiFi icon on the top bar. Choose your network to connect. You only need to do once unless the network setting has changed or need to be changed.

Disable HDMI/Analog Audio

This step is important to get the Raspberry Pi Zero Docking Hub audio to work with the Google Assistant software.

Start a terminal and edit /boot/config.txt

sudo nano /boot/config.txt

Disable the analog and hdmi audio by inserting '#' in front of the following line in the file:

#dtparam=audio=on 

Press ctrl-x, y, and enter to save.

Enable SSH/VNC (Optional)

If you don't want to use the monitor, keyboard, and mouse at next startup, enabling these options would allow you to remote access the Pi. These options are under Preference/Raspberry Pi Configuration, then go to Interfaces and checkmark the SSH and VNC options.

Reboot the Pi to take the setting in effect.

Setup Bluetooth Speaker

After reboot and the desktop screen comes back, pair with your Bluetooth speaker.

  1. Go to the Bluetooth icon on the top menu bar, turn on Bluetooth and then add a Bluetooth device.
  2. Put the Bluetooth speaker in pairing mode
  3. You should see the speaker discovered. Highlight the speaker entry and click Pair.
  4. You will get a successful pair message but the speaker is not connected yet. Go to the speaker icon on the top menu bar. Click on the Bluetooth speaker. Your speaker should produce a chime or voice notification to indicate the Bluetooth connection is successfully made.

Change Audio Setting

After the Bluetooth speaker is connected, the .asoundrc file will be generated with the Bluetooth speaker info in it. You need to modify this file to set up the built-in microphone on the docking hub.

The original ~/.asoundrc file looks like this:

pi@raspberrypi:~ $ cat ~/.asoundrc
pcm.!default {
  type plug
  slave.pcm { 
    type bluealsa device "40:00:88:00:18:0E"
    profile "a2dp" 
  }
} 
ctl.!default {
  type bluealsa
}

You need to modify it to look like below. Your copy of .asoundrc should be exactly the same as below except the Bluetooth address "40:00:88:00:18:0E", which should come from your original.

pcm.!default {
  type asym
  capture.pcm "mic"
  playback.pcm "speaker"
} 
pcm.mic {
  type plug slave { pcm "hw:1,0" }
}
pcm.speaker {
  type plug 
  slave.pcm { 
    type bluealsa device "40:00:88:00:18:0E"  
    profile "a2dp" 
  } 
}

Finally, save a copy to /etc/asound.conf and prevent being overwritten

sudo cp ~/.asoundrc /etc/asound.conf
chmod a-w ~/.asoundrc

Step 5: Install Google Assistant Software

Prepare Google Project and Account

Before you install the Google Assistant software, you need to configure a developer project and account settings. Click this link and follow the steps there. Once finished, come back here.

Setup Virtual Environment

Open a terminal and follow the steps to set up the virtual environment

sudo apt-get update
sudo apt-get install python3-dev python3-venv
python3 -m venv env
env/bin/python -m pip install pip setuptools --upgrade
source env/bin/activate

Install More Prerequisite Libraries

On the same terminal, type:

sudo apt-get install portaudio19-dev libffi-dev libssl-dev
pip install wheel

Install Google Assistant SDK

On the same terminal, install the Google Assistant SDK and the oauth tools. The last command requires the client secret file generated while preparing Google Project and account.

python -m pip install google-assistant-sdk[samples]pip install --upgrade google-auth-oauthlib[tool]
google-oauthlib-tool --client-secrets path/to/client_secret_XXXXX.json --scope https://www.googleapis.com/auth/assistant-sdk-prototype --save --headless

Checking It Out

Google Assistant should be working at this point. You can verify it by issuing the following command.

googlesamples-assistant-pushtotalk

Next step will be installing the wake word engine - snowboy - so you don't need to press enter to activate.

Step 6: Install Snowboy Wake Word Engine

Clone the Snowboy repository as follows:

make -p ~/Development/Assistant 
cd ~/Development/Assistant
git clone https://github.com/Makerspot/snowboy.git

To make Snowboy working with Raspbian Stretch, you need to rebuild the _snowboydetect.so for python3

sudo apt-get install swig3.0 python-pyaudio python3-pyaudio soxsudo libatlas-base-dev
pip install pyaudio
sudo ln -s /usr/bin/swig3.0 /usr/local/bin/swig
cd ~/Development/Assistant/snowboy/swig/Python3
make

Now you can run the Google Assistant using the "OK Google" wake word.

cd ~/Development/Assistant/snowboy/examples/Python3
python assistant_wrapper.py resources/OK\ google.pmdl

The "OK Google" wake word model file is a personalized model which may not work well for you. If you find the wake word doesn't work well, you may consider to train your own model and replace the "OK google.pmdl" file. Go to https://snowboy.kitt.ai/ to train your own model. You can even pick your own wake word - it doesn't have to be "OK Google".

Step 7: OK Google, Sing a Song

Congratulation! Say "OK Google" (or whatever wake word you have installed), wait for the Ding prompt, then ask Google Assistant with your question.

If you have enabled SSH (or VNC server), you can restart the Pi and run the Google Assistant software headless (without the monitor/keyboard/mouse). In your PC start an SSH terminal and connect to the Pi.

First, make the Pi Bluetooth to auto connect the speaker (only need to do it once).

echo -e "connect <bt speaker mac address>" | bluetoothctl
echo -e "trust <bt speaker mac address>" | bluetoothctl 

Every time the Pi reboot, it can reconnect to the speaker, however, only when the speaker is powered off and on as well. Then follow the steps below to start Google Assistant.

source ~/env/bin/activate
cd ~/Development/Assistant/snowboy/examples/Python3
python assistant_wrapper.py resources/OK\ google.pmdl