Google Home Controlling a Raspberry Pi

I’ve just got a Google Home Mini speaker thingy and have figured out a relatively simple way to have it send commands to a Raspberry Pi when you say certain phrases to it. The phrases can contain parameters to be sent to the Pi too. There are probably other ways to do this, but this way is fairly easy.

The first step was to look at the google Home API. The second step was to say “That looks like hard work.” and ignore it.

The way I opted for in the end was to use If This Then That. For anyone who’s not heard of it, it’s a web site that allows you to setup little jobs that get triggered by things. You can use a google account to sign into ifttt which is good as it makes life much simpler. You need a google account for the Google Home Speaker anyway.

First make it web controlled

You’ll also need to route web traffic from a fixed URL to your Pi. This can be on a non-standard port but don’t assume that’s any form of security. It’s probably best to avoid anything on there that could be useful to the bad guys or expensive to you. If your ISP doesn’t give you a static IP address, you may need to use a Dynamic DNS provider. I already use dyndns for other things so was covered there.

On the Pi, you’ll need a web server. I use mini-httpd which can be installed as follows

sudo apt-get install mini-httpd
sudo nano /etc/default/mini-httpd

Now set set the START parameter in there to 1.
CTRL X to exit and agree to save the file

sudo nano /etc/mini-httpd.conf

change host=localhost to host=0.0.0.0
change the port if desired
CTRL X to exit and save the file.

sudo service mini-httpd restart

Now you have a web server. Put the Pi IP address into a Raspberry Pi to test it. It should just say It Works.

The next step is to create a CGI program to be run when a URL is requested. I called my script “switch” as I want to use it to control my Christmas lights. On my main site there’s some information about using a Pi to control electrical sockets without risking killing yourself. It all boils down to a command called switch which I use in my script. Here’s how to create the script. The script contents are just below.

cd /usr/share/mini-httpd/html
sudo mkdir cgi-bin
cd cgi-bin
sudo nano switch

My URL will be

http://MYADDRESS/cgi-bin/switch?channel=3&state=on

Here is the script I used.

#!/bin/sh
echo -e content-type: text/plain\\n

myline=$QUERY_STRING
PARAMS=`echo $myline | cut -d \? -f 2`

CHANPARAM=`echo $PARAMS | cut -d \& -f 1`
STATEPARAM=`echo $PARAMS | cut -d \& -f 2`

CHAN=`echo $CHANPARAM | cut -d = -f 2`
STATE=`echo $STATEPARAM | cut -d = -f 2`

echo switch $CHAN $STATE
switch $CHAN $STATE

The first echo tells the browser it’s getting some text back – just useful for debugging really. Next I split the PARAMS off the end of the URL and split them into individual parameters before taking just the part of each one after the equals. I ignore the part before the equals and hope the URL is always the same way around. I create the URL, so this shortcut should be ok.
To make this file executable type this

sudo chmod a+x switch

The script can be tested by typing the url into a browser and checking that the Pi turns the right socket on or off. If not, you can add echo commands around in the script and try to figure out why not, the text gets fed back to the browser for you to investigate.

Now make it voice controlled

Now you have a web controlled Pi, it’s time to get the google box to control it with voice commands.

In ifttt.com, you need to link to your google home account and the webhooks service. To do this go to the My Application section and click the Services link on the right hand side. Linking the services is simple enough once you’ve found them.

Now got to My Applets and click New applet.

Click the +This in the big text and you’ll get a lot of choices for the trigger event. Select Google Assistant and choose Say a phrase with a text ingredient.

For the trigger text, I entered “switch the christmas lights $” without the quotes. this means it will listen for “OK Google. Switch the christmas lights XXXX” where XXXX is any spoken word or phrase. I also set it to use the words Fairy lights in one of the option boxes. For the output I have it say “OK, turning the fairy lights $” and it replaces the $ with whatever you said earlier.

Next you have to pick a That option. Choose the webhooks service and tell it to make a web request. The url will be

http://MYADDRESS/cgi-bin/switch?chan=3&state={{TextField}}

It helps with the textfield part by providing an add ingredient button.

Chose Get for the method and leave the other two boxes alone.

Save it.

Now test it. Just say “Ok Google. Switch the christmas lights on” and the Google Home speaker should send the command to ifttt. Ifttt will send the command back to your Pi and your Pi will switch on your power socket.

If it didn’t work, clues may be lurking in the ifttt activity log or the Pi /var/log/mini-httpd.log file. You can also try adding lines to the script to log something to a file somewhere.

Conclusion

I guess this has been two guides really. CGI scripts on a Pi and Google Home interfacing. It just happens that ifttt provides a fairly simple way to combine the two and end up with voice controlled Christmas lights.

As usual, I like to hope this has been useful but don’t really want anyone to follow it to the letter. I don’t mind if anyone does, but I’d rather provide a bit of inspiration people to create something of their own. It’s far more rewarding all round.

 

Leave a Reply

Your email address will not be published. Required fields are marked *