Speaking weather station

Posted by mantonel on October 3, 2016. Last update on October 16, 2016.
Python Raspberry Pi


We are going to use 3 tools to create this nice weather station :

Get data

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:

sudo raspi-config

Choose "9 Advanced Options" then "A8 Audio" and select the option you want.

Full example

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):

#!/usr/bin/env python3

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>'

def getTime(date_p):
    """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:
        return "midnight"
    elif hour > 0 and hour < 12:
        return str(hour) + " am"
    elif hour == 12:
        return "midday"
    elif hour > 12 and hour < 24:
        return str(hour) + " pm"
        # Should not happen
        return ""

# 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"][0]["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:

sudo raspi-config

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:

crontab -e

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

If you want more information, take a look at the man page or at the ubuntu documentation.

1 Comment

Alex October 17, 2016
Nice tutorial!