SenseHAT weather forecaster

Here’s a really simple scrolling weather forecast for the RaspberryPi SenseHAT.

It uses the BBC Weather RSS feed, so you would replace the postcode (‘bs1′) with your own to get your local forecast.

You’ll also need to install feedparser – this is very easy. Just type
sudo pip install feedparser
on the Raspberry Pi command line, and you should be good to go. Obviously, your Pi needs to be connected to the internet for this to work.

Here’s the simplest possible version of the Python code to display a 3 day forecast in rolling text:

import feedparser
from sense_hat import SenseHat

sense = SenseHat()

d = feedparser.parse('http://open.live.bbc.co.uk/weather/feeds/en/sw1/3dayforecast.rss')
entries = int(len(d['entries']))
for x in range(entries):
    sense.show_message(d['entries'][x]['title'])

This reads the RSS feed’s XML into an array called ‘d’. It then counts how many forecasts there are – I guess there will always be 3, but seeing as it’s easy to count them, let’s count them anyway. It then loops 3 times showing the forecast for that day, which is stored in the ‘title’ block of the XML.

A few possible improvements spring to mind.

It’s quite a long scrolling message, so let’s add different colours for each day of the week, so when you glance at it you know if it’s today’s weather (white text), tomorrow’s (green) or the day after tomorrow (blue):

import feedparser
from sense_hat import SenseHat

sense = SenseHat()
colours = [[255,255,255],[0,255,0],[0,0,255]]

print colours

d = feedparser.parse('http://open.live.bbc.co.uk/weather/feeds/en/bs1/3dayforecast.rss')
entries = int(len(d['entries']))
for x in range(entries):
    sense.show_message(d['entries'][x]['title'], text_colour=colours[x])

(If you did have more than 3 entries in a 3-day forecast, the code would break – you’d need to add more colours).

Now I see the SenseHAT can’t display degree signs, it shows a question mark instead, so let’s strip them out:

import feedparser
from sense_hat import SenseHat

sense = SenseHat()
colours = [[255,255,255],[0,255,0],[0,0,255]]

d = feedparser.parse('http://open.live.bbc.co.uk/weather/feeds/en/bs1/3dayforecast.rss')
entries = int(len(d['entries']))
for x in range(entries):
    wx = (d['entries'][x]['title'])
    disp_wx = wx.replace(u"\u00B0",'')
    sense.show_message(disp_wx, text_colour=colours[x])

I’ve used \u00B0 as this is the escape sequence for a degree symbol, and I replace this with nothing, though you could replace it with the word ‘degrees’.

So far, this only runs once, so let’s get the program to keep running. Because we are responsible digital citizens, we won’t keep requesting the weather forecast every few seconds, we’ll ask for it once when we start the program running, and then keep the text scrolling by adding a while True statement:

import feedparser
from sense_hat import SenseHat

sense = SenseHat()
colours = [[255,255,255],[0,255,0],[0,0,255]]

d = feedparser.parse('http://open.live.bbc.co.uk/weather/feeds/en/bs1/3dayforecast.rss')
entries = int(len(d['entries']))

while True:
    for x in range(entries):
        wx = (d['entries'][x]['title'])
        disp_wx = wx.replace(u"\u00B0",'')
        sense.show_message(disp_wx, text_colour=colours[x])

Next I notice that ‘maximum/minimum temperature’ is a bit of a screenful, so let’s change that to ‘high’ or ‘low’:

import feedparser
from sense_hat import SenseHat

sense = SenseHat()
colours = [[255,255,255],[0,255,0],[0,0,255]]

d = feedparser.parse('http://open.live.bbc.co.uk/weather/feeds/en/bs1/3dayforecast.rss')
entries = int(len(d['entries']))

while True:
    for x in range(entries):
        wx = (d['entries'][x]['title'])
        disp_wx = wx.replace(u"\u00B0",'')
        disp_wx = disp_wx.replace('Maximum Temperature','High')
        disp_wx = disp_wx.replace('Minimum Temperature','Low')
        sense.show_message(disp_wx, text_colour=colours[x])

Finally, this version strips out the Fahrenheit and gets a fresh forecast after displaying the 3 day forecast 10 times:

import feedparser
from sense_hat import SenseHat
import re

