-
Notifications
You must be signed in to change notification settings - Fork 20
/
SkyCamera.py
409 lines (333 loc) · 9.37 KB
/
SkyCamera.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
import requests
import time
import picamera
import state
import hashlib
from PIL import ImageFont, ImageDraw, Image
import traceback
import util
import datetime as dt
# Check for user imports
try:
import conflocal as config
except ImportError:
import config
def SkyWeatherKeyGeneration(userKey):
catkey = "AZWqNqDMhvK8Lhbb2jtk1bucj0s2lqZ6" +userKey
md5result = hashlib.md5(catkey)
#print ("hashkey =", md5result.hexdigest())
return md5result.hexdigest()
def takeSkyPicture():
if (config.SWDEBUG):
print ("--------------------")
print ("SkyCam Picture Taken")
print ("--------------------")
camera = picamera.PiCamera()
camera.exposure_mode = "auto"
try:
camera.rotation = 180
#camera.rotation = 270
camera.resolution = (1920, 1080)
# Camera warm-up time
time.sleep(2)
camera.capture('static/skycamera.jpg')
# now add timestamp to jpeg
pil_im = Image.open('static/skycamera.jpg')
draw = ImageDraw.Draw(pil_im)
# Choose a font
font = ImageFont.truetype("/usr/share/fonts/truetype/freefont/FreeSans.ttf", 25)
# set up units
#wind
val = util.returnWindSpeed(state.ScurrentWindSpeed)
WindStval = "{0:0.1f}".format(val) + util.returnWindSpeedUnit()
val = util.returnWindSpeed(state.ScurrentWindGust)
WindGtval = "{0:0.1f}".format(val) + util.returnWindSpeedUnit()
val = util.returnTemperatureCF(state.currentOutsideTemperature)
OTtval = "{0:0.1f} ".format(val) + util.returnTemperatureCFUnit()
myText = "SkyWeather %s Wind Speed: %s Wind Gust: %s Temp: %s " % (dt.datetime.now().strftime('%d-%b-%Y %H:%M:%S'),WindStval, WindGtval, OTtval)
# Draw the text
color = 'rgb(255,255,255)'
#draw.text((0, 0), myText,fill = color, font=font)
# get text size
text_size = font.getsize(myText)
# set button size + 10px margins
button_size = (text_size[0]+20, text_size[1]+10)
# create image with correct size and black background
button_img = Image.new('RGBA', button_size, "black")
# put text on button with 10px margins
button_draw = ImageDraw.Draw(button_img)
button_draw.text((10, 5), myText, fill = color, font=font)
# put button on source image in position (0, 0)
pil_im.paste(button_img, (0, 0))
bg_w, bg_h = pil_im.size
# WeatherSTEM logo in lower left
size = 64
WSLimg = Image.open("static/WeatherSTEMLogoSkyBackground.png")
WSLimg.thumbnail((size,size),Image.ANTIALIAS)
pil_im.paste(WSLimg, (0, bg_h-size))
# SkyWeather log in lower right
SWLimg = Image.open("static/SkyWeatherLogoSymbol.png")
SWLimg.thumbnail((size,size),Image.ANTIALIAS)
pil_im.paste(SWLimg, (bg_w-size, bg_h-size))
# Save the image
pil_im.save('static/skycamera.jpg', format= 'JPEG')
pil_im.save('static/skycameraprocessed.jpg', format= 'JPEG')
time.sleep(2)
except:
if (config.SWDEBUG):
print(traceback.format_exc())
print ("--------------------")
print ("SkyCam Picture Failed")
print ("--------------------")
finally:
try:
camera.close()
except:
if (config.SWDEBUG):
print ("--------------------")
print ("SkyCam Close Failed ")
print ("--------------------")
if (config.USEWEATHERSTEM == True):
sendSkyWeather()
import base64
def sendSkyWeather():
# defining the api-endpoint
API_ENDPOINT = "https://skyweather.weatherstem.com/"
with open("static/skycamera.jpg", "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
if (config.SWDEBUG):
print ("--------------------")
print ("SkyCam Package Sending")
print ("--------------------")
if(state.barometricTrend == True):
bptrendvalue = "Rising"
else:
bptrendvalue = "Falling"
currentTime = time.time()
data = {
"SkyWeatherVersion": config.SWVERSION,
"SkyWeatherHardware": config.STATIONHARDWARE,
"api_key": state.WeatherSTEMHash,
"device":{
"key": config.STATIONKEY,
"MAC":config.STATIONMAC,
},
"utc":currentTime,
"sensors":[
{
"name":"OutsideTemperature",
"value": state.currentOutsideTemperature,
"units" : "C"
},
{
"name":"OutsideHumidity",
"value": state.currentOutsideHumidity,
"units" : "%"
},
{
"name":"InsideTemperature",
"value": state.currentInsideTemperature,
"units" : "C"
},
{
"name":"InsideHumidity",
"value": state.currentInsideHumidity,
"units" : "%"
},
{
"name":"RainInLast60Minutes",
"value": state.currentRain60Minutes,
"units" : "mm/h"
},
{
"name":"VisibleSunlight",
"value": state.currentSunlightVisible,
"units" : "lux"
},
{
"name":"IRSunlight",
"value": state.currentSunlightIR,
"units" : "lux"
},
{
"name":"UVSunlightt",
"value": state.currentSunlightUV,
"units" : "lux"
},
{
"name":"WindSpeed",
"value": state.ScurrentWindSpeed,
"units" : "kph"
},
{
"name":"WindGust",
"value": state.ScurrentWindGust,
"units" : "kph"
},
{
"name":"WindDirection",
"value": state.ScurrentWindDirection,
"units" : "degrees"
},
{
"name":"totalRain",
"value": state.currentTotalRain,
"units" : "mm"
},
{
"name":"BarometricPressure",
"value": state.currentBarometricPressure,
"units" : "hPa"
},
{
"name":"Altitude",
"value": state.currentAltitude,
"units" : "m"
},
{
"name":"SeaLevelPressure",
"value": state.currentSeaLevel,
"units" : "hPa"
},
{
"name":"BarometricTrend",
"value": bptrendvalue,
"units" : ""
},
{
"name":"OutdoorAirQuality",
"value": state.Outdoor_AirQuality_Sensor_Value,
"units" : "AQI"
},
{
"name":"IndoorAirQuality",
"value": state.Indoor_AirQuality_Sensor_Value,
"units" : "AQI"
},
{
"name":"LastLightningDistance",
"value": state.currentAs3935LastDistance,
"units" : "km"
},
{
"name":"LastLightningTimeStamp",
"value": state.currentAs3935LastLightningTimeStamp,
"units" : ""
}
],
"solarpower":[
{
"name":"BatteryVoltage",
"value": state.batteryVoltage,
"units" : "V"
},
{
"name":"BatteryCurrent",
"value": state.batteryCurrent,
"units" : "ma"
},
{
"name":"SolarVoltage",
"value": state.solarVoltage,
"units" : "V"
},
{
"name":"SolarCurrent",
"value": state.solarCurrent,
"units" : "ma"
},
{
"name":"LoadVoltage",
"value": state.loadVoltage,
"units" : "V"
},
{
"name":"LoadCurrent",
"value": state.loadCurrent,
"units" : "ma"
},
{
"name":"BatteryPower",
"value": state.batteryPower,
"units" : "W"
},
{
"name":"SolarPower",
"value": state.solarPower,
"units" : "W"
},
{
"name":"LoadPower",
"value": state.loadPower,
"units" : "W"
},
{
"name":"BatteryCharge",
"value": state.batteryCharge,
"units" : "%"
},
{
"name":"WXBatteryVoltage",
"value": state.WXbatteryVoltage,
"units" : "V"
},
{
"name":"WXBatteryCurrent",
"value": state.WXbatteryCurrent,
"units" : "ma"
},
{
"name":"WXSolarVoltage",
"value": state.WXsolarVoltage,
"units" : "V"
},
{
"name":"WXSolarCurrent",
"value": state.WXsolarCurrent,
"units" : "ma"
},
{
"name":"WXLoadVoltage",
"value": state.WXloadVoltage,
"units" : "V"
},
{
"name":"WXLoadCurrent",
"value": state.WXloadCurrent,
"units" : "ma"
},
{
"name":"WXBatteryPOWER",
"value": state.WXbatteryPower,
"units" : "W"
},
{
"name":"WXSolarPower",
"value": state.WXsolarPower,
"units" : "W"
},
{
"name":"WXLoadPower",
"value": state.WXloadPower,
"units" : "W"
},
{
"name":"WXBatteryCharge",
"value": state.WXbatteryCharge,
"units" : "%"
}
],
"cameras":[
{
"name":"Sky Camera",
"image": encoded_string
}
]
}
# sending post request and saving response as response object
r = requests.post(url = API_ENDPOINT, json = data)
#print data
# extracting response text
pastebin_url = r.text
if (config.SWDEBUG):
print("The pastebin URL is (r.text):%s"%pastebin_url)