-
Notifications
You must be signed in to change notification settings - Fork 17
/
LEDWeather.py
121 lines (94 loc) · 3.2 KB
/
LEDWeather.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# Original example by Zach Hoeken Smith (http://hoektronics.com)
# Modified for the new Blinkyboard library by Max Henstell 3/2013
# Modified for Weather Underground by Marty McGuire 4/2013
# To use this behind a proxy, set the http_proxy environment variable, like so:
# export http_proxy=http://127.0.0.1:3213
# before running this script
import blinkytape
import glob
import os
import colorsys
import tempfile
import json
import time
import sys
if sys.version_info > (3, 0):
import urllib.request as requestlib
else:
import urllib2 as requestlib
# Get your API key at http://www.wunderground.com/weather/api
# Specify it here or in the WUNDERGROUND_KEY environment var. E.g.:
# $ WUNDERGROUND_KEY=abcdefg123456 python LEDWeather.py
#apikey = "abcdefg123456"
apikey = os.environ.get('WUNDERGROUND_KEY')
state = "NV"
city = "Las_Vegas"
url = "http://api.wunderground.com/api/{}/hourly/q/{}/{}.json".format(
apikey, state, city)
def connect(port):
if not port:
sys.exit("Could not locate a BlinkyTape.")
print("BlinkyTape found at: %s" % port)
bt = blinkytape.BlinkyTape(port)
bt.displayColor(0, 0, 0)
return bt
def get_hourly_data():
print("[%d] Fetching %s" % (time.time(), url))
try:
page_data = requestlib.urlopen(url)
data = json.load(page_data)
if not len(data) or data is None:
raise Exception("Error parsing hourly data")
return data
except Exception as ex:
print(ex)
color_map = {
-10: (255, 0, 255),
0: (158, 0, 255),
10: (0, 0, 255),
20: (0, 126, 255),
30: (0, 204, 255),
40: (5, 247, 247),
50: (127, 255, 0),
60: (247, 247, 5),
70: (255, 204, 0),
80: (255, 153, 0),
90: (255, 79, 0),
100: (204, 0, 0),
110: (169, 3, 3),
120: (186, 50, 50)
}
def color_for_temp(temp):
"""Returns an RGB color triplet for the given (Fahrenheit scale) temp.
Temps colors taken from the Weather Channel mapping found at:
http://wattsupwiththat.com/2008/06/26/color-and-temperature-perception-is-everything/
"""
color = None
for temp_ceil in sorted(color_map.keys()):
color = color_map[temp_ceil]
if temp < temp_ceil:
break
return adjust_color(color)
def adjust_color(color, dim_factor=0.10):
r, g, b = color
h, s, v = colorsys.rgb_to_hsv(r / 256.0, g / 256.0, b / 256.0)
r, g, b = colorsys.hsv_to_rgb(h, s, v * dim_factor)
return int(r * 256), int(g * 256), int(b * 256)
if __name__ == "__main__":
import optparse
parser = optparse.OptionParser()
parser.add_option("-p", "--port", dest="portname",
help="serial port (ex: /dev/ttyUSB0)", default=None)
(options, args) = parser.parse_args()
bt = connect(options.portname)
data = get_hourly_data()
if not data:
sys.exit(
"Could not fetch weather data. Check your proxy settings and try again. Try: export http_proxy=PROXY_IP:PROXY_PORT before running this script.")
print(data)
for hour in data['hourly_forecast']:
temp = int(hour['temp']['english'])
r, g, b = color_for_temp(temp)
print("Temp: {}. Color: {},{},{}".format(temp, r, g, b))
bt.sendPixel(r, g, b)
bt.show()