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.

[pastacode lang=”python” manual=”%23PRECIPITATION%20FOR%20SENSEHAT%0A%0Aimport%20urllib2%0Afrom%20decimal%20import%20*%0Afrom%20xml.etree%20import%20ElementTree%20as%20ET%0Afrom%20sense_hat%20import%20SenseHat%0Afrom%20time%20import%20sleep%0A%0Asense%20%3D%20SenseHat()%0A%0Aprec%3DNone%0Aprecmax%3DNone%0Atemp%3DNone%0A%0A%23Rotation%0Arot%3D180%0Asense.set_rotation(rot)%20%23When%20power%20cable%20is%20upwards%0A%0A%23Color%20definitions%0AR%20%3D%20(55%2C%200%2C%200)%0AG%20%3D%20(0%2C%20255%2C%200)%0AB%20%3D%20(0%2C%200%2C%20255)%20%23Blue%0AC%20%3D%20(0%2C%2055%2C%2055)%0AM%20%3D%20(255%2C%20255%2C%200)%0AY%20%3D%20(255%2C%200%2C%20255)%0AW%20%3D%20(255%2C%20255%2C%20255)%20%23White%0AO%20%3D%20(0%2C%200%2C%200)%0A%0A%23background%20behind%20the%20readings%0Aprecip_back%20%3D%20%5B%0AO%2C%20O%2C%20O%2C%20O%2C%20O%2C%20O%2C%20O%2C%20O%2C%0AO%2C%20O%2C%20O%2C%20O%2C%20O%2C%20O%2C%20O%2C%20O%2C%0AO%2C%20O%2C%20O%2C%20O%2C%20O%2C%20O%2C%20O%2C%20O%2C%0AO%2C%20O%2C%20O%2C%20O%2C%20O%2C%20O%2C%20O%2C%20O%2C%0AO%2C%20O%2C%20O%2C%20O%2C%20O%2C%20O%2C%20O%2C%20O%2C%0AO%2C%20O%2C%20O%2C%20O%2C%20O%2C%20O%2C%20O%2C%20O%2C%0AO%2C%20O%2C%20O%2C%20O%2C%20O%2C%20O%2C%20O%2C%20O%2C%0AO%2C%20O%2C%20O%2C%20O%2C%20O%2C%20O%2C%20O%2C%20O%2C%0A%5D%0A%0Asense.show_message(%22Precipitation%22)%0A%0Awhile%20True%3A%0A%0A%20%20%20%20%23Preparing%203%20empty%20arrays%20-%20one%20for%20each%20forecast.%20Keeping%20separate%20arrays%20for%20clarity%0A%20%20%20%20yrtemp%20%3D%5B%5D%20%23holds%20the%20temperatures%20for%20each%20hour%0A%20%20%20%20yrprec%20%3D%5B%5D%20%23for%20precipitation%0A%20%20%20%20yrprecmax%20%3D%5B%5D%20%23for%20max%20precipitation%20if%20present%0A%0A%20%20%20%20sense.set_pixels(precip_back)%20%23Start%20each%20reading%20by%20erasing%20the%20display%0A%0A%20%20%20%20%23Read%20online%20precipitation%20data%20by%20fetching%20the%20relevant%20XML%20at%20yr.no.%20The%20following%20URL%20is%20for%20Vanlose%2C%20Denmark%0A%20%20%20%20html_content%20%3D%20urllib2.urlopen(‘https%3A%2F%2Fwww.yr.no%2Fsted%2FDanmark%2FSj%25C3%25A6lland%2FVanl%25C3%25B8se%2Fvarsel_time_for_time.xml’).read()%0A%20%20%20%20root%20%3D%20ET.fromstring(html_content)%0A%0A%23%20%20%20%20Next%20two%20lines%20are%20for%20testing%20on%20an%20offline%20file%20if%20there%20is%20no%20precipitation.%20You%20also%20need%20to%20outcomment%20the%20two%20lines%20above%20%20%0A%23%20%20%20%20html_content%20%3D%20ET.ElementTree(file%3D’%2Fhome%2Fpi%2FDesktop%2Fvarsel_time_for_time.xml’)%0A%23%20%20%20%20root%20%3D%20html_content.getroot()%0A%0A%23%20%20%20%20lines%20%3D%20html_content.split(%22%5Cn%22)%20%23This%20is%20for%20testing%20if%20the%20XML%20has%20content.%20Default%20turned%20off%0A%23%20%20%20%20print(%22Number%20of%20lines%20is%20%7B%7D%22.format(len(lines)))%0A%0A%20%20%20%20for%20temperature%20in%20root.iter(‘temperature’)%3A%0A%20%20%20%20%20%20%20%20temp%3Dfloat(temperature.get(‘value’))%0A%20%20%20%20%20%20%20%20yrtemp.append(temp)%0A%20%20%20%20%0A%20%20%20%20for%20precipitation%20in%20root.iter(‘precipitation’)%3A%0A%20%20%20%20%20%20%20%20prec%3Dprecipitation.get(‘value’)%0A%20%20%20%20%20%20%20%20prec%3DDecimal(prec).quantize(Decimal(‘1.’)%2C%20rounding%3DROUND_UP)%20%23Round%20up%20to%20nearest%20number.%0A%20%20%20%20%20%20%20%20yrprec.append(prec)%0A%0A%20%20%20%20%20%20%20%20precmax%3Dprecipitation.get(‘maxvalue’)%0A%20%20%20%20%20%20%20%20if%20precmax%3D%3DNone%3A%0A%20%20%20%20%20%20%20%20%20%20%20precmax%3D0%0A%20%20%20%20%20%20%20%20precmax%3DDecimal(precmax).quantize(Decimal(‘1.’)%2C%20rounding%3DROUND_UP)%0A%20%20%20%20%20%20%20%20yrprecmax.append(precmax)%0A%0A%20%20%20%20for%20x%20in%20range(8)%3A%20%23We%20only%20need%20the%20first%208%20readings%20for%20the%20SenseHAT%208X8%20matrix%0A%0A%20%20%20%20%20%20%20%20%23Read%20data%20from%20position%20x%20in%20each%20array%0A%20%20%20%20%20%20%20%20prec%3Dyrprec%5Bx%5D%0A%20%20%20%20%20%20%20%20precmax%3Dyrprecmax%5Bx%5D%0A%20%20%20%20%20%20%20%20temp%3Dyrtemp%5Bx%5D%0A%23%20%20%20%20%20%20%20%20print%20prec%2C%20precmax%2C%20temp%0A%0A%20%20%20%20%20%20%20%20if%20prec%20%3E%208%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20prec%3D8%20%23Capping%20the%20mm%20readings%20so%20it%20fits%20to%20SenseHAT%20display%20(max%208)%0A%20%20%20%20%20%20%20%20if%20precmax%20%3E%208%3A%0A%20%20%20%20%20%20%20%20%20%20%20precmax%3D8%0A%20%20%20%20%20%20%20%20if%20x%3C8%3A%20%23Build%20the%20display%20with%20reading%20from%20arrays%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20y%20in%20range%20(8)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(precmax-1)%3E%3Dy%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20sense.set_pixel(x%2C%20(7-y)%2C%20C)%20%23First%20show%20the%20max%20precipitation%20layer%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20y%20in%20range%20(8)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(prec-1)%3E%3Dy%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20temp%3C0%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20sense.set_pixel(x%2C%20(7-y)%2C%20W)%20%23Add%20the%20precipitation%20on%20top.%20White%20if%20snow%20is%20expected%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20temp%20%3E%3D0%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20sense.set_pixel(x%2C%20(7-y)%2C%20B)%20%23Add%20the%20precipitation%20on%20top.%20Blue%20if%20rain%20is%20expected.%0A%0A%20%20%20%20sleep(600)%20%23Wait%2010%20mins%20before%20updating%20the%20XML%0A” message=”Precipitation” highlight=”” provider=”manual”/]