Precipitation

Using the Raspberry Pi with Sense HAT as a precipitation monitor for the family. Each vertical row represents an hour which gives a clear overview of the next 8 hours forecast. Max values are green, rain is blue and snow is white. Max 8mm per row that are capped for higher hourly forecast values.

#PRECIPITATION FOR SENSEHAT

import urllib2
from decimal import *
from xml.etree import ElementTree as ET
from sense_hat import SenseHat
from time import sleep

sense = SenseHat()

prec=None
precmax=None
temp=None

#Rotation
rot=180
sense.set_rotation(rot) #When power cable is upwards

#Color definitions
R = (55, 0, 0)
G = (0, 255, 0)
B = (0, 0, 255) #Blue
C = (0, 55, 55)
M = (255, 255, 0)
Y = (255, 0, 255)
W = (255, 255, 255) #White
O = (0, 0, 0)

#background behind the readings
precip_back = [
O, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
O, O, O, O, O, O, O, O,
]

sense.show_message("Precipitation")

while True:

    #Preparing 3 empty arrays - one for each forecast. Keeping separate arrays for clarity
    yrtemp =[] #holds the temperatures for each hour
    yrprec =[] #for precipitation
    yrprecmax =[] #for max precipitation if present

    sense.set_pixels(precip_back) #Start each reading by erasing the display

    #Read online precipitation data by fetching the relevant XML at yr.no. The following URL is for Vanlose, Denmark
    html_content = urllib2.urlopen('https://www.yr.no/sted/Danmark/Sj%C3%A6lland/Vanl%C3%B8se/varsel_time_for_time.xml').read()
    root = ET.fromstring(html_content)

#    Next two lines are for testing on an offline file if there is no precipitation. You also need to outcomment the two lines above  
#    html_content = ET.ElementTree(file='/home/pi/Desktop/varsel_time_for_time.xml')
#    root = html_content.getroot()

#    lines = html_content.split("\n") #This is for testing if the XML has content. Default turned off
#    print("Number of lines is {}".format(len(lines)))

    for temperature in root.iter('temperature'):
        temp=float(temperature.get('value'))
        yrtemp.append(temp)
    
    for precipitation in root.iter('precipitation'):
        prec=precipitation.get('value')
        prec=Decimal(prec).quantize(Decimal('1.'), rounding=ROUND_UP) #Round up to nearest number.
        yrprec.append(prec)

        precmax=precipitation.get('maxvalue')
        if precmax==None:
           precmax=0
        precmax=Decimal(precmax).quantize(Decimal('1.'), rounding=ROUND_UP)
        yrprecmax.append(precmax)

    for x in range(8): #We only need the first 8 readings for the SenseHAT 8X8 matrix

        #Read data from position x in each array
        prec=yrprec[x]
        precmax=yrprecmax[x]
        temp=yrtemp[x]
#        print prec, precmax, temp

        if prec > 8:
            prec=8 #Capping the mm readings so it fits to SenseHAT display (max 8)
        if precmax > 8:
           precmax=8
        if x<8: #Build the display with reading from arrays
            for y in range (8):
                if (precmax-1)>=y:
                    sense.set_pixel(x, (7-y), C) #First show the max precipitation layer
            for y in range (8):
                if (prec-1)>=y:
                    if temp<0:
                        sense.set_pixel(x, (7-y), W) #Add the precipitation on top. White if snow is expected
                    if temp >=0:
                        sense.set_pixel(x, (7-y), B) #Add the precipitation on top. Blue if rain is expected.

    sleep(600) #Wait 10 mins before updating the XML
Precipitation