sense = SenseHat()
colours = [[255,255,255],[0,255,0],[0,0,255]]

def fetch_weather():
    w = feedparser.parse('http://open.live.bbc.co.uk/weather/feeds/en/bs1/3dayforecast.rss')
    return(w)

d = fetch_weather()
entries = int(len(d['entries']))

counter = 0

while True:
    counter = counter + 1
    for x in range(entries):
        wx = (d['entries'][x]['title'])
        disp_wx = wx.replace(u"\u00B0",'')
        disp_wx = disp_wx.replace('Maximum Temperature','High')
        disp_wx = disp_wx.replace('Minimum Temperature','Low')
        disp_wx = re.sub(r'\([^)]*\)', '', disp_wx)
        sense.show_message(disp_wx, text_colour=colours[x])
    if counter == 10:
        d = fetch_weather()
        entries = int(len(d['entries']))
        sense.show_message('UPDATING', text_colour=[255,0,0])
        counter = 0

A nice extension would be to display symbols based on the weather, or perhaps change text colour depending on the temperature – red if it’s warm, blue if it’s cold? The sky (ahem) is the limit…

This entry was posted in Raspberry Pi and tagged , , . Bookmark the permalink.

15 Responses to SenseHAT weather forecaster

  1. Bonzadog says:

    An excellent Project, finally something useful for the SenseHat. I will certainly
    make this project.
    Well done!
    BD

  2. Bonzadog says:

    As I wrote previoulsy….I am going to try this out but for a for a German weather rss
    where I currently live. So I’ll need to parse appropriately.

    But I also want the forecast for Southampton, where my parents live , what code would I use? I will use that a reference point.

    Where does SW1 “point” to ?
    If I cut and paste http://open.live.bbc.co.uk/weather/feeds/en/sw1/3dayforecast.rss
    to a Browser

    I get :
    HTTP Status 404 -

    type Status report

    message

    description The requested resource () is not available.
    Apache Tomcat

    Any advice for me?

    BD

  3. Flanux says:

    Great project !
    Indeed something useful for my SenseHat.
    How can i easily change from fahrenheit to celsius ?

    Thanks for your project…

  4. okahiro says:

    Thank you for the great job!
    I made my Sense Hat a smart weather forecaster thanks to your job.
    I had easily found ID of Yokohama from the following BBC website.
    http://www.bbc.com/weather/
    Just type the name of your town in the field “Find a Forecast” and push enter key.
    Then, you will automatically jump to the page having URL “http://www.bbc.com/weather/???????”.
    In case of Yokohama, ??????? is 1848354.

    • okahiro says:

      I inserted the following line just below the “counter = *” line to show title, including the data source (BBC) and city name faster. :-)

      sense.show_message(d.feed.title, scroll_speed=0.05)

  5. Thanael says:

    Hey there :) This is exactly what I want to do with my sensehat – almost! Thank you for putting together this guide. My eyes cross when I try to figure out code though. Have you since created a version of the python script which displays today’s weather in picture form, along with the min/max temperature?

    • blogmywiki says:

      Yes! I am actually hoping to do this in the next few weeks :)

      • Thanael says:

        Amazing! I’ll keep an eye on your blog then. Thank you!

      • Thanael says:

        I asked a friend to help me write some code to display images based on the weather. We came up with a decent proof of concept today. All it currently does it parse the feed for phrases BBC weather likes to use and outputs a different coloured dot for each phrase. It also adds a dot for today/tomorrow/next day. From there, I will edit in proper images and add a temperature readout of some sort.

        It’s very much proof of concept at the minute, but if anyone wants to use it as a base it’s at pastebin. Search the following, or append to the pastebin url:
        A0gYmG16

  6. John Gleeson says:

    Hi just wondering how ye are getting this to run?
    I’ve tried running it as a python file but it gives multiple errors such as Feedparser no module found when it is installed so thought I might be doing something wrong?
    Any help would be appreciated.

    • blogmywiki says:

      Hi John – this is quite an old project and I’ve not looked at it in several years so I’m really out of touch with it – and the Raspberry Pi – I’m afraid. Is it possible feedparser is not installed for the same version of Python that you’re running the Python script in? Also that BBC weather RSS feed doesn’t seem to exist any more!
      cheers
      Giles

Leave a Reply