A Raspberry Pi Google Home.
Introduction
I asked my dad for a Google home, The answer I got was NO. So I made my own using Google APIs and RaspberryPi. Let me show you how I did it.
Required Hardware
- Raspberry Pi
- USB Microphone
- External Speaker (HDMI/Audio Jack/USB)
- A MicroSD card with Raspbian installed on it
Setting up audio devices
- Start your Raspberry Pi and connect the USB microphone and the External speaker.
- Open terminal and type
arecord -l
- Identify your USB Microphone and note down its device and card number.
- Next, Type in
aplay -l
- If your speaker is connected through the audio jack, It will be labeled as bcm2835 ALSA or Analog. The HDMI output is labeled as bcm2835 IEC958/HDMI. Again, note down its device and card number.
- Now once we have the device and card numbers, we will need to create a file named .asoundrc in the home directory which will define the devices which should be used by the audio driver. Run the command
nano /home/pi/.asoundrc
to create this file. - Put this code into .asoundrc
pcm.!default {
type asym
capture.pcm "mic"
playback.pcm "speaker"
}
pcm.mic {
type plug
slave {
pcm "hw:<crdno>,<devno>"
}
}
pcm.speaker {
type plug
slave {
pcm "hw:<crdno>,<devno>"
}
}
Make sure to replace <crdno>
and <devno>
with the with the device and card numbers you noted down.
Exit the editor by CTRL + X then Y and ENTER at the last.
Testing the audio devices
Before we get started setting up the google assistant, we will first check if the microphone and the speaker are working properly.
- Use the command
speaker-test -t wav
to ensure that the speakers are working. To increase or decrease the volume, use the commandalsamixer
. - Let’s now test the microphone by using the command
arecord --format=S16_LE --duration=10 --rate=16000 --file-type=raw out.raw
.This will record a 10sec audio from your USB microphone. To play the recorded audio, type inaplay --format=S16_LE --rate=16000 out.raw
. - Now once the audio is working properly fine, We can finally start setting up the assistant.
Setting up the Project
- Go to Actions on Google to create a new project. Log in to your google account and give your project a name. Click Create.
- Click Device registration, then register model. Fill in the product name and manufacturer name then choose the device type as a speaker. Note down the device model id. We will need this later.
- Head to the API manager and enable the google assistant API. Once enabled, click manage, then credentials, and go to the OAuth consent screen. Choose your E-Mail ID and give a product name. Click Save.
- Now go the credentials tab, Click create credentials. From the drop-down, choose OAuth client ID. Select application type as other, click create.
- On the credentials page, Scroll down and download the JSON file.
- Make sure you have Enabled Web & App Activity, Device Information, and Voice & Audio Activity in the Activity Controls of your account that you have used to sign in to the Cloud Console.
Installing the Libraries
- First update the Pi’s package list using the command
sudo apt-get update
- We will now be creating a folder where we will store the credentials which we downloaded earlier. To create the folder, type in
mkdir ~/pihome
- Enter the command
nano ~/pihome/credentials.json
and paste the contents of the json file we downloaded earlier. Save the file by pressing Ctrl + X then Y and then finally Enter. - Run the command
sudo apt-get install python3-dev python3-venv
to install Python and Python Virtual Environment. - Enable it by running the command
python3 -m venv env
. - Run the command
env/bin/python -m pip install --upgrade pip setuptools
–upgrade to upgrade to the latest versions of pip and setuptools. - Type the command source
env/bin/activate
to get into the new Python environment that we have setup. - Now we need to Install the library for the Google Assistant. Run these commands to install them.
python -m pip install --upgrade google-assistant-library
python -m pip install --upgrade google-assistant-sdk[samples]
Authorizing your Pi
- Use the command
python -m pip install --upgrade google-auth-oauthlib[tool]
to install the authorizing tool. - With the tool now installed, We will use these commands to authorize our pi.
google-oauthlib-tool --client-secrets ~/pihome/credentials.json \
--scope https://www.googleapis.com/auth/assistant-sdk-prototype \
--scope https://www.googleapis.com/auth/gcm \
--save --headless
You will now get a URL, copy that into your browser and log in with the same Google account you created your project with. You will now be presented with an authentication code, copy that and paste it into the terminal window. You will now get a message saying credentials saved.
Voila! You have now setup the Google Assistant on your Pi!
Firing up the Google Assistant
- Open terminal and type
source env/bin/activate
- To start the google assistant, Run the following command and make sure to replace
<projectid>
with your own project ID and<deviceid>
with your own device model ID which we noted down earlier.
googlesamples-assistant-pushtotalk --project-id <projectid> --device-model-id <deviceid>
This is a one time process, so next time when you want to use the google assistant, use the command source env/bin/activate
then the command googlesamples-assistant-pushtotalk
17 Responses
Great to see a young kid blogging and experimenting with technology.
You are on the right path, keep it up. Best Wishes.
Excellent! Way to go..Good Luck
Great going kid…
How you approach a thought is really impressive. You are doing great. Keep it up and moving
Seems quite interesting, one would never have this crazy idea doing this using raspberry pi. If you ever interested in deep learning works. Lets drill down on one or two projects together.
Great tutorial kid. Keep it up!
Good Job!
Good job, Naman!!
Great Work Kid!!!
Never Stop learning!!
Lovely work Naman. Great experiment. ?
I am so inspired to work hard even more!! Salute to your passion for tech ?
I’m speechless ?
Awesome .. way u go !!
Great! I would definitely try this.
Awesome buddy. Way to go. Keep it up.
Naman,
Great work!
Btw, have you experimented with Intel NUC for home projects. I started working with it about a year ago but got busy with other things and couldn’t go further.
Iqbal
Awesome. You are an inspiration to many. Keep it going !!