Introduction: Pi-Assistant
This is Google Assistant project using Raspberry Pi 3 A+ board.
This was design for my colleges IEEE project so people will be more interested in technology and making stuff.
I will go though the basic installation of the OS for the raspi, setting up google assistant on the raspi, and auto start.
Lets start!
Step 1: Parts List
We will be using Raspberry Pi 3 A+ board
The reason using the A+ board is just because I was it is cheaper than the B board and I was wanting to use it since it just got launched.
1x Raspberry Pi 3 A+
1x Micro USB cable (for power)
1x ethernet cable
1x USB to ethernet
1x USB hub
1x Microphone
1x Speaker
It will be useful if you can get a USB hub + Ethernet port cable.
Also, you will need another computer to work on this project.
Step 2: Raspi Setup
You will need to install Rasbian OS on the sd card.
Go to https://www.raspberrypi.org/downloads/raspbian/ and download the latest Rasbian.
MAC:
Use Etcher and burn the img file on the sd card.
*The sd card will be erased make sure to take a backup if needed.
Windows:
Use Rufus and burn the img file on the sd card.
*The sd card will be erased make sure to take a backup if needed.
---------------------------------------------------------------
Enable SSH by placing a file named “ssh” (without any extension) onto the boot partition of the SD card
If you are going to use a monitor you do not need to do this.
Step 3: Connect Hardware
Now connect the hardwares together.
Use a usb hub and connect the mic and the ethernet cable. Plug the speaker into the 3.5mm stereo jack.
Now connect the other side of the ethernet cable to your computer.
Finally, plug in the micro usb cable to the raspi.
Step 4: Login to Raspi
Open terminal or use putty and ssh in to the raspi
type
ssh pi@raspberrypi
To log in as
username: pi
password: raspberry
Now you are in raspi!
You can go to sudo raspi-config to change the password and connect to wi-fi.
Step 5: Setting Up Audio
You will need to configure the audio system on the raspi to be able to use google assistant sample code.
Type
arecord -l
aplay -l
and write down the card number and the device number.
For the speaker, you will want to choose the on that says bcm2835 ALSA.
Then you will make a file .asoundrc under /home/pi
Type
nano .asoundrc
Now copy paste the code below and replace the card number and the device number with your number.
pcm.!default {
type asym
capture.pcm "mic"
playback.pcm "speaker"
}
pcm.mic {
type plug
slave {
pcm "hw: card number, device number"
}
}
pcm.speaker {
type plug
slave {
pcm "hw:card number, device number"
}
}
Now use the code below to test the functionality of the speaker and the mic.
speaker-test -t wav
arecord --format=S16_LE --duration=5 --rate=16000 --file-type=raw out.raw
aplay --format=S16_LE --rate=16000 out.raw
Step 6: Install the SDK and Sample Code
Install the SDK and the sample code on the raspi by running these commands.
First you will install Python 3
sudo apt-get update
sudo apt-get install python3-dev python3-venv # Use python3.4-venv if the package cannot be found.
python3 -m venv env
env/bin/python -m pip install --upgrade pip setuptools wheel
source env/bin/activate
Get the Google Assistant packages
sudo apt-get install portaudio19-dev libffi-dev libssl-dev libmpg123-dev
python -m pip install --upgrade google-assistant-library<br>
python -m pip install --upgrade google-assistant-sdk[samples]
Step 7: Register Your Pi-assistant
You will need to register your project and the device to be able to use Google Assistant.
Step though the following instructions.
1. Enable Google Assistant API
b. Click on Add/import project.
c. Create a new project, type a name in the Project name box and click CREATE PROJECT.
d. Click Device registration near the bottom of the page.
e. Enable the Google Assistant API
Go to link and Click Enable.
f. You must configure the OAuth consent screen for your project in the Cloud Platform Console.
2. Re-open Action Console to register the device model.
a. Fill in information
b. Once you are done Click Register Model
c. Next you will download the credentials
You will also need to place this file on the raspberry pi
To do this, you can type the command in terminal (replace client-id with your own id)
scp ~/Downloads/client_secret_<strong><em>client-id</em></strong>.json pi@raspberrypi-ip:/home/pi/Download
d. You can skip the Specify traits
e. If you edit the model you will need to re-download the credencial
Step 8: Generate Credentials
Install or update the authorization tool:
python -m pip install --upgrade google-auth-oauthlib[tool]
Generate credentials to be able to run the sample code and tools. Reference the JSON file you downloaded in a previous step; you may need to copy it the device. Do not rename this file.
google-oauthlib-tool --scope https://www.googleapis.com/auth/assistant-sdk-prototype \<br> --scope https://www.googleapis.com/auth/gcm \ --save --headless --client-secrets /path/to/client_secret_client-id.json
Step 9: Try the Sample Code
Now you can run the sample program.
To do this run the following command replacing the my-dev-project and my-model
googlesamples-assistant-hotword --project-id my-dev-project --device-model-id my-model
Once it starts running try
Hey Google whats the weather like?
What time is it?
If it gives you an error about audio try and run this command
sudo apt-get install matrixio-creator-xxxx
Step 10: Setup Autostart: Generate Script
To make the raspi able to automatically start the google assistant software, we will edit the autostart file.
First make a script called google_autostart.sh
nano google_autostart.sh
Then you will type
----------------------------------
#!/bin/bash
source env/bin/activate
google-assistant-demo&
-----------------------------------
& at the end of the line will make the software run on the background.
When ever you make a script, the file won't have permission to execute.
You can check by running
ls -l google_autostart.sh
it should result you with
-rw-r--r-- l pi pi date time google_autostart
To give this script permission to be a script run
sudo chmod +x google_autostart.sh
Now if you check the file the color of .sh file should change and say
-rwxr-xr-x l pi pi date time google_autostart.sh
Try and if it works you successfully generated a script file to auto start google assistant.
./google_autostart.sh
Step 11: Setup Autostart: Set the Script to Autostart
Now you have to set the script in to the start up file in the raspi.
Go to
/etc/xdg/lxsession/LXDE-pi/
then
nano autostart
in the file, add the directory and the script information on the last line.
/home/pi/google_autostart.sh
Now you should be able to unplug the ethernet cable and only have the speaker, mic and the power on the usb and the google assistant software should automatically startup.
Step 12: Extra
The instructions we went though here is just for the basic Google assistant sample code.
You can upgrade the software by installing different libraries.
The link below will add you pi-assistant little more fetures
https://github.com/googlesamples/assistant-sdk-pyt...
If you set up the Google Cast SDK you will be able to do things like
Ok Google, play Spotify
You can also use google assistant and the other pins and ports on the raspi to do more Actions
like controlling LEDs, Motors, and anything you can think of !!!