Using Python to read weather forecast data

When the Raspberry Pi Zero came out last year, my love of small computers meant that I quickly purchased one along with a Scroll pHAT despite not having a project in mind. Recently, I realised that the small size and low power requirements meant that it'd be great for displaying the day's weather forecast.

The first task was getting the forecast data. For most countries the most accurate information is likely to come from government-funded weather services, which meant that my choice was the Bureau of Meteorology. Their Weather Data Services page shows the huge amount of data they have available, but since my display was a 5 x 11 LED matrix and I'm in a capital city, the 'précis' forecasts seemed like a good fit. If you're elsewhere in Australia, the BoM has 7-day forecasts available in XML format. Since the data I'd picked was in plain text format, it was pretty easy to write a script to process it:

#! /usr/bin/env python

from ftplib import FTP
from datetime import datetime

# Create a list for the forecast, and a simple function that appends lines to that list.

forecast = []
def add_lines(next_line):
    forecast.append(next_line)

ftp = FTP('ftp2.bom.gov.au')    # Create FTP object
ftp.login() # Log in (anonymously) to the FTP server
line = ftp.retrlines("RETR anon/gen/fwo/IDA00100.dat", callback = add_lines) # Read each line of the file using the add_lines function
ftp.quit()  # Quit the server

# Change the number in the square brackets below to choose the capital city:
# 1 = Sydney    2 = Melbourne   3 = Brisbane    4 = Perth
# 5 = Hobart    6 = Canberra    7 = Darwin

forecast = forecast[4].split('#')   # Isolate the line containing the city, and split it into fields.
forecastdate = datetime.strptime(forecast[5], '%Y%m%d%H%M%S').strftime('%d %b') # Convert 14-digit date/time to date/month string.

To display the forecast on the pHAT, I used code from one of the examples in the GitHub library, and added it to the end of the script:

# Additional libraries needed for the pHAT
import sys
import scrollphat
import time

scrollphat.set_brightness(5)    # Chosen by die roll
scrollphat.set_rotate(True) # My Pi Zero sits upside down (ports facing up)
scrollphat.write_string(forecastdate + ": " + forecast[6] + ". " + forecast[7] + "    ")    # Write forecast string to the pHAT

while True:
    try:
        scrollphat.scroll() # Scroll 1 pixel every 0.1 s
        time.sleep(0.1)
    except KeyboardInterrupt:   # Clear the screen if CTRL+C is pressed
        scrollphat.clear()
        sys.exit(-1)

I think a diffuser in front of the LEDs would aid readability, but currently it looks like this when in action (you can click on the image for an animated GIF of today's forecast):

Scroll pHAT displaying weather forecast

Although the script works, having to login to the Pi to run and quit the script means it's not likely to get used much as it's probably easier just to go to the BoM website. This should be fairly easy to alter as the pHAT communicates with the Pi over I2C, meaning there are plenty of GPIO pins left to add a button. This would make it much more practical as you could push the button and have the script start, scroll the forecast a few times, and then turn the LEDs off. It seems like a simple improvement, though the main obstacle is likely to be accessing the spare GPIO pins on the Pi, as the female header on the Scroll pHAT occupies all of them. I think this addition will be a desoldering/rewiring task for some future free time.