Skip to content

Instantly share code, notes, and snippets.

@pbojinov
Forked from brentajones/blinkt-weather.py
Last active June 11, 2018 14:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pbojinov/0b39906a5e3f3c158b8b15a92e011db8 to your computer and use it in GitHub Desktop.
Save pbojinov/0b39906a5e3f3c158b8b15a92e011db8 to your computer and use it in GitHub Desktop.
A weather status script for the Raspberry Pi and Blinkt module - https://blog.bjones.net/2018/02/04/pi-blinkt-weather-status/
import json, time, os.path
import blinkt, requests
status_colors = {'current':[1,1,1],'high':[1,0,0],'low':[0,0,1],'precip':[0,1,0]}
blinkt.set_clear_on_exit()
blinkt.set_brightness(.1)
def getForecast(key,lat,lng):
url = 'https://api.darksky.net/forecast/' + key + '/' + str(lat) + ',' + str(lng)
stl = requests.get(url)
return stl.json()
def getTempColor(temp):
if temp <= 0:
color = [10,0,30]
elif temp <= 32:
color = [0,0,30]
elif temp <= 50:
color = [0,15,30]
elif temp <= 65:
color = [0,30,0]
elif temp <= 80:
color = [60,20,0]
elif temp <= 95:
color = [60,10,0]
else:
color = [30,0,0]
return color
def getStoplightColor(prob,low,high):
if prob < low:
color = [0,30,0]
elif prob < high:
color = [60,20,0]
else:
color = [30,0,0]
return color
def showSevenDayTemps(temps, which):
blinkt.clear()
# set status light
color = status_colors[which]
blinkt.set_pixel(7,color[0],color[1],color[2])
for i in range(7):
color = getTempColor(temps[i])
blinkt.set_pixel(i,color[0],color[1],color[2])
blinkt.show()
def showSevenDayPrecip(precip, which):
blinkt.clear()
# set status light
color = status_colors[which]
blinkt.set_pixel(7,color[0],color[1],color[2])
for i in range(7):
color = getStoplightColor(precip[i],.25,.75)
blinkt.set_pixel(i,color[0],color[1],color[2])
blinkt.show()
def showCurrent(data):
blinkt.clear()
# set status light
color = status_colors['current']
blinkt.set_pixel(7,color[0],color[1],color[2])
# get current temp (use color scale)
color = getTempColor(data['temp'])
blinkt.set_pixel(0,color[0],color[1],color[2])
# daily high and low (use color scale)
color = getTempColor(data['low'])
blinkt.set_pixel(1,color[0],color[1],color[2])
color = getTempColor(data['high'])
blinkt.set_pixel(2,color[0],color[1],color[2])
# precip (red, yellow, green)
color = getStoplightColor(data['precip'],.25,.75)
blinkt.set_pixel(3,color[0],color[1],color[2])
blinkt.show()
def prepData(forecast):
data = {'lows': [], 'highs': [], 'precip': [], 'current': {}}
for day in forecast['daily']['data']:
data['lows'].append(day['apparentTemperatureLow'])
data['highs'].append(day['apparentTemperatureHigh'])
data['precip'].append(day['precipProbability'])
curr_temp = forecast['currently']['apparentTemperature']
data['current']['temp'] = curr_temp
data['current']['low'] = curr_temp
data['current']['high'] = curr_temp
for hour in forecast['hourly']['data'][:24]:
if hour['apparentTemperature'] < data['current']['low']:
data['current']['low'] = hour['apparentTemperature']
if hour['apparentTemperature'] > data['current']['high']:
data['current']['high'] = hour['apparentTemperature']
data['current']['precip'] = forecast['daily']['data'][0]['precipProbability']
return data
forecast = None
cur_time = time.time()
interval = 5*60
delay = 2
while True:
if forecast is not None and (cur_time + interval) > time.time():
pass
else:
forecast = getForecast( [api key] ,38.6270,-90.1994)
data = prepData(forecast)
cur_time = time.time()
showCurrent(data['current'])
time.sleep(delay)
showSevenDayTemps(data['lows'], 'low')
time.sleep(delay)
showSevenDayTemps(data['highs'], 'high')
time.sleep(delay)
showSevenDayPrecip(data['precip'], 'precip')
time.sleep(delay)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment