Speaking weather station
We are going to use 3 tools to create this nice weather station :
- Openweathermap.org to get the weather;
- Ivona.com to make the Raspberry Pi speak;
- Pyvona to easily communicate with Ivona's servers.
First you need to create a free account on Openweathermap.org. It's for limited use but it's more than enough for what we need.
You need to build your URL to get the data you want. I used the 5 day / 3 hour forecast API.
The website gives the weather by steps of 3 hours : 0/3/6/9/12/15/18/21. In this example I'm going to request the weather of Paris for 6 steps in english. The units used are metrics and the data is sent as JSON.
Here is my URL (replace <YOUR_APPID> by your APPID once you have subscribed):
Use Ivona's voice
As previously you need to create an account on Ivona.com. It's for limited use too but you won't need more for the speaking weather station.
Once registered, get your credentials (there is a secret key and an access key) and install Pyvona:
python3 -m pip install pyvona
Before doing anything make sure you have a speaker plugged.
Now we have everything we need, Pyvona does all the dirty work to help us communicate with Ivona's servers. Obviously you can also do it by yourself with POST or GET requests (as described on their website).
voice = pyvona.create_voice("YOUR_ACCESS_KEY", "YOUR_SECRET_KEY")
If the previous code seems to work but does not produce any sound, check if the audio output is set through HDMI or 3.5mm jack (depends on how your speaker is plugged). To check this enter:
Choose "9 Advanced Options" then "A8 Audio" and select the option you want.
Here we go, let's try to put all this together.
In this example I get only the temperature and the weather for the next 15 hours (by steps of 3 hours):
import json, requests, datetime, pyvona
# Ivona keys
WEATHER_URL = 'http://api.openweathermap.org/data/2.5/forecast/city?id=6455259&mode=json&lang=en&units=metric&cnt=6&APPID=<YOUR_APPID>'
ACCESS_KEY = 'YOUR_ACCESS_KEY'
SECRET_KEY = 'YOUR_SECRET_KEY'
"""Return a string representing the time of the day from the given date."""
hour = datetime.datetime.strptime(date_p, "%Y-%m-%d %H:%M:%S").hour
if hour == 0 or hour == 24:
elif hour > 0 and hour < 12:
return str(hour) + " am"
elif hour == 12:
elif hour > 12 and hour < 24:
return str(hour) + " pm"
# Should not happen
# Request and load JSON containing weather data from http://api.openweathermap.org
response = requests.get(WEATHER_URL)
data = json.loads(response.text)
# Build the string
speech = "Hello sir. "
for i in range(0, len(data["list"])):
# Date format : 2016-09-29 12:00:00
speech += "At " + getTime(data["list"][i]["dt_txt"]) + " it's " + str(round(data["list"][i]["main"]["temp"])) + " degrees and " + str(data["list"][i]["weather"]["main"]) + ". "
speech += "Have a good day sir."
voice = pyvona.create_voice(ACCESS_KEY, SECRET_KEY)
Schedule script execution
First you need to check if the timezone is properly configured:
Compare the output of the command with your time, if it does not match:
Choose "4 Internationalisation Options" then "I2 Change Timezone" and select the geographic area and the city you live in.
We are going to use cron to schedule the execution of the Python script:
This command should open your crontab file with a text editor (the first time it should ask you which text editor you want to use). In this example I'm going to schedule the execution of the script "/home/pi/tutorial_speaking_weather_station/speaking_weather_station.py" every day of the week (from monday to friday) at 8:00 am:
0 8 * * 1-5 /home/pi/tutorial_speaking_weather_station/speaking_weather_station.py
If you want to log the output of the script so that you know when something goes wrong (don't forget to create the file "log.txt" and give it permissions):
0 8 * * 1-5 /home/pi/tutorial_speaking_weather_station/speaking_weather_station.py > /home/pi/tutorial_speaking_weather_station/log.txt