Still playing with the Google Home Assistant that runs on one of my Rapberry Pi's. And while playing I realised I missed something. I am planning to build the Assistant into a dummy mannequin head or into a mirror. However when I speak the words 'OK Google' I do not have a visible feedback telling that the Raspberry Assistant is actually reacting, so I have no way to see if it was triggered.
What I need therefore is a led that goes on whenever the Assistant starts listening and goes off when the last sentences have been spoken by the Raspberry Assistant. That's a good way of getting a feedback.
The Google Assistant kit that was supplied by the Raspberry Magazine MAGPI does have a visible feedback in the form of a led. I am however using the Google SDK version which works different.
So switch over to the Magpi version you would say. However the Magpi version is triggered by a button and not by speaking to it and further it has a rather nasty voice when it does not understand something. Overall it is a lesser quality version as the Google SDK is.
So let's attach a led.
First step is to attach a LED to the Raspberry Pi.
The LED is attached to IO pin 18 and through a limiting resistor of 1K to ground. Thats all.
The Magpi version uses pin 25. So for avoiding future compatibility issues you could use pin 25. However make sure that you alter the software accordingly.
The Software Patch
The Raspberry Assistant is for the main part written in Python which is great for us, as this can be altered easily.
To adress the I/O pins of the Raspberry, what we need to do to control the LED, you will need to use the Python GPIO library.
There is however a small problem.
The Raspberry assistant runs from within an ENV. The GPIO library is not directly available in that ENV. So we first have to make it available.
Open the file-manager which can be found at the top-right part of your screen.
Make a new folder called RPi and copy the contents of /usr/lib/python3/dist-packages/RPi to the new RPi folder.
The last step is to alter __MAIN___.PY so that it turns on the led when the conversation starts and turns it off again when it ends.
You can find this file here: /home/pi/env/lib/python3.4/site-packages/google/assistant
Double click on __main__.py and it will open in an editor. Now alter the file as in the source code below. Do NOT copy the file below as it may have wrong indents.
Python is very picky about indenting.
The indents in this listing are SPACES not TABS !!!
If you mix the indenting the program will not work, so only use SPACES for indenting.
Choose File/save and all is done.
Let's look at the listing.
23 import RPi.GPIO as GPIO
25 GPIO.setup(18, GPIO.OUT)
26 GPIO.output(18, GPIO.LOW)
Line 23 to 26 are new entered lines.
First the GPIO library is imported. The GPIO mode is set to BCM so the IO pins will match the Raspberry's layout. And then GPIO 18 is defined as an OUTPUT and set in LOW state so the LED will initially be off.
44 if event.type == EventType.ON_CONVERSATION_TURN_STARTED:
46 GPIO.output(18, GPIO.HIGH)
49 if (event.type == EventType.ON_CONVERSATION_TURN_FINISHED and
50 event.args and not event.args['with_follow_on_turn']):
52 GPIO.output(18, GPIO.LOW)
This is where it all happens.
As soon as the conversation starts GPIO 18 will be set HIGH (on) and when the conversation finishes the GPIO 18 is set LOW again (off).
Remember that if you have chosen to put the led on GPIO 25 you will have to later the source code accordingly.
That is all folks.
Restart your Pi and restart the Assistant
Now when you say "OK Google" the led goes on and when the conversation ends the led goes off again. A nice visual feedback that shows that you pronounced "OK Google" the right way so the Assistant understands.
OK, I am off for the summer hollidays.
New stories will be posted in August or so.
Till then: have fun !!!