A Raspberry Pi Google Home.

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

  1. Start your Raspberry Pi and connect the USB microphone and the External speaker.
  2. Open terminal and type arecord -l
  3. Identify your USB Microphone and note down its device and card number.
  4. Next, Type in aplay -l
  5. 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.
  6. 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.
  7. 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 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.
  1. Use the command speaker-test -t wav to ensure that the speakers are working.  To increase or decrease the volume, use the command alsamixer.
  2. 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 in aplay --format=S16_LE --rate=16000 out.raw.
  3. Now once the audio is working properly fine, We can finally start setting up the assistant.

Setting up the Project

  1. Go to Actions on Google to create a new project. Log in to your google account and give your project a name. Click Create.
  2. 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.
  3. 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.
  4. Now go the credentials tab, Click create credentials. From the drop-down, choose OAuth client ID. Select application type as other, click create.
  5. On the credentials page, Scroll down and download the JSON file.
  6. 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 

  1. First update the Pi’s package list using the command sudo apt-get update
  2. 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
  3. 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 and then finally Enter.
  4. Run the command sudo apt-get install python3-dev python3-venv to install Python and Python Virtual Environment.
  5. Enable it by running the command python3 -m venv env.
  6. Run the command env/bin/python -m pip install --upgrade pip setuptools –upgrade to upgrade to the latest versions of pip and setuptools.
  7. Type the command source env/bin/activate to get into the new Python environment that we have setup.
  8. 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

  1. Use the command python -m pip install --upgrade google-auth-oauthlib[tool] to install the authorizing tool.
  2. 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

  1. Open terminal and type source env/bin/activate
  2. 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

Congratulations! You now have a working Google Assistant that you made on your own!

 

17 Responses

  1. Raman Khurana says:

    Great to see a young kid blogging and experimenting with technology.
    You are on the right path, keep it up. Best Wishes.

  2. Indrajeet says:

    Excellent! Way to go..Good Luck

  3. Amit Sharma says:

    Great going kid…

  4. Manish says:

    How you approach a thought is really impressive. You are doing great. Keep it up and moving

  5. ANAND AGRAWAL says:

    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.

  6. pradeep Singh says:

    Great tutorial kid. Keep it up!

  7. Vaibhav Saxena says:

    Good Job!

  8. John Terry says:

    Good job, Naman!!

  9. Shauvik says:

    Great Work Kid!!!

    Never Stop learning!!

  10. Ashwath Salimath says:

    Lovely work Naman. Great experiment. ?

  11. bhavesh shah says:

    I am so inspired to work hard even more!! Salute to your passion for tech ?

  12. Manuf Rahman says:

    I’m speechless ?

  13. Anish says:

    Awesome .. way u go !!

  14. Avinash Magdum says:

    Great! I would definitely try this.

  15. Bitknol says:

    Awesome buddy. Way to go. Keep it up.

  16. Iqbal Khan says:

    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

  17. Raghu Konka says:

    Awesome. You are an inspiration to many. Keep it going !!

Leave a Reply

Your email address will not be published.