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
- 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)
url = "http://feeds.bbci.co.uk/news/rss.xml"
- variable to store url to the headlinesnumber_of_headlines = 3
- number of headlines that will be readedfeed = feedparser.parse(url)
- variable to store parsed feed from the urlheadlines = ""
- empty variable to store headlinesfor i in range(0, number_of_headlines):
- loop that repeats as many times as you set innumber_of_headlines
title = feed.entries[i].title
- variable to store title of the articlesummary = (feed.entries[i].summary)
- variable to store summary of the articleheadlines = headlines + " " + title + " " + summary
- addtitle
andsummary
to theheadlines
variableaiy.audio.say(headlines)
- make Assistant say headlinesCreate your trigger command
elif text == 'headlines':
assistant.stop_conversation()
headlines()
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
- Define a function which will stop the radio
def radio_off():
try:
player.stop()
except NameError as e:
logging.info("Player isn't playing")
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]
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()
logging.info("Radio command received: %s ", text)
- print a received radio commandstation_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 playedinstance = vlc.Instance()
-create a new VLC instanceglobal player
- create a global player variableplayer = instance.media_player_new()
- create a new MediaPlayer instancemedia = instance.media_new(station)
- create a new media instance with the stationplayer.play()
- play a radioCreate your trigger command
elif 'radio' in text:
assistant.stop_conversation()
radio(text)
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
- Google Assistant on Raspberry | Part 3: Custom wake word
- Google Assistant on Raspberry | Part 2: Custom actions
- Google Assistant on Raspberry | Part 1: Installation process
Thank you for your contribution, and for including references this time :)
Few suggestions for future work:
Looking forward to your upcoming tutorials.
Chat with us on Discord.
[utopian-moderator]Need help? Write a ticket on https://support.utopian.io/.
Hey @mcfarhat Here's a tip for your valuable feedback! @Utopian-io loves and incentivises informative comments.
Contributing on Utopian
Learn how to contribute on our website.
Want to chat? Join us on Discord https://discord.gg/h52nFrV.
Vote for Utopian Witness!
Hi buddy
I've been away for past 3 weeks (short holiday) and finally im back :) Loads of catching up is awaiting me now.
I checked your profile and im glad to see that you're still very active on steemit.
How have you been doing?
Obviously upvoted.
Cheers buddy,
Piotr
hi @neavvy
Did you give up on steemit? I just checked your profile and it seem to be completly dead :(
hope you're okey out there.
Yours,
Piotr