Linux Format

Pi Digital Assistant

Les Pounder shows us how physical computing, some Python libraries and an open data API can be used to build a Pi butler.

-

Les Pounder builds a digital assistant with some Python libraries and an open data API.

Personal assistants vary in their design and scope but what are the basics? Well, it should be easy to use and provide instant access to informatio­n. Fortunatel­y, creating one can be quite simple. We’re going to create a project where waving your hand in front of a sensor will have your homemade assistant inform you of the latest news and weather before you head out into the world.

We start by connecting our sensor, which only requires three connection­s to our Pi: 5V power, Ground and GPIO17. Even though the sensor uses 5V for power, the output signal from the sensor is only 3.3V which is the maximum safe voltage for the Raspberry Pi GPIO pins. To tweak the sensor’s sensitivit­y there’s a small potentiome­ter on the rear of the sensor. Turn this clockwise to increase the sensor’s range.

We used a standard 3.5mm speaker on our Pi, so we needed to ensure our audio was routed to the analog output, from the Pixel desktop right-click on the Speaker icon in the top right and select ‘Analog’. Your Pi will also need to be connected to the internet, in order to put this into a box for standalone use, use a Wi-Fi connection to minimise wires.

Software setup

Before we start writing any Python code we firstly need to install a few software packages. In the Terminal, we will install a simple MP3 player called mpg321 that will handle playback of our audio with $ sudo apt install mpg321 .

We now move on to installing the Python 3 specific libraries. Using pip3 we shall install feedparser to parse the raw RSS news feeds, gTTS, Google’s Text to Speech service and pyowm the Python Open Weather Map API. $ sudo pip3 install feedparser $ sudo pip3 install gTTS $ sudo pip3 pyowm

With the installati­on completed, we can now move on to coding the project. Open the Python 3 editor, found in the Programmin­g menu. The first screen to open is the Python shell, which we will not be using so click on File > New to create a new file. Once the new window opens immediatel­y save the file using File > Save and call it butler.py. Remember to save as you work through the code.

In the code, we start with importing the necessary libraries. We will import the MotionSens­or class from the GPIO Zero library – this is used for our sensor – followed by Feedparser to parse the RSS feeds; the time library to get the current time; gTTS for our text to speech service; os to work with the Linux operating system and pyowm to use the OpenWeathe­rMap service. from gpiozero import MotionSens­or import feedparser import time from gtts import gTTS import os import pyowm

To retrieve the RSS news feed, we need to use feedparser to parse the RSS into something that Python can work with: news = feedparser.parse("http://feeds.bbci.co.uk/news/rss. xml?edition=uk") pir = MotionSens­or(17)

We create an object called news that will store the feed from BBC News and later we’ll work with the feed. Next, we create an object called pir that will store the GPIO connection used for our infra red sensor, in this case GPIO17.

We now enter into a loop that will run as long as the Raspberry Pi is powered up:

while True: status = pir.wait_for_inactive()

First, the loop will store the status of our sensor inside of a variable called status . The value is either True or False. True when it detects an object, or False when nothing is detected. Our sensor works by sending a high signal to the GPIO pin when at rest, but when it detects an object it drops the signal to low, which triggers a change. We now need to wait for the trigger to be activated, storing True inside the variable. if status == True: current_time = time.ctime() str(current_time) If that is the case then we get the current time using the time.ctime() function, storing it in a variable called current_ time , next we ensure the data inside the variable is a string. We now move to our first look at gTTS, Google’s text to speech library: tts = gTTS(text=(current_time), lang='en-us') tts.save("time.mp3") os.system("mpg321 time.mp3")

We create an object called tts and in it we store the text that we wish to convert to speech, in this case the current_

time variable, we also can choose the speech language. We chose American English as it had the most natural tone. Google text to speech doesn’t perform live speech synthesis, rather it just converts the text to an audio file. We save the file with a relevant filename and call mpg321 from the terminal to playback the audio file.

To retrieve the top five news headlines from the BBC News RSS feed, we use a for loop that iterates five times and each time the value of i is increased by 1, starting at 0. for i in range(5):

Again, we’ll create a tts object for storing the text that we want to convert to speech. But in this case, we create a new MP3 every time the loop is run, and each time it will store a different news item. Earlier, we created an object called news that stored the RSS feed. We now extract that data using a dictionary (our news object, a list contained inside the dictionary called entries , another dictionary that stores the numerical value of the news headline, and finally the text for the headline is stored as a key called title . for i in range(5): tts = gTTS(text=(news["entries"][i]["title"]), lang="en-us") tts.save("news.mp3") os.system("mpg321 news.mp3")

In true TV news style, we now go to the weather report. Here we use the OpenWeathe­rMap API, called pyowm. We create an object called owm and store inside our API key necessary to access the service ( seeOpenWea­ther Map:howtogetyo­urownAPIke­y, above). We then create another object called observatio­n and in there we store the location of our weather report. Last, we create another object called w to get the weather for our location.

owm = pyowm.OWM("API KEY") observatio­n = owm.weather_at_ place("Blackpool,uk")

w = observatio­n.get_weather()

Our weather comes in two sections: the current status, cloudy, raining, sunny and the temperatur­e. Creating a variable called weather we get the status and store it in the variable. We then pass this to gTTS for conversion to speech, ensuring that the data stored in the weather variable is stored as a string, and then played back using mpg321.

weather = w.get_status() tts = gTTS(text=("The current weather is, "+str(weather)), lang="en-us") tts.save("weather.mp3") os.system("mpg321 weather.mp3")

Our temperatur­e report is handled in the same manner as the weather status, but to get the correct temperatur­e data stored in a variable called temperatur­e we need to pass an extra argument, in this case celsius and pass the key temp_

max so that the value stored for that key is returned and stored in the variable.

temperatur­e = w.get_temperatur­e("celsius")["temp_ max"]

tts = gTTS(text=("The current temperatur­e is, “+str(temperatur­e)+"celsius"), lang="en-us") tts.save("temperatur­e.mp3") os.system("mpg321 temperatur­e.mp3") Now that our code is complete, save your work and click on Run > Run Module to test that it’s working correctly.

 ??  ?? We had a spare speaker kit laying around, so we integrated it into the project instead of a big speaker. We bought the kit from Kitronik.
We had a spare speaker kit laying around, so we integrated it into the project instead of a big speaker. We bought the kit from Kitronik.
 ??  ??
 ??  ?? Our final project sees a Raspberry Pi 3 enclosed in a wooden box. The sensor pokes out of the box for easy access.
Our final project sees a Raspberry Pi 3 enclosed in a wooden box. The sensor pokes out of the box for easy access.

Newspapers in English

Newspapers from Australia