Google Assistant on Raspberry | Part 4: Modifications

Words
502
Reading
3 min
Listen
Play
8y

1.jpg

Repository

https://github.com/google/aiyprojects-raspbian

What Will I Learn?

  • You will learn how to modify the Assistant to read headlines
  • You will learn how to play radio on Assistant

Requirements

Difficulty

Intermediate

Make sure you are using assistant_library_with_local_commands_demo.py as Assistant main.py file (you can learn more about these files from my previous tutorials, see in Curriculum).

Headlines reader

Source: https://github.com/ktinkerer/aiyprojects-raspbian/blob/voicekit/src/headlines.py

  • Open new terminal window on your Raspberry and install Feedparser. It is a Python library which allows to parse feeds in many kinds of formats
sudo pip install feedparser
  • Open Assistant main file
sudo nano /home/pi/voice-recognizer-raspi/src/main.py
  • Add necessary library
import feedparser

2.png

  • Define a function which will be reading headlines
def headlines():
        url = "http://feeds.bbci.co.uk/news/rss.xml"
        number_of_headlines = 3
        feed = feedparser.parse(url)
        headlines = ""
        for i in range(0, number_of_headlines):
                title = feed.entries[i].title
                summary = (feed.entries[i].summary)
                headlines = headlines + " " + title + " " + summary
        aiy.audio.say(headlines)

3.png

  • url = "http://feeds.bbci.co.uk/news/rss.xml" - variable to store url to the headlines

  • number_of_headlines = 3 - number of headlines that will be readed

  • feed = feedparser.parse(url) - variable to store parsed feed from the url

  • headlines = "" - empty variable to store headlines

  • for i in range(0, number_of_headlines): - loop that repeats as many times as you set in number_of_headlines

  • title = feed.entries[i].title - variable to store title of the article

  • summary = (feed.entries[i].summary) - variable to store summary of the article

  • headlines = headlines + " " + title + " " + summary - add title and summary to the headlines variable

  • aiy.audio.say(headlines) - make Assistant say headlines

  • Create your trigger command

elif text == 'headlines':
        assistant.stop_conversation()
        headlines()

4.png

It checks if you said given phrase. If so, it will not send command to the Google Assistant API and will execute given function.

Radio player

Source: https://github.com/ktinkerer/aiyprojects-raspbian/blob/voicekit/src/radio_mod.py

  • Open new terminal window and install VLC player for Python
sudo pip install python-vlc
  • Open Assistant main file
sudo nano /home/pi/voice-recognizer-raspi/src/main.py
  • Add necessary library
import vlc

5.png

  • Define a function which will stop the radio
def radio_off():
        try:
                player.stop()
        except NameError as e:
                logging.info("Player isn't playing")

6.png

The function is trying to stop the player. In the case the radio is not playing, function will print an error message.

  • Define a function that will contain radio stations
def get_station(station_name):
        stations = {
                '1': 'http://a.files.bbci.co.uk/media/live/manifesto/audio/simulcast/hls/uk/sbr_high/ak/bbc_radio_one.m3u8',
                'one': 'http://a.files.bbci.co.uk/media/live/manifesto/audio/simulcast/hls/uk/sbr_high/ak/bbc_radio_one.m3u8',
                '2': 'http://a.files.bbci.co.uk/media/live/manifesto/audio/simulcast/hls/uk/sbr_high/ak/bbc_radio_two.m3u8',
                '3': 'http://a.files.bbci.co.uk/media/live/manifesto/audio/simulcast/hls/uk/sbr_high/ak/bbc_radio_three.m3u8',
                '4': 'http://a.files.bbci.co.uk/media/live/manifesto/audio/simulcast/hls/uk/sbr_high/ak/bbc_radio_fourfm.m3u8',
                '5': 'http://a.files.bbci.co.uk/media/live/manifesto/audio/simulcast/hls/uk/sbr_high/ak/bbc_radio_five_live.m3u8',
                '5 sports': 'http://a.files.bbci.co.uk/media/live/manifesto/audio/simulcast/hls/uk/sbr_high/ak/bbc_radio_five_live_sports_extra.m3u8',
                '6': 'http://a.files.bbci.co.uk/media/live/manifesto/audio/simulcast/hls/uk/sbr_high/ak/bbc_6music.m3u8',
                '1xtra': 'http://a.files.bbci.co.uk/media/live/manifesto/audio/simulcast/hls/uk/sbr_high/ak/bbc_radio_1xtra.m3u8',
                '4 extra': 'http://a.files.bbci.co.uk/media/live/manifesto/audio/simulcast/hls/uk/sbr_high/ak/bbc_radio_four_extra.m3u8',
                'nottingham': 'http://a.files.bbci.co.uk/media/live/manifesto/audio/simulcast/hls/uk/sbr_high/ak/bbc_radio_nottingham.m3u8',
                'planet rock': 'http://icy-e-bz-08-boh.sharp-stream.com/planetrock.mp3',
                        }
        return stations[station_name]

7.png

For example 'planet rock': is a station name and 'http://icy-e-bz-08-boh.sharp-stream.com/planetrock.mp3', is a direct link to the broadcast. When you say radio planet rock the program will search for station with that name and will play its broadcast. You can also add your stations with direct links to them.

  • Define a function that will be plaing a radio
def radio(text):
        logging.info("Radio command received: %s ", text)
        station_name = (text.replace('radio', '', 1)).strip()
        if station_name == "off":
                logging.info("Switching radio off")
                radio_off()
                return
        try:
                station = get_station(station_name)
        except KeyError as e:
                logging.error("Error finding station %s", station_name)
                # Set a default station here
                station = 'http://a.files.bbci.co.uk/media/live/manifesto/audio/simulcast/hls/uk/sbr_high/ak/bbc_6music.m3u8'
        logging.info("Playing radio: %s ", station)
        instance = vlc.Instance()
        global player
        player = instance.media_player_new()
        media = instance.media_new(station)
        player.set_media(media)
        player.play()

8.png

  • logging.info("Radio command received: %s ", text) - print a received radio command
  • station_name = (text.replace('radio', '', 1)).strip() - get the station name from the command
 if station_name == "off":
        logging.info("Switching radio off")
        radio_off()
        return

If the command is radio off program will print a message and will execute the function that turns off the player

try:
        station = get_station(station_name)

Try to find given station in get_station function

except KeyError as e:
                logging.error("Error finding station %s", station_name)
                # Set a default station here
                station = 'http://a.files.bbci.co.uk/media/live/manifesto/audio/simulcast/hls/uk/sbr_high/ak/bbc_6music.m3u8'

If the program has not found the station it will print an error message and will play a default one (you can set there any station you want)

  • logging.info("Playing radio: %s ", station) - print the station that will be played

  • instance = vlc.Instance() -create a new VLC instance

  • global player - create a global player variable

  • player = instance.media_player_new() - create a new MediaPlayer instance

  • media = instance.media_new(station) - create a new media instance with the station

  • player.play() - play a radio

  • Create your trigger command

elif 'radio' in text:
        assistant.stop_conversation()
        radio(text)

9.png

It checks if there is a word radio in the phrase. If so, it will not send command to the Google Assistant API and will start radio player.

Here is how it works:

Curriculum

Thank you for reading. Hope this tutorial will be helpful.

Google Assistant on Raspberry | Part 4: Modifications | Ecency