Skip to content

Instantly share code, notes, and snippets.

@brentajones
Last active February 4, 2021 12:24
Show Gist options
  • Save brentajones/54ad1c1aab6fbc1606b4d26617cd0cd4 to your computer and use it in GitHub Desktop.
Save brentajones/54ad1c1aab6fbc1606b4d26617cd0cd4 to your computer and use it in GitHub Desktop.
A weather status script for the Raspberry Pi and Blinkt module
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)
@raspberrycoulis
Copy link

raspberrycoulis commented Jun 13, 2018

@brentajones, where do you add the API key in all this? When I tried adding it on line 115, I get:

NameError: name 'myapikey' is not defined

(Obviously, I'm not going to post my API key there, but you get the point)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment