Wednesday, May 10, 2017

Raspberry Pi 3 with Google AIY VoiceHat

I was lucky enough to get a copy of Issue 57 of The MagPi with the VoiceHat from Google on the cover.

This is a great bit of kit with Raspberry Pi Hat, speaker, microphone, and a cardboard box to put it in.
For a bit of fun I fitted it into a cardboard R2D2 that I had from before Christmas.

This is not the droid you're looking for
After doing the usual of asking it to tell a joke and give me the weather and details of what's nearby I was wondering what I could use the VoiceHat for.

We have an Amazon Dot and it's main purpose in the house is playing radio station.  I tried this out and nothing played.  Seems streaming radio support isn't built in.

Then keeping up with what others were doing Mike Redrobe posted on the Raspberry Pi forum that he had it playing YouTube audio. https://www.raspberrypi.org/forums/viewtopic.php?f=114&t=182665

With a bit of assistance I got this working.  If you're careful with the name of the video you can get it to play fairly much any song.  My kids loved this.

But I still wanted to play streaming radio.  You know, one command and it's off rather than needing to keep telling it new songs to play.

In a later post Mike mentions using VLC as the player and armed with his example I began looking to see if I could get streaming radio to work.

I did. Yeah! Then I went on the trail to add playing podcasts.  Again a success.
You need to install VLC

sudo apt update
sudo apt upgrade
sudo apt install vlc

Radio Stations supported are:
Absolute Radio
Absolute 80s
Absolute 90s
Absolute 00s
Eagle Radio
BBC Radio 1
BBC Radio 2
BBC Radio 3
BBC Radio 4
Capital FM


Podcasts Supported are:
Good Job Brain
No Such Thing As a Fish
Freakonomics


Here is a little video of it working.  I recorded it in Portrait as the cardboard R2D2 it is in is portrait shared so if viewing on a phone will fill the screen, while of viewing on a computer it will have the side bars which is probably better than seeing the messy table.




The action.py file that's needed with the commands built in is available on GitHub
https://github.com/winkleink/RPi_AIY


Other useful bits and pieces

Knowing what Google is actually hearing

While setting up the radio stations Absolute 90s was interpreted by Google as Absolute 90s, but Absolute 00s was interpreted as absolute noughties
It took me a while to figure this out and command that helped me was:

sudo journalctl -u voice-recognizer -n 20 -f

This shows what's happening when you issue a command,
Here's a small snippet showing what the voice command is converted to.
Absolute 00s or is that Absolute Noughties

Make sure to stop and start the service after each change to action.py

sudo systemctl stop voice-recognizer && sudo systemctl start voice-recognizer

Want to add more radio stations
This website has a list of streaming radio stations and their urls.  This is the UK list.

Stopping the playback

Since I'm using the button to stop the playback based on Mike Redrobe's code I decided to do the GPIO setup at the top so it is shared by all the commands rather than having it in each command.

The code is a while loop that keeps going until the button is pressed and then a kill statement to stop the process.  To be truthful I'm not sue how the kill bit works, I just know it does, so thank you to Mike for sharing.

while gpio.input(23):
    time.sleep(1)


pkill = subprocess.Popen(["/usr/bin/pkill","vlc"],stdin=subprocess.PIPE)
p.kill()


Doing the podcasts
Getting the most recent podcast to play is a bit more hacky than the radio stream as the radio stream is always the same while each podcast episode has a different url.  To achieve this I first had to find the relevant rss feed and then parse the file to find the start and end of the url to the mp3.  audioboom.com podcasts aren't too bad, but the Freakonomics one based off the feedburner rss was a little more complex as the url to the mp3 is actually a redirect url and they don't work.

https://www.podtrac.com/pts/redirect.mp3/audio.wnyc.org/freakonomics_podcast/freakonomics_podcast050317.mp3

You can see in the middle there is redirect.mp3 and then what looks like a path to a different website audio.wnyc.org
This meant the parser had to first find "/redirect.mp3/" and then from there find "mp3" to get the main bit for the audio.wnyc.org link.
Finally it needed to add back in "http://"
You can see how this is done in the code below.

url = 'http://feeds.feedburner.com/freakonomicsradio?format=xml' response = urllib.request.urlopen(url) data = response.read() # a `bytes` object text = data.decode('utf-8') startmp3 = text.find('/redirect.mp3/')+14 endmp3 = text.find('.mp3',startmp3+16)+4 if startmp3 > 0: command = "http://"+text[startmp3:endmp3]


Have a play and see what you can get the Raspberry Pi AIY to do.
Just be sure to backup any files you modify before changing them.
I didn't backup my action.py when I started playing but luckily google has made the original files available on GitHub https://github.com/google/aiyprojects-raspbian

I'm really looking forward to seeing how this grows and expands.

Just saw that https://twitter.com/k_tinkerer has been doing some great things with the AIY as well.  Worth checking out if you want to do more.

http://ktinkerer.co.uk/

I'll definitely be adding the shutdown and reboot commands from here.


No comments:

Post a Comment