How to build your own smart speaker — Google Assistant, Google Cloud, Actions on Google and ReSpeaker Core v2.0 — Part 2
By: Sachin Kumar
Tags: Google Cloud Platform, Python, Chatbots, Technology, Google Assistant

How to build your own smart speaker — Google Assistant, Google Cloud, Actions on Google and ReSpeaker Core v2.0 — Part 2 SACHIN KUMAR Follow Jun 30, 2019 · 5 min read

In this tutorial, we will learn how to build your own smart speaker using Google Assistant and ReSpeaker Core v2.0. Notably, this is a continuation of the part 1 tutorial.

Also, we will embed the Google Assistant and build our own Google Home/Google mini like smart speaker.

Introduction to the Google Assistant Library Setup and Configure — Hardware, Network, and Audio Install the SDK and Sample Code Run the Sample Code Automate — Run your app at Bootup Conclusion References

1. Introduction to the Google Assistant Library

The Google Assistant Library for Python is a turnkey solution for anyone who wants to quickly integrate the Assistant into a project. Further, the library is written in Python and is supported on popular hardware like Raspberry Pi 3, etc.

2. Setup and Configure — Hardware, Network, and Audio

Please refer to the previous tutorial and complete all the steps before you proceed on to the next steps.

3. Install the SDK and Sample Code

3.1 Configure a new Python virtual environment

Firstly, Use a Python virtual environment to isolate the SDK and its dependencies from the system Python packages. So, open the QTerminal and run the following commands one after the other.

For Python 3:

sudo apt-get update

sudo apt-get install python3-dev python3-venv

python3 -m venv env

env/bin/python -m pip install --upgrade pip setuptools wheel

source env/bin/activate

3.2 Get the package

The Google Assistant SDK package contains all the code required to get the Google Assistant running on the device, including the sample code. So, execute the scripts below one by one.

Install Dependencies

sudo apt-get install portaudio19-dev libffi-dev libssl-dev libmpg123-dev

python3 -m pip install --upgrade google-assistant-library==1.0.1

python3 -m pip install --upgrade google-assistant-sdk[samples]==0.5.1

4. Run the Sample Code — Hey Google

Since we did all the groundwork from the previous tutorial and the remaining steps here, we can go ahead and run our sample code.

Change the command googlesamples-assistant-hotword --project-id <my-dev-project> --device-model-id <my-model > with your own IDs.

So, for the command above, change < my-dev-project > into your project-id and change < my-model> into your Model ID .

For this demo, it should be like

googlesamples-assistant-hotword --project-id mysmartspeaker-xxxxx --device-model-id mysmartspeaker-xxxx-my-smart-speaker-xxxxx

You can also run the same program by using the following command.

python3 /home/respeaker/env/bin/googlesamples-assistant-hotword --project-id mysmartspeaker-xxxxx --device-model-id mysmartspeaker-xxxx-my-smart-speaker-xxxxx

Finally, you can trigger the Google Assistant by using the hotword Ok Google/Hey Google.

Try asking the following

Ok Google, Tell me a joke.

Ok Google, Beam me up, Scotty.

5. Automate — Run your app at Bootup

Now that we have the sample app running, we do need to run it all the time and also start automatically on bootup. In order for us to do that, we need to configure a few things. Let’s get started.

5.1 Create a service file.

By default, our device doesn’t do anything when it boots up; you must manually execute a program. But you can make this program (or any other program of your choice) run automatically upon bootup by creating a systemd service.

All you need is a .service configuration file. This file accepts a long list of configuration options.

In the QTerminal, enter the following command.

cd /home/respeaker/env/bin/

sudo vi assistant-on-start.service

Copy-paste the following code into assistant-on-start.service file. Make sure to replace the project id and device-model-id with yours.

[Unit]

Description=My Assistant Smart Speaker

Wants=network-online.target

After=network-online.target

Wants=systemd-timesyncd.service

After=systemd-timesyncd.service



[Service]

Environment=DISPLAY=:0

ExecStart=/bin/bash -c '/home/respeaker/env/bin/python3 -u /home/respeaker/env/bin/googlesamples-assistant-hotword --project-id mysmartspeaker-xxxxx --device-model-id mysmartspeaker-xxxx-my-smart-speaker-xxxxx'

WorkingDirectory=/home/respeaker/env/bin/

Restart=always

User=respeaker



[Install]

WantedBy=multi-user.target

To put this file into action, you need to put it into the /lib/systemd/system/ directory. But instead of actually moving this file there, you can create a symbolic link (a “symlink”) in this directory that points to the file that already exists. You can do this as follows:

# Create the symlink

sudo ln -s `pwd`/assistant-on-start.service /lib/systemd/system # Reload the service files so the system knows about this new one

sudo systemctl daemon-reload

Now we need to tell the system to run this service on bootup:

sudo systemctl enable assistant-on-start.service

All set! You can try rebooting now to see it work.

Or manually run it with this command:

sudo service assistant-on-start start

Note: Even after the prompt returns from this command, it takes a few moments for the program to start.

If you want to stop the service from running on bootup, disable it with this command:

sudo systemctl disable assistant-on-start.service

And to manually stop it once it’s running, use this command:

sudo service assistant-on-start stop

You can check the status of your service with this command:

sudo service assistant-on-start status

6. Conclusion

To conclude, we have successfully embedded the Google Assistant into our smart speaker. We have also automatically set it to run all the time and also automatically start on boot. So we now have a Google Home/Google mini like device of our own which does voice recognition using ReSpeaker Core v2.0 featuring a 6 array mic and speech algorithms. In the next upcoming tutorials, we will be extending the device to do more like connecting to a Bluetooth speaker, making it portable, etc. Until then, check out my other tutorial posts on medium and on my website techwithsach.com.

7. References