-
Notifications
You must be signed in to change notification settings - Fork 14
/
updateBlynk.py
503 lines (394 loc) · 19.6 KB
/
updateBlynk.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
from __future__ import print_function
# provides routine to update SGS Blynk Display
import time
import requests
import json
import util
import state
import traceback
# Check for user imports
import config
DEBUGBLYNK = False
def stopFlash():
r = requests.get(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V30?value=0')
def blynkInit():
# initalize button states
try:
if (DEBUGBLYNK):
print("Entering blynkInit:")
r = requests.get(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V5?value=0')
if (state.runOLED == True):
r = requests.get(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V6?value=1')
else:
r = requests.get(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V6?value=0')
r = requests.get(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V30?value=0')
# initialize LEDs
r = requests.get(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V42?value=255')
r = requests.get(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V43?value=255')
if (DEBUGBLYNK):
print("config.English_Metric = ", config.English_Metric)
if (config.English_Metric == 0):
r = requests.get(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V8?value=0')
else:
r = requests.get(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V8?value=1')
if (DEBUGBLYNK):
print("Exiting blynkInit:")
except Exception as e:
print("exception in blynkInit")
print (e)
return 0
def blynkResetButton(buttonNumber):
try:
r = requests.get(config.BLYNK_URL+config.BLYNK_AUTH+'/update/'+buttonNumber+'?value=0')
except Exception as e:
print("exception in blynkResetButton")
print (e)
return 0
def blynkEventUpdate(Event):
try:
put_header={"Content-Type": "application/json"}
val = Event
put_body = json.dumps([val])
if (DEBUGBLYNK):
print("blynkEventUpdate:",val)
r = requests.put(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V31', data=put_body, headers=put_header)
if (DEBUGBLYNK):
print("blynkEventUpdate:POST:r.status_code:",r.status_code)
return 1
except Exception as e:
print("exception in blynkEventUpdate")
print (e)
return 0
def blynkTerminalUpdate(entry):
try:
put_header={"Content-Type": "application/json"}
entry = time.strftime("%Y-%m-%d %H:%M:%S")+": "+entry+"\n"
put_body = json.dumps([entry])
if (DEBUGBLYNK):
print("blynkStateUpdate:Pre:put_body:",put_body)
r = requests.put(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V32', data=put_body, headers=put_header)
if (DEBUGBLYNK):
print("blynkStateUpdate:POST:r.status_code:",r.status_code)
except Exception as e:
print("exception in blynkTerminalUpdate")
print (e)
return 0
def blynkSolarMAXLine(entry):
try:
put_header={"Content-Type": "application/json"}
put_body = json.dumps([entry])
if (DEBUGBLYNK):
print("blynkSolarMAXUpdate:Pre:put_body:",put_body)
r = requests.put(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V75', data=put_body, headers=put_header)
if (DEBUGBLYNK):
print("blynkSolarMAXUpdate:POST:r.status_code:",r.status_code)
except Exception as e:
print("exception in blynkSolarMAXUpdate")
print (e)
return 0
def blynkSolarTerminalUpdate(entry):
try:
put_header={"Content-Type": "application/json"}
entry = time.strftime("%Y-%m-%d %H:%M:%S")+": "+entry+"\n"
put_body = json.dumps([entry])
if (DEBUGBLYNK):
print("blynkStateUpdate:Pre:put_body:",put_body)
r = requests.put(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V33', data=put_body, headers=put_header)
if (DEBUGBLYNK):
print("blynkStateUpdate:POST:r.status_code:",r.status_code)
except Exception as e:
print("exception in blynkTerminalUpdate")
print (e)
return 0
def blynkUpdateImage():
#Blynk.setProperty(V1, "urls", "https://image1.jpg", "https://image2.jpg");
try:
if (DEBUGBLYNK):
print("blynkUpdateImage:started")
"""
r = requests.get(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V70?value=2') # Picture URL
if (DEBUGBLYNK):
print "blynkUpdateImage:OTHER:r.status_code:",r.status_code
#r = requests.get(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V70?urls=http://www.switchdoc.com/2.jpg') # Picture URL
#r = requests.get(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V70?urls=http://www.switchdoc.com/skycamera.jpg,http://www.switchdoc.com/2.jpg') # Picture URL
r = requests.get(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V70?value=1;url=http://www.switchdoc.com/skycamera.jpg')
if (DEBUGBLYNK):
print "blynkUpdateImage:OTHER:r.status_code:",r.status_code
r = requests.get(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V70?value=2;url=http://www.switchdoc.com/2.jpg') # Picture URL
if (DEBUGBLYNK):
print "blynkUpdateImage:OTHER:r.status_code:",r.status_code
r = requests.get(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V70?value=2') # Picture URL
if (DEBUGBLYNK):
print "blynkUpdateImage:OTHER:r.status_code:",r.status_code
"""
r = requests.get(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V70?urls=http://www.switchdoc.com/SkyWeatherNoAlpha.png') # Picture URL
except Exception as e:
print("exception in blynkUpdateImage")
print (e)
return 0
def blynkStateUpdate():
try:
# do not blynk if no main reading yet
if (state.lastMainReading != "Never"):
blynkUpdateImage()
put_header={"Content-Type": "application/json"}
# set last sample time
put_header={"Content-Type": "application/json"}
val = time.strftime("%Y-%m-%d %H:%M:%S")
put_body = json.dumps([val])
if (DEBUGBLYNK):
print("blynkEventUpdate:",val)
r = requests.put(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V44', data=put_body, headers=put_header)
if (DEBUGBLYNK):
print("blynkEventUpdate:POST:r.status_code:",r.status_code)
# do the graphs
if (config.USEWSAQI):
val = state.WS_AQI
else:
val = state.AQI
put_body = json.dumps([val])
if (DEBUGBLYNK):
print("blynkStateUpdate:Pre:put_body:",put_body)
r = requests.put(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V7', data=put_body, headers=put_header)
if (DEBUGBLYNK):
print("blynkStateUpdate:POST:r.status_code:",r.status_code)
val = util.returnTemperatureCF(state.OutdoorTemperature)
tval = "{0:0.1f} ".format(val) + util.returnTemperatureCFUnit()
put_body = json.dumps([tval])
r = requests.put(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V0', data=put_body, headers=put_header)
val = util.returnTemperatureCF(state.OutdoorTemperature)
put_body = json.dumps([val])
r = requests.put(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V10', data=put_body, headers=put_header)
val = state.OutdoorHumidity
put_body = json.dumps(["{0:0.1f}%".format(val)])
r = requests.put(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V1', data=put_body, headers=put_header)
val = state.OutdoorHumidity
put_body = json.dumps([val])
r = requests.put(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V11', data=put_body, headers=put_header)
val = util.returnTemperatureCF(state.IndoorTemperature)
tval = "{0:0.1f} ".format(val) + util.returnTemperatureCFUnit()
put_body = json.dumps([tval])
r = requests.put(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V21', data=put_body, headers=put_header)
val = util.returnTemperatureCF(state.IndoorTemperature)
tval = "{0:0.1f}".format(val)
put_body = json.dumps([val])
r = requests.put(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V120', data=put_body, headers=put_header)
val = state.IndoorHumidity
put_body = json.dumps(["{0:0.1f}%".format(val)])
r = requests.put(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V13', data=put_body, headers=put_header)
val = state.IndoorHumidity
put_body = json.dumps(["{0:0.1f}".format(val)])
r = requests.put(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V121', data=put_body, headers=put_header)
if (state.fanState == False):
val = 0
else:
val = 1
put_body = json.dumps([val])
r = requests.put(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V122', data=put_body, headers=put_header)
#wind
val = util.returnWindSpeed(state.WindSpeed)
tval = "{0:0.1f}".format(val) + util.returnWindSpeedUnit()
put_body = json.dumps([tval])
r = requests.put(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V9', data=put_body, headers=put_header)
#now humiidyt
#val = util.returnWindSpeed(state.WindSpeed)
val = state.OutdoorHumidity
put_body = json.dumps([val])
r = requests.put(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V19', data=put_body, headers=put_header)
# outdoor Air Quality
if (config.USEWSAQI):
val = state.WS_AQI
else:
val = state.AQI
put_body = json.dumps([val])
r = requests.put(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V20', data=put_body, headers=put_header)
#wind direction
val = "{0:0.0f}/".format(state.WindDirection) + util.returnWindDirection(state.WindDirection)
put_body = json.dumps([val])
r = requests.put(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V2', data=put_body, headers=put_header)
#rain
val = "{0:0.2f}".format(state.TotalRain)
if (config.English_Metric == 1):
tval = "{0:0.2f}mm".format(state.TotalRain)
else:
tval = "{0:0.2f}in".format(state.TotalRain / 25.4)
put_body = json.dumps([tval])
r = requests.put(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V3', data=put_body, headers=put_header)
#Sunlight
val = "{0:0.0f}".format(state.SunlightVisible)
#print ("Sunlight Val = ", state.SunlightVisible)
put_body = json.dumps([val])
r = requests.put(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V4', data=put_body, headers=put_header)
#Sunlight for Graph
val = "{0:0.0f}".format(state.SunlightVisible)
#print ("Sunlight Val = ", state.SunlightVisible)
put_body = json.dumps([val])
r = requests.put(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V130', data=put_body, headers=put_header)
#barometric Pressure
if (config.English_Metric == 1):
tval = "{0:0.2f}hPa".format(state.BarometricPressureSeaLevel*10.0)
else:
tval = "{0:0.2f}in".format((state.BarometricPressureSeaLevel * 0.2953))
put_body = json.dumps([tval])
r = requests.put(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V40', data=put_body, headers=put_header)
#barometric Pressure graph
if (config.English_Metric == 1):
tval = "{0:0.2f}".format(state.BarometricPressureSeaLevel)
else:
tval = "{0:0.2f}".format((state.BarometricPressureSeaLevel * 0.2953))
put_body = json.dumps([tval])
r = requests.put(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V41', data=put_body, headers=put_header)
#solar data
val = "{0:0.2f}".format(state.solarVoltage)
put_body = json.dumps([val])
r = requests.put(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V50', data=put_body, headers=put_header)
val = "{0:0.1f}".format(state.solarCurrent)
put_body = json.dumps([val])
r = requests.put(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V51', data=put_body, headers=put_header)
val = "{0:0.2f}".format(state.batteryVoltage)
put_body = json.dumps([val])
r = requests.put(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V52', data=put_body, headers=put_header)
val = "{0:0.1f}".format(state.batteryCurrent)
put_body = json.dumps([val])
r = requests.put(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V53', data=put_body, headers=put_header)
val = "{0:0.2f}".format(state.loadVoltage)
put_body = json.dumps([val])
r = requests.put(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V54', data=put_body, headers=put_header)
val = "{0:0.1f}".format(state.loadCurrent)
put_body = json.dumps([val])
r = requests.put(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V55', data=put_body, headers=put_header)
val = "{0:0.1f}W".format(state.batteryPower)
put_body = json.dumps([val])
r = requests.put(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V60', data=put_body, headers=put_header)
val = "{0:0.1f}W".format(state.solarPower)
put_body = json.dumps([val])
r = requests.put(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V61', data=put_body, headers=put_header)
val = "{0:0.1f}W".format(state.loadPower)
put_body = json.dumps([val])
r = requests.put(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V62', data=put_body, headers=put_header)
if (config.SolarMAX_Present == True):
blynkSolarMAXLine(state.SolarMAXLastReceived)
if (state.SolarMAXLastReceived != "Never"):
val = util.returnTemperatureCF(state.SolarMaxInsideTemperature)
tval = "{0:0.1f} ".format(val) + util.returnTemperatureCFUnit()
put_body = json.dumps([tval])
r = requests.put(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V76', data=put_body, headers=put_header)
val = "{0:0.1f}%".format(state.SolarMaxInsideHumidity)
put_body = json.dumps([val])
r = requests.put(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V77', data=put_body, headers=put_header)
val = "{0:0.1f}".format(state.batteryCharge)
put_body = json.dumps([val])
r = requests.put(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V56', data=put_body, headers=put_header)
val = "{0:0.1f}".format(state.batteryCharge)
put_body = json.dumps([val])
r = requests.put(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V127', data=put_body, headers=put_header)
delta = util.returnTemperatureCF(state.IndoorTemperature)- util.returnTemperatureCF(state.OutdoorTemperature)
val = "{0:0.1f}".format(delta)
put_body = json.dumps([val])
r = requests.put(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V128', data=put_body, headers=put_header)
# LEDs
if (state.barometricTrend): #True is up, False is down
r = requests.get(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V42?color=%2300FF00') # Green
if (DEBUGBLYNK):
print("blynkAlarmUpdate:OTHER:r.status_code:",r.status_code)
else:
r = requests.get(config.BLYNK_URL+config.BLYNK_AUTH+'/update/V42?color=%23FF0000') # red
return 1
except Exception as e:
print("exception in blynkStateUpdate")
print(traceback.format_exc())
print (e)
return 0
def blynkStatusUpdate():
if (DEBUGBLYNK):
print("blynkStatusUpdate Entry")
try:
put_header={"Content-Type": "application/json"}
# look for English or Metric
r = requests.get(config.BLYNK_URL+config.BLYNK_AUTH+'/get/V8') # read button state
if (DEBUGBLYNK):
print("blynkStatusUpdate:POSTEM:r.status_code:",r.status_code)
print("blynkStatusUpdate:POSTEM:r.text:",r.text)
if (r.text == '["1"]'):
if (config.English_Metric == 0):
config.English_Metric = 1
if (DEBUGBLYNK):
print("blynkStatusUpdate:POSTBRC:config.English_Metric set to Metric")
blynkTerminalUpdate("Set to Metric Units ")
f = open("/home/pi/SDL_Pi_SkyWeather/config.English_Metric.txt", "w")
f.write("1")
f.close()
else:
if (config.English_Metric == 1):
config.English_Metric = 0
f = open("/home/pi/SDL_Pi_SkyWeather/config.English_Metric.txt", "w")
f.write("0")
f.close()
if (DEBUGBLYNK):
print("blynkStatusUpdate:POSTBRC:config.English_Metric set to English")
blynkTerminalUpdate("Set to English Units ")
# look for rainbow button change
r = requests.get(config.BLYNK_URL+config.BLYNK_AUTH+'/get/V5') # read button state
if (DEBUGBLYNK):
print("blynkStatusUpdate:POSTBR:r.status_code:",r.status_code)
print("blynkStatusUpdate:POSTBR:r.text:",r.text)
if (r.text == '["1"]'):
state.runRainbow = True
blynkTerminalUpdate("Turning Rainbow On ")
if (DEBUGBLYNK):
print("blynkStatusUpdate:POSTBRC:state.runRainbow set to True")
else:
if(state.runRainbow == True):
blynkTerminalUpdate("Turning Rainbow Off ")
state.runRainbow = False
if (DEBUGBLYNK):
print("blynkStatusUpdate:POSTBRC:state.runRainbow set to False")
# turn OLED ON and OFF
r = requests.get(config.BLYNK_URL+config.BLYNK_AUTH+'/get/V6') # read button state
#if (DEBUGBLYNK):
if (r.text == '["1"]'):
if (state.runOLED == False):
state.runOLED = True
blynkTerminalUpdate("Turning OLED On ")
if (DEBUGBLYNK):
print("blynkStatusUpdate:POSTBRO:state.runOLED set to True")
if (config.OLED_Originally_Present == True):
config.OLED_Present = True
util.turnOLEDOn()
else:
if (state.runOLED == True):
blynkTerminalUpdate("Turning OLED Off ")
state.runOLED = False
if (DEBUGBLYNK):
print("blynkStatusUpdate:POSTBRO:state.runOLED set to False")
if (config.OLED_Originally_Present == True):
config.OLED_Present = False
util.turnOLEDOff()
# look for Flash Strip Command
r = requests.get(config.BLYNK_URL+config.BLYNK_AUTH+'/get/V30') # read button state
if (DEBUGBLYNK):
print("blynkStatusUpdate:POSTBF:r.status_code:",r.status_code)
print("blynkStatusUpdate:POSTBF:r.text:",r.text)
if (r.text == '["1"]'):
state.flashStrip = True
if (DEBUGBLYNK):
print("blynkStatusUpdate:POSTBRF:state.flashStrip set to True")
else:
state.flashStrip = False
if (DEBUGBLYNK):
print("blynkStatusUpdate:POSTBRF:state.flashStrip set to False")
return 1
except Exception as e:
print("exception in blynkStatusUpdate")
print (e)
return 0
def blynkSGSAppOnline():
try:
r = requests.get(config.BLYNK_URL+config.BLYNK_AUTH+'/isAppConnected')
if (DEBUGBLYNK):
print("blynkSGSAppOnline:POSTCHECK:r.text:",r.text)
return r.text
except Exception as e:
print("exception in blynkApponline")
print (e)
return ""