forked from DJAkbar/cloudy-a
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudy-a.py
326 lines (288 loc) · 8.89 KB
/
cloudy-a.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import urllib2
import json
import sys
import time
import datetime
import random
import pigpio
#insert the WeatherUnderground API-key here:
wunder_api = ""
#insert the location for the forecast here. For example "Germany/Berlin"
location = ""
#this decides when it is time to display the forecast for the next day. If you want to show always the forecast for the day, set it to 24:
switch_time = 24
#time we call the API
UpdateTimeInMinutes = 60
#Valid Values are 1,2,3, and 4
Brightness =4
#mapping the colors rather then hardcode
r1 = 17
g1 = 24
b1 = 22
r2 = 26
g2 = 13
b2 = 12
#turns on output to web interface, as well as local paths of where you will save files
WebInterfaceActive = False
WebInterfacePath = '/var/www/html/CloudyPie/'
#------------DO NOT EDIT BELOW THIS LINE UNLESS YOU ARE A PROGRAMMER ------------#
#region Global Variables
NextUpdate = datetime.datetime.now() + datetime.timedelta(minutes = -1) #forces an update right away
condition = ""
conditions = {"cloudy" : "cloudy", "nt_cloudy" : "cloudy", "fog" : "cloudy", "nt_fog" : "cloudy", "hazy" : "cloudy", "nt_hazy" : "cloudy", "mostlycloudy" : "cloudy", "nt_mostlycloudy" : "cloudy", "partlycloudy" : "cloudy", "nt_partlycloudy" : "cloudy", "clear" : "sunny", "nt_clear" : "sunny", "sunny" : "sunny", "nt_sunny" : "sunny", "mostlysunny" : "sunny", "nt_mostlysunny" : "sunny", "partlysunny" : "sunny", "nt_partlysunny" : "sunny", "chanceflurries" : "snowy", "nt_chanceflurries" : "snowy", "flurries" : "snowy", "nt_flurries" : "snowy", "chancesnow" : "snowy", "nt_chancesnow" : "snowy", "snow" : "snowy", "nt_snow" : "snowy", "chancerain" : "rainy", "nt_chancerain" : "rainy", "chancesleet" : "rainy", "nt_chancesleet" : "rainy", "sleet" : "rainy", "nt_sleet" : "rainy", "rain" : "rainy", "nt_rain" : "rainy", "tstorms" : "stormy", "nt_tstorms" : "stormy", "chancetstorms" : "stormy", "nt_chancetstorms" : "stormy"}
bright=0
brightnew=0
brightnew2=0
bright2=0
fadetime=0
fadetime2=0
maxBrightness = 63 * Brightness
pi = pigpio.pi()
def WeatherNeedsUpdating():
return NextUpdate < datetime.datetime.now()
def CheckWeather():
global NextUpdate
global condition
if WeatherNeedsUpdating():
NextUpdate = datetime.datetime.now() + datetime.timedelta(minutes = UpdateTimeInMinutes)
try:
myweather = json.load(urllib2.urlopen('http://api.wunderground.com/api/' + wunder_api + '/forecast/q/' + location + '.json'))
myweather_sum = myweather['forecast']['simpleforecast']['forecastday']
if int(time.strftime("%H")) < int(switch_time):
ampm = 1
else:
ampm = 2
for period in myweather_sum:
if period['period'] == ampm:
orig_conditions = period['icon']
condition = conditions[orig_conditions]
if WebInterfaceActive:
writeOutput(json.dumps(myweather), WebInterfacePath + "output.json")
writeOutput(datetime.datetime.now().strftime("%c"), WebInterfacePath + "Lastupdated.log")
writeOutput("", WebInterfacePath + "error.log")
except Exception as e:
print(str(e))
NextUpdate = datetime.datetime.now() + datetime.timedelta(seconds = 30)
condition = "Error"
if WebInterfaceActive:
writeOutput(str(e), WebInterfacePath + "error.log")
#These are the different animations for the LED-strips:
def rain():
blueishness1=maxBrightness
blueishness2=0
fadetimeblue=random.uniform(0.02,0.06)
pi.set_PWM_dutycycle(r1,0)
pi.set_PWM_dutycycle(g1,0)
pi.set_PWM_dutycycle(r2,0)
pi.set_PWM_dutycycle(g2,0)
while blueishness1 !=0 and blueishness2 !=maxBrightness:
blueishness1=blueishness1-1
blueishness2=blueishness2+1
time.sleep(fadetimeblue)
pi.set_PWM_dutycycle(b1,blueishness1)
pi.set_PWM_dutycycle(b2,blueishness2)
time.sleep(random.uniform(0.1,2))
while blueishness1 !=maxBrightness and blueishness2 !=0:
blueishness1=blueishness1+1
blueishness2=blueishness2-1
time.sleep(fadetimeblue)
pi.set_PWM_dutycycle(b1,blueishness1)
pi.set_PWM_dutycycle(b2,blueishness2)
time.sleep(random.uniform(0.1,2))
def cloud():
whiteness1=maxBrightness
whiteness2=0
fadetimewhite=random.uniform(0.02,0.04)
while whiteness1 != 0 and whiteness2 != maxBrightness:
whiteness1=whiteness1-1
whiteness2=whiteness2+1
time.sleep(fadetimewhite)
pi.set_PWM_dutycycle(g2,whiteness1)
pi.set_PWM_dutycycle(r2,whiteness1)
pi.set_PWM_dutycycle(b2,whiteness1)
pi.set_PWM_dutycycle(r1,whiteness2)
pi.set_PWM_dutycycle(g1,whiteness2)
pi.set_PWM_dutycycle(b1,whiteness2)
time.sleep(random.uniform(0.5,1))
while whiteness1 != maxBrightness and whiteness2 != 0:
whiteness1=whiteness1+1
whiteness2=whiteness2-1
time.sleep(fadetimewhite)
pi.set_PWM_dutycycle(g2,whiteness1)
pi.set_PWM_dutycycle(r2,whiteness1)
pi.set_PWM_dutycycle(b2,whiteness1)
pi.set_PWM_dutycycle(r1,whiteness2)
pi.set_PWM_dutycycle(g1,whiteness2)
pi.set_PWM_dutycycle(b1,whiteness2)
time.sleep(random.uniform(0.5,1))
def sun():
yellowness1=maxBrightness
yellowness2=0
fadetimeyellow=random.uniform(0.05,0.06)
pi.set_PWM_dutycycle(b1,0)
pi.set_PWM_dutycycle(b2,0)
while yellowness1 != 0 and yellowness2 != maxBrightness:
yellowness1=yellowness1-1
yellowness2=yellowness2+1
time.sleep(fadetimeyellow)
pi.set_PWM_dutycycle(g2,(yellowness1/5))
pi.set_PWM_dutycycle(r2,yellowness1)
pi.set_PWM_dutycycle(r1,yellowness2)
pi.set_PWM_dutycycle(g1,(yellowness2/5))
time.sleep(random.uniform(0.1,0.5))
while yellowness1 != maxBrightness and yellowness2 != 0:
yellowness1=yellowness1+1
yellowness2=yellowness2-1
time.sleep(fadetimeyellow)
pi.set_PWM_dutycycle(g2,(yellowness1/5))
pi.set_PWM_dutycycle(r2,yellowness1)
pi.set_PWM_dutycycle(r1,yellowness2)
pi.set_PWM_dutycycle(g1,(yellowness2/5))
time.sleep(random.uniform(0.1,0.5))
def snow():
global bright
global brightnew
global brightnew2
global bright2
global fadetime
global fadetime2
SnowBright = 61 * Brightness
for flash_i in range(190):
start=random.randint(1,100)
if start==1 and bright==0:
brightnew= 50*Brightness #200
if bright==0:
fadetime=random.uniform(0.002,0.0025)
else:
pass
elif start==2 and bright2==0:
brightnew2=50 * Brightness
if bright2==0:
fadetime2=random.uniform(0.002,0.0025)
else:
pass
else:
pass
if brightnew > bright and bright < SnowBright:
bright=bright+1
elif brightnew == bright:
brightnew=0
elif brightnew < bright and bright !=0:
bright=bright-1
else:
pass
if brightnew2 > bright2 and bright2 < SnowBright:
bright2=bright2+1
elif brightnew2 == bright2:
brightnew2=0
elif brightnew2 < bright2 and bright2 !=0:
bright2=bright2-1
else:
pass
pi.set_PWM_dutycycle(r1, bright+55)
pi.set_PWM_dutycycle(b1, bright+55)
pi.set_PWM_dutycycle(g1, bright+55)
time.sleep(fadetime)
pi.set_PWM_dutycycle(r2, bright2+55)
pi.set_PWM_dutycycle(b2, bright2+55)
pi.set_PWM_dutycycle(g2, bright2+55)
time.sleep(fadetime2)
def flash():
global bright
global brightnew
global brightnew2
global bright2
global fadetime
global fadetime2
MinBrightness = 13*maxBrightness
for flash_i in range(100):
start=random.randint(1,210)
if start==1:
brightnew=random.randint(MinBrightness,maxBrightness)
if bright==0:
fadetime=random.uniform(0.002,0.006)
else:
pass
elif start==2:
brightnew2=random.randint(MinBrightness,maxBrightness)
if bright2==0:
fadetime2=random.uniform(0.002,0.006)
else:
pass
else:
pass
if bright+brightnew<maxBrightness:
bright=bright+brightnew
brightnew=0
else:
pass
if bright2+brightnew2<maxBrightness:
bright2=bright2+brightnew2
brightnew2=0
else:
pass
pi.set_PWM_dutycycle(r1, bright)
if bright>50:
pi.set_PWM_dutycycle(b1, bright)
else:
pi.set_PWM_dutycycle(b1, 50)
pi.set_PWM_dutycycle(g1, bright)
time.sleep(fadetime)
if bright !=0:
bright=bright-1
else:
pass
pi.set_PWM_dutycycle(r2, bright2)
if bright2>50:
pi.set_PWM_dutycycle(b2, bright2)
else:
pi.set_PWM_dutycycle(b2, 50)
pi.set_PWM_dutycycle(g2, bright2)
time.sleep(fadetime2)
if bright2 !=0:
bright2=bright2-1
else:
pass
def error_value():
pi.set_PWM_dutycycle(r2, 100)
time.sleep(1)
pi.set_PWM_dutycycle(r1, 100)
time.sleep(1)
pi.set_PWM_dutycycle(r2, 0)
time.sleep(1)
pi.set_PWM_dutycycle(r1, 0)
time.sleep(1)
def writeOutput(text, filename):
fh = open(filename,"w")
fh.write(text)
fh.close()
#this executes the main loop. E.g. it is looking for the conditions and decides for the animation that should be displayed:
def main_loop():
while 1:
CheckWeather()
if condition=="rainy":
rain()
elif condition=="cloudy":
cloud()
elif condition=="sunny":
sun()
elif condition=="snowy":
snow()
elif condition=="stormy":
flash()
else:
error_value()
time.sleep(0.2)
#Start
if __name__ == '__main__':
try:
main_loop()
except KeyboardInterrupt:
print >> sys.stderr, '\nExiting by user request.\n'
sys.exit(0)
except Exception as e:
if WebInterfaceActive:
writeOutput(str(e), WebInterfacePath + "error.log")
sys.exit(0)