-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRPi_Weather_Station.py
269 lines (238 loc) · 5.7 KB
/
RPi_Weather_Station.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import time
import os
import datetime
from sense_hat import SenseHat
import json, requests
sense = SenseHat()
sense.low_light = True
sense.set_rotation(270) # Optional
sense.clear()
# Get the weather-api
key = 'Your Key Here'
#units = 'metric'
units = 'imperial'
cityid = 'Your ID Here'
#url = requests.get('http://api.openweathermap.org/data/2.5/weather?id='+cityid+'&units='+units+'&APPID='+key)
#weather = json.loads(url.text)
# Display Clock, Date, etc.
show_clock = True
show_month = False
show_temp = True
show_weathre = True
show_mail = False
number = [
0,0,0,0, #zero
0,1,1,1,
0,1,0,1,
0,1,1,1,
0,1,1,0, #one
0,0,1,0,
0,0,1,0,
0,0,1,0,
0,1,1,0, #two
0,0,0,1,
0,0,1,0,
0,1,1,1,
0,1,1,1, #three
0,0,0,1,
0,0,1,1,
0,1,1,1,
0,0,0,1, #four
0,1,0,1,
0,1,1,1,
0,0,0,1,
0,1,1,1, #five
0,1,1,0,
0,0,0,1,
0,1,1,0,
0,1,0,0, #six
0,1,1,0,
0,1,0,1,
0,0,1,0,
0,1,1,1, #seven
0,0,0,1,
0,0,1,0,
0,0,1,0,
0,0,1,0, #eight
0,1,1,1,
0,1,1,1,
0,0,1,0,
0,0,1,0, #nine
0,1,0,1,
0,0,1,1,
0,0,0,1,
0,0,0,0, #blank
0,0,0,0,
0,0,0,0,
0,0,0,0
]
hourColour = [0,0,10] # blue
minuteColour = [0,10,0] # green
empty = [0,0,0] # off / black
clockImage = [
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0
]
e = (0,0,0)
clear = [
e,e,e,e,e,e,e,e,
e,e,e,e,e,e,e,e,
e,e,e,e,e,e,e,e,
e,e,e,e,e,e,e,e,
e,e,e,e,e,e,e,e,
e,e,e,e,e,e,e,e,
e,e,e,e,e,e,e,e,
e,e,e,e,e,e,e,e
]
b = (0, 0, 10)
r = (10, 0, 10)
cloudy = [
e,e,e,e,e,e,e,e,
e,e,e,e,e,e,e,e,
e,e,e,e,b,e,e,e,
e,b,e,b,e,b,b,e,
b,e,b,e,e,e,e,b,
b,e,e,e,e,e,e,b,
e,b,b,b,b,b,b,e,
e,e,e,e,e,e,e,e
]
thermo = [
e,e,e,e,e,e,e,e,
e,e,r,e,r,r,r,e,
e,e,r,e,e,e,e,e,
e,e,r,e,r,r,e,e,
e,e,r,e,e,e,e,e,
e,r,e,r,e,e,e,e,
e,e,r,e,e,e,e,e,
e,e,e,e,e,e,e,e
]
def clock():
now = datetime.datetime.now()
second = now.second
x = 0
y = 7
r = 10
g = 10
b = 10
pixel = (r,g,b)
hour = time.localtime().tm_hour
minute = time.localtime().tm_min
if(hour > 12):
hour = hour-12
if(hour == 0):
hour = 12
# Map digits to clockImage array
pixelOffset = 0
index = 0
for indexLoop in range(0, 4):
for counterLoop in range(0, 4):
if (hour >= 10):
clockImage[index] = number[int(hour/10)*16+pixelOffset]
else:
clockImage[index] = number[int(10)*16+pixelOffset]
clockImage[index+4] = number[int(hour%10)*16+pixelOffset]
clockImage[index+32] = number[int(minute/10)*16+pixelOffset]
clockImage[index+36] = number[int(minute%10)*16+pixelOffset]
pixelOffset = pixelOffset + 1
index = index + 1
index = index + 4
# Colour the hours and minutes
for index in range(0, 64):
if (clockImage[index]):
if index < 32:
clockImage[index] = hourColour
else:
clockImage[index] = minuteColour
else:
clockImage[index] = empty
# Display the time
sense.set_pixels(clockImage)
sense.set_pixel(x, y, pixel)
time.sleep(0.5)
sense.set_pixels(clockImage)
#sense.set_pixels(clear)
time.sleep(0.5)
#time.sleep(.3)
def month():
now = datetime.datetime.now()
second = now.second
x = 0
y = 7
r = 255
g = 0
b = 0
pixel = (r,g,b)
mday = time.localtime().tm_mday
mon = time.localtime().tm_mon
# Map digits to clockImage array
pixelOffset = 0
index = 0
for indexLoop in range(0, 4):
for counterLoop in range(0, 4):
clockImage[index] = number[int(mday/10)*16+pixelOffset]
clockImage[index+4] = number[int(mday%10)*16+pixelOffset]
clockImage[index+32] = number[int(mon/10)*16+pixelOffset]
clockImage[index+36] = number[int(mon%10)*16+pixelOffset]
pixelOffset = pixelOffset + 1
index = index + 1
index = index + 4
# Colour the hours and minutes
for index in range(0, 64):
if (clockImage[index]):
if index < 32:
clockImage[index] = minuteColour
else:
clockImage[index] = hourColour
else:
clockImage[index] = empty
# Display the time
sense.set_pixels(clockImage)
time.sleep(120)
print ('Ready!')
while (True):
if show_mail:
os.system('sudo python /home/pi/SenseHat/Sense_CheckMail.py')
if show_clock:
for x in range(2):
for x in range(8):
clock()
if show_month:
month()
# response = os.system("ping -c 1 api.openweathermap.org")
# if response == 0:
# print 'up'
# show_temp = True
# show_weathre = True
# else:
# print 'down'
# show_temp = False
# show_weathre = False
try:
url = requests.get('http://api.openweathermap.org/data/2.5/weather?id='+cityid+'&units='+units+'&APPID='+key)
show_temp = True
show_weathre = True
if show_weathre:
sense.set_pixels(cloudy)
weather = json.loads(url.text)
weathre = weather['weather'][0]['description']
temp = weather['main']['temp']
main_weather = weather['weather'][0]['main']
sense.set_pixels(cloudy)
time.sleep(1.0)
sense.show_message(str(weathre), text_colour=[0, 0, 10])
time.sleep(0.3)
if show_temp:
sense.set_pixels(thermo)
time.sleep(1.0)
sense.show_message(str(temp), text_colour=[15, 0, 10])
time.sleep(0.3)
except:
show_temp = False
show_weathre = False
sense.show_message("no internet", text_colour=[10, 0, 0])