How to build Raspberry Pi voice control (home automation)

Siri, Amazon Echo and “S Voice” are all voice recognition programs designed to make life easier. Even a Raspberry Pi voice control can be easily built by yourself. Because such a speech recognition is of course very beneficial for home automation.

In this tutorial, I’ll show you how to digitize speech through a microphone, convert it into text, and then respond to it.

 

The voice recognition feature can already be tested here (in Chrome).

Hardware for the Raspberry Pi voice control

Alternatively, you can also take a USB microphone, but I have not tested it. Still, it should work.

If you want to control the GPIOs by voice input, for example, a breadboard and jumper cable is helpful for connecting to the Raspberry Pi.

 

Preparation

Google is the language recognition service. To use the API, you need to join this group with your Google Account.

Next, open the Developer Console and press “Create Project”. Once created, click on “APIs and Authentication” -> “APIs” on the left and search for “Speech API”. You activate this for your project and click on “Access Data” on the left. Here you have to create a new public key (browser key).

You have to insert the now created API key in the script.

It should be said that every day 50 requests are free. If you need more, you can either buy it from Google or create a second project and get another key 😉

Raspberry Pi voice control – software

The principle is the following. An audio file will be created, sent to Google and sent back as text. So let’s start:

sudo apt-get update
sudo apt-get install flac

Now we check if the USB card has been detected correctly:

lsusb

It should be such an entry:

Bus 001 Device 004: ID 1130:f211 Tenx Technology, Inc. TP6911 Audio Headset

Now we have the recording devices output:

arecord -l

For me, the output looks like this. The number is important:

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

 

At this point, I recommend testing the microphone. For me the microphone was muted (which can be reversed with amixer -c 1). So we record a short test file and let it play:

arecord -d 10 -f cd -t wav -D plughw:1,0 test.wav
aplay -f dat test.wav

If you hear something, everything has worked out and it can go on.

 

We create a file that sends and evaluates the request.

sudo nano speech2text.sh

The file has the following content. Above you have to enter your API key.

Afterwards, the script still has to be assigned necessary rights.

chmod +x speech2text.sh

Now it can be started and talked.

./speech2text.sh

I’ve included two example queries in the lower part of the script that can be used to respond to speech input. For example. another script or command could be executed. What you use this for is up to you.

For completeness, here is my test:

pi@raspberrypi ~ $ ./speech.sh
Recording... To stop, press CTRL + C and wait.
^C
Execute...
Google answer: "turn on the light"

Light will switch on

 

If there should be interest, I could also create and post using a smartphone.

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.