-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather.py
76 lines (63 loc) · 1.95 KB
/
weather.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import requests
import json
import time
from BlinkyTape import BlinkyTape
bb = BlinkyTape('/dev/ttyACM0') #least on Mac OS X, this is the port to use!
RGB_OFFSET = 25
def getTemperature():
try:
r = requests.get("http://datapoint.metoffice.gov.uk/public/data/val/wxobs/all/json/3544?res=hourly&key=35d53d57-9aa0-45de-9772-0871f0ceffda")
data = json.loads(r.text)
print data
periods = data['SiteRep']['DV']['Location']['Period']
latestPeriod = periods[len(periods) - 1]
lastSampleDate = latestPeriod['value']
reports = latestPeriod['Rep']
lastSampleHour = len(reports) - 1
currentTemperature = reports[lastSampleHour]['T']
print "%s %s:00 UTC %s C" % (lastSampleDate, lastSampleHour, currentTemperature)
return int(float(currentTemperature))
except:
print "Unexpected error:", sys.exc_info()[0]
def increasingColourOffset(offset):
return offset * RGB_OFFSET
def decreasingColourOffset(offset):
return 255 - increasingColourOffset(offset)
#20 10 0 10 20 30 40
def convertTemperatureToRgb(temperature):
if (temperature <= -20):
return [0,0,255]
elif (temperature <= -10):
# -20 < t <= -5
g = increasingColourOffset(temperature + 20)
return [0,g,255]
elif (temperature <= 0):
# -5 < t <= 10
b = decreasingColourOffset(temperature + 10)
return [0,255,b]
elif (temperature <= 10):
# 10 < t <= 25
r = increasingColourOffset(temperature )
return [r,255,0]
elif (temperature <= 20):
# 25 < t <= 40
g = decreasingColourOffset(temperature - 10)
return [255,g,0]
else:
# 40 < t
return [255,0,0]
return [0,0,0]
bb.displayColor(0,0,0)
while True:
currentTemperature = getTemperature()
pixelCount = currentTemperature + 20
print pixelCount if pixelCount > 0 else 0
colour = convertTemperatureToRgb(currentTemperature)
print colour
for i in range(pixelCount):
colour = convertTemperatureToRgb(i-20)
bb.sendPixel(colour[0],colour[1],colour[2])
for i in range(pixelCount,60):
bb.sendPixel(0,0,0)
bb.show()
time.sleep(1800)