Why build your own google home?

I currently have an amazon echo and I was wondering if the google home might be any better at recognising what I’m saying. I decided that I should build one and test the google home ai before I decide whether to continue with amazon, or go with google.

Items needed:

Rapsberry pi USB microphone 3.5mm Speaker

Download and install the latest raspbian

Note that we will be installing the full raspbian for this (not lite). I’ve covered this type of task before here: install raspbian jessie on your Pi. It’s still relevant even though we currently have raspbian stretch.

Once completed and booted Sound

sudo apt-get update
sudo apt get upgrade

Install and test microphone

I picked up a very cheap usb microphone that looks like this:

01 usb microphone

Plug the usb microphone in and run lsusb in the terminal. I can see a “C-Media Electronics” device in the list so I’m assuming that this is my microphone.

02 list of usb devices

next open the alsamixer:

alsamixer

Press F6 and select the usb pnp device Increase the gain Esc

03 Altering the gain on the alsamixer

arecord -l

I can see the device listed as card 1:

**** List of CAPTURE Hardware Devices ****
card 1: Device [USB PnP Sound Device], device 0: USB Audio [USB Audio]
Subdevices: 1/1
Subdevice #0: subdevice #0

I can now do a test record with it:

arecord /home/pi/Desktop/a.wav -D sysdefault:CARD=1

Talk into the mic and then hit ctrl+c when finished. Plug your speaker into the 3.5mm jack (or some headphones) and test with the following:

omxplayer -p -o hdmi /home/pi/Desktop/a.wav

You should hear the recording back. And now my microphone is set up.

Add a file into your home directory called .asoundrc that contains the following:

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 "hw:0,0"
  }
}

make it default

Create google project

Go to: https://console.cloud.google.com/project and click “create project”. (You may need to sign in if you aren’t currently logged into your google account).

04 creating the google project

You can name the project whatever you would like to. I’m calling mine “voice-pi”. Click create and you will be taken to your list of projects. Note that the new project may take a moment to show in the list. If you don’t see it after a few seconds, hit refresh in the browser, hopefully then you should see your new project.

05 list of projects

Go to the accordion menu and select Api & Services -> dashboard -> library

06 library

Make sure you have your newly created project selected in the drop downloaded

Search for “google assistant” in the search area

07 google assistant

Click on “google-assistant” and then click “enable”

08 enabling

Create credentials for the pi

Click on “credentials” from the “Api & Services” menu and select “Oauth consent screen” from the top

Name your credentials (I called mine “voice-pi” so it matched the project for easy reference), then click save

09 credentials creation

Now click the “create credentials” button in the middle and select “Oauth client ID”

10 Oauth

Select “Other” and I am going to name it the same again “voice-pi” then click “create”

11 client id

Click ok to dismiss the popup box and then download the credentials as a json file by clicking the download icon on the right

12 popup box

Save the file to disk. If you’re doing this on another computer, you will need to copy the json file onto your pi.

13 Download credentials json file

Check account permissions are active

Go to https://myaccount.google.com/activitycontrols

The activities we need enabled are:

  • web activity
  • location history
  • device information
  • voice and audio activity

14 Account permissions

Open a terminal on your pi and install the following:

// note, these below two were already installed on my pi
sudo apt-get install python3-dev python3-venv

15 python environment tools

Now run the following:

python3 -m venv env
env/bin/python -m pip install --upgrade pip setuptools
source env/bin/activate
python -m pip install --upgrade https://github.com/googlesamples/assistant-sdk-python/releases/download/0.3.0/google_assistant_library-0.0.2-py2.py3-none-linux_armv7l.whl
python -m pip install --upgrade google-auth-oauthlib[tool]

Saw an error here (see screen shot). I continued and it didn’t seem to affect anything. The install appeared to complete after the error.

16 on screen error

// replace below with your client secret file
google-oauthlib-tool --client-secrets /home/pi/client_secret_xxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com.json --scope https://www.googleapis.com/auth/assistant-sdk-prototype --save --headless

Hit enter on the last command and then copy the url link out of the terminal and open it in your browser.

Select the account you created the project with (if asked)

Click allow

17 Account linking to pi

Copy the code returned and paste it back into your terminal

18 Generated code

19 Paste back into terminal

Give it a try

Now in your terminal start the application (assuming you’re still in the python environment if not get back into it by entering source env/bin/activate and hitting enter in the terminal and then enter the following):

google-assistant-demo

NOTE to exit google assistant type Ctrl+c and to then exit python env enter: deactivate

20 Ready to accept request

Now ask it a question by using the invoke term “ok google” or “hey google” followed by your question. You should see the terminal change and then display what you said before responding with the answer (through the speaker).

get next screenshot to show what is on the screen when making a request

Install speaker

I have a speaker and a hat board on order and I’ll update this post when it arrives.

Hat board ordered insert name Speaker odered insert name

  • Solder hat board to supplied pins

21 Soldered hat

  • Connect to pi:

    Amp Vin to Raspbery Pi 5V Amp GND to Raspbery Pi GND Amp DIN to Raspbery Pi #21 Amp BCLK to Raspbery Pi #18 Amp LRCLK to Raspbery Pi #19

22 Connected up

Run the following:

curl -sS https://raw.githubusercontent.com/adafruit/Raspberry-Pi-Installer-Scripts/master/i2samp.sh | bash

If you’re concerned about running scripts from the internet you can go to https://learn.adafruit.com/adafruit-max98357-i2s-class-d-mono-amp/raspberry-pi-usage where there are detailed instructions so you can do it manually.

You will be asked to reboot.

When it comes back up, run speaker-test -c2 and you should hear some white noise. Ctrl+c will stop it

When I tried to run google assistant again I saw this error:

OpenAlsaHandle PcmOpen: No such file or directory

I think the script I ran updated .asoundrc so I’m changing it to the following:

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 "hw:0,0"
  }
}

Now when I run the assistant it works correctly and sends the sound back through the speaker.

Pops and clicks on starting

I found that there was a pop the very first time that you ask google something. After this, it seemed to have gone.

A quick note on localisation

I found that the weather was being given to me in Fahrenheit. Being based in the UK, I prefer celsius so to switch it I simply invoked the assistant and then said:

‘change my units to Celsius’

After this the weather was reported in Celsius.

Outro blurb

On further testing, I’ve noticed that the speaker pop only seems to be on the first use. I’m not sure how long before it will pop again but I will test it for a while to see.

This is a fun little project but I think that the release of the google home mini (very soon whilst I’m writing this) may render it redundant. I think this one may end up as a second google home device.

I think a nice further part to this project would be to attempt to get it working as a service and with the google home app. Not sure if this is possible but would at least be worth a read.