Temperature and wind information for your location on a PiFace display

A really simple script, hopefully self explanatory, to get your current location and display some weather data on the PiFace. I’ve got this running as a cronjob every hour.

NOTE: requires that you get yourself an API key from the met office from here; http://www.metoffice.gov.uk/datapoint

NOTE: this script doesn’t handle exceptions, it just ignores them. 


# Reads data from the met office for our location and displays the temperature and wind-speed on the PiFace


import pifacecad

import metoffer

import urllib

import xml.etree.ElementTree as ET



# get our IP address

url = ‘http://checkip.dyndns.org’

response = urllib.urlopen(url)

htmlData = response.read()

startTag = ‘Address: ‘

ipaddress = htmlData[htmlData.find(startTag)+len(startTag):htmlData.find(‘</body’)]

print ‘External IP address is: ‘ + ipaddress


# get our latitude and longtitude (based on IP address)

url = ‘http://freegeoip.net/xml/’+ipaddress

response = urllib.urlopen(url)

tree = ET.fromstring(response.read())

latitude = tree.find(‘Latitude’).text

longtitude = tree.find(‘Longitude’).text


print ‘Geolocation: ‘ + latitude + ‘:’ + longtitude


# initialize PiFace and met office API

pfcad = pifacecad.PiFaceCAD()

met_api_key = ‘YOUR-API-KEY-HERE’

met = metoffer.MetOffer(met_api_key)


# get local weather

weather_observations = met.nearest_loc_obs(float(latitude),float(longtitude))


# Info on metoffer data: https://pypi.python.org/pypi/MetOffer/

weather_report = metoffer.parse_val(weather_observations)

# last item is the latest observation data

weather_data = weather_report.data[len(weather_report.data)-1]


# Update PiFace display

#info on PiFace http://piface.github.io/pifacecad/example.html



wind_info = weather_data[‘Wind Speed’]

wind_direction = weather_data[‘Wind Direction’]

pfcad.lcd.write(str(wind_info[0])+’ ‘+wind_info[1]+ ‘ from ‘+wind_direction[0])



print ‘Something went wrong…’ # ignoring exceptions 


One thought on “Temperature and wind information for your location on a PiFace display

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s