-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.py
executable file
·475 lines (423 loc) · 18.6 KB
/
code.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
import board
import digitalio
import time
import adafruit_ahtx0
import busio
import alarm
import ipaddress
import ssl
import wifi
import socketpool
import adafruit_requests
import rtc
import os
import neopixel
from adafruit_lc709203f import LC709203F
import adafruit_requests as requests
from adafruit_fona.adafruit_fona import FONA
from adafruit_fona.fona_3g import FONA3G
import adafruit_fona.adafruit_fona_network as network
import adafruit_fona.adafruit_fona_socket as cellular_socket
pixels = neopixel.NeoPixel(board.NEOPIXEL, 1)
##################
## Set pins to safe values in case the sms board is attached
###################
power_pin = digitalio.DigitalInOut(board.D9)
power_pin.direction = digitalio.Direction.INPUT
reset_pin = digitalio.DigitalInOut(board.D10)
reset_pin.direction = digitalio.Direction.INPUT
LTE_SHIELD_POWER_PULSE_PERIOD = 3.2
LTE_RESET_PULSE_PERIOD = 10.0
## SMS relay number - should be a twilio number and entered as an integer with country code
SMS_RELAY_NUMBER = 16469709199
SMS_QUEUE_LENGTH = 2
###########
# Get wifi details and more from a secrets.py file
try:
from secrets import secrets
except ImportError:
print("WiFi secrets are kept in secrets.py, please add them there!")
raise
## URL
HEATSEEK_URL = "http://relay.heatseek.org/temperatures"
CODE_VERSION = "F-CP-1.1.3"
VOLT_DIFF_FOR_CHARGE = 0.06
QUIET_MODE_SLEEP_LENGTH = 600
## FUNCTIONS
def init_sms_board():
print("Initializing the Cell board with a power pulse")
# Initialize the modem
power_pin.switch_to_output()
power_pin.value = False
time.sleep(LTE_SHIELD_POWER_PULSE_PERIOD)
# power_pin.switch_to_input()
uart = busio.UART(board.TX, board.RX, baudrate=115200)
# uart = board.UART()
global fona
fona = FONA(uart, reset_pin)
# Initialize cellular data network
global network
net = network.CELLULAR(fona, ("ting", '', ''))
while not net.is_attached:
print("Attaching to network...")
time.sleep(0.5)
print("Attached!")
time.sleep(0.5)
## END OF FUNCTION init_sms_board
def deep_sleep(secs):
fade_status(0, 0, 128, 3, 1)
# go to sleep for an hour and see if it's time to wake up from quiet mode next time
time_to_wake = time.monotonic() + secs
# set the time alarm, notice that monotonic_time here is a named argument and must be set in the function call
time_alarm = alarm.time.TimeAlarm(monotonic_time=time_to_wake)
# Exit the program, and then deep sleep until the alarm wakes us.
alarm.exit_and_deep_sleep_until_alarms(time_alarm)
## END OF FUNCTION deep_sleep
def handle_quiet_mode(new_voltage):
# return if no file
if 'quiet.txt' not in os.listdir(): return
print("QUIET MODE DETECTED - quiet.txt exists, checking voltage against battery.txt")
# Okay, we've got a quiet.txt. Sleep unless the battery is charging
# See quiet.txt.example for more info
if 'battery.txt' in os.listdir():
f = open('battery.txt',"r")
## get and parse it's data
last_voltage = f.read().splitlines()[0]
f.close()
print('QUIET MODE DATA - new voltage:{}, last_voltage:{}'.format(new_voltage, last_voltage))
if(new_voltage > float(last_voltage) + VOLT_DIFF_FOR_CHARGE):
fade_up_status(128, 128, 128, 4, 1)
print("QUIET MODE ENDED - battery is charging. Unit must be plugged in")
print("Clearing battery.txt and quiet.txt")
# Higher voltage even with some offset! We're plugged in
# and charging. Clear the quiet and return.
os.remove('battery.txt')
os.remove('quiet.txt')
return
else:
# Lower voltage, update the battery.txt file
if(new_voltage < float(last_voltage)):
# overwrite file with new lower voltages, don't write higher voltages
# we're keeping a "low water mark" here
print("QUIET MODE: overwriting battery.txt low water mark")
with open('battery.txt', "w") as bf:
bf.write('{}\n'.format(new_voltage))
flash_status(80, 0, 128, 2, 1)
print("QUIET MODE: Sleeping for "+ str(QUIET_MODE_SLEEP_LENGTH / 60) + " min")
deep_sleep(QUIET_MODE_SLEEP_LENGTH)
else:
print("QUIET MODE: writing new battery.txt")
with open('battery.txt', "w") as bf:
bf.write('{}\n'.format(new_voltage))
flash_status(80, 0, 128, 2, 1)
print("QUIET MODE: Sleeping for "+ str(QUIET_MODE_SLEEP_LENGTH / 60) + " min")
deep_sleep(QUIET_MODE_SLEEP_LENGTH)
## END OF FUNCTION handle_quiet_mode
def transmit_queue(requests):
print("Entered function: transmit_queue")
if 'queue' not in os.listdir(): return
qfiles = os.listdir('/queue/')
for qfile in qfiles:
if not qfile.startswith('1'):
print('Removing extraneous file in queue /queue/{}'.format(qfile))
os.remove('/queue/{}'.format(qfile))
if not qfile.endswith('.txt'):
print('Removing extraneous file in queue /queue/{}'.format(qfile))
os.remove('/queue/{}'.format(qfile))
## File looks legit, open it
print("opening queued file /queue/{}".format(qfile))
f = open('/queue/{}'.format(qfile),"r")
## get and parse it's data
qdata = f.read().splitlines()[0].split(',')
f.close()
## make our heatseek data object with that queued data
heatseek_data = {
"hub":"featherhub",
"cell": secrets["cell_id"],
"time": qdata[0],
"temp": qdata[1],
"humidity": qdata[2],
"sp": secrets["reading_interval"],
"cell_version": CODE_VERSION,
}
## try sending it, we already know we have a connection or we
## wouldn't get to the transmit_queue function
send_success = False
print("Sending queued data file /queue/{}".format(qfile))
response = requests.post(HEATSEEK_URL, data=heatseek_data)
if response.status_code == 200:
print("SUCCESS sending queued to Heat Seek at {}".format(time.time()))
send_success = True
os.remove('/queue/{}'.format(qfile))
else:
print("Sending queued heatseek data failed")
return False
## END OF FUNCTION - transmit_queue(requests)
def transmit_sms_queue():
print("Entered function: transmit_sms_queue")
if 'queue' not in os.listdir(): return
qfiles = os.listdir('/queue/')
heatseek_json = '{{"c":"{}","i":"{}","r":['.format(secrets["cell_id"], secrets["reading_interval"])
heatseek_json_ar = []
## Get the files in batches of 2
for qfile in qfiles[0:SMS_QUEUE_LENGTH]:
if not qfile.startswith('1'):
print('Removing extraneous file in queue /queue/{}'.format(qfile))
os.remove('/queue/{}'.format(qfile))
if not qfile.endswith('.txt'):
print('Removing extraneous file in queue /queue/{}'.format(qfile))
os.remove('/queue/{}'.format(qfile))
## File looks legit, open it
print("opening queued file /queue/{}".format(qfile))
f = open('/queue/{}'.format(qfile),"r")
## get and parse it's data
qdata = f.read().splitlines()[0].split(',')
f.close()
## make our heatseek data object with that queued data
heatseek_json_ar.append('{{"ti":"{}","te":"{}","h":"{}"}}'.format((int(qdata[0]) - 1667875724), round((float(qdata[1])),1), round(float(qdata[2]),1)))
heatseek_json += (",".join(heatseek_json_ar))
heatseek_json += ']}'
send_success = False
send_success = fona.send_sms(SMS_RELAY_NUMBER, str(heatseek_json))
if send_success:
print("SUCCESS sending queued to Heat Seek at {}".format(time.time()))
for qfile in qfiles[0:SMS_QUEUE_LENGTH]:
os.remove('/queue/{}'.format(qfile))
qfiles = os.listdir('/queue/')
if len(qfiles) > 0:
transmit_sms_queue()
# TODO: call this again recursively if we still have files
return True
else:
print("Sending queued heatseek data failed")
return False
## END OF FUNCTION - transmit_sms_queue(requests)
def clear_queued_files():
if 'queue' not in os.listdir(): return
qfiles = os.listdir('/queue/')
for qfile in qfiles:
os.remove('/queue/{}'.format(qfile))
def write_queue_file():
if 'queue' not in os.listdir():
os.mkdir('queue')
with open("/queue/{}.txt".format(time.time()), "a") as qp:
fade_status(128, 128, 0, 2, 2)
print("Couldn't send or batching SMS msgs, writing a queue file")
print('{},{},{}'.format(time.time(), ((sensor.temperature * 1.8) + 32), sensor.relative_humidity))
qp.write('{},{},{}\n'.format(time.time(), ((sensor.temperature * 1.8) + 32), sensor.relative_humidity))
qp.flush()
qp.close()
def flash_status(red=128, green=128, blue=128, flash_length=0.5, repeat=1):
for x in range(0, repeat):
pixels.fill((red, green, blue))
time.sleep(flash_length)
pixels.fill((0, 0, 0))
time.sleep(flash_length)
def flash_warning(red=128, green=0, blue=0, red2=128, green2=128, blue2=0,flash_length=0.5, repeat=4):
for x in range(0, repeat):
pixels.fill((red, green, blue))
time.sleep(flash_length)
pixels.fill((red2, green2, blue2))
time.sleep(flash_length)
pixels.fill((0, 0, 0))
def fade_status(red=0, green=0, blue=128, fade_length=2, repeat=1):
for x in range(0, repeat):
for y in range(100, 1, -1):
pixels.fill((int(pow(red, y/100)), int(pow(green, y/100)), int(pow(blue, y/100))))
time.sleep(fade_length / 100)
pixels.fill((0, 0, 0))
def fade_up_status(red=128, green=128, blue=128, fade_length=2, repeat=1):
for x in range(0, repeat):
for y in range(1, 100):
pixels.fill((int(pow(red, y/100)), int(pow(green, y/100)), int(pow(blue, y/100))))
time.sleep(fade_length / 100)
pixels.fill((0, 0, 0))
## MAIN CODE BLOCK
reading_interval = int(secrets["reading_interval"])
led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT
print("Starting up, blink slow then fast for 6 sec")
for x in range(8):
led.value = not led.value
time.sleep(0.5)
for x in range(8):
led.value = not led.value
time.sleep(0.25)
try:
print("Blinking done, now startinging the program")
## Set up the realtime clock
r = rtc.RTC()
print(f"Time at start: {r.datetime}")
if (secrets['sms_mode'] == "true"):
init_sms_board()
try:
r.datetime = time.localtime(fona.get_timestamp())
except:
flash_warning()
print(f"Time after getting fona time: {r.datetime}")
## Check on the battery
print("LC709203F simple test")
print("Make sure LiPoly battery is plugged into the board!")
battery_sensor = LC709203F(board.I2C())
print("IC version:", hex(battery_sensor.ic_version))
print("Battery: Mode: %s / %0.3f Volts / %0.1f %%" % (battery_sensor.power_mode, battery_sensor.cell_voltage, battery_sensor.cell_percent))
except Exception as e:
flash_warning()
print("\nERROR: Problem during startup.")
print('Error message: {}'.format(e))
print("Check your connections.")
print('Deep sleep for reading interval ({}) and try again'.format(reading_interval))
deep_sleep(reading_interval)
try:
i2c = board.I2C() # uses board.SCL and board.SDA
sensor = adafruit_ahtx0.AHTx0(i2c)
# If the sensor is connected, go to read only mode so we can write temperatures
print("\nSENSOR DETECTED, attempting to writing to temperatures.txt, CIRCUITPY is read-only by computer")
flash_status(0,128,0,1,1)
# storage.remount("", switch.value)
except ValueError:
print("\nNO SENSOR, not writing to temperatures.txt CIRCUITPY is writeable by computer")
flash_status(0,0,128,1,1)
except Exception as e:
flash_warning()
print("\nERROR: Problem during sensor startup.")
print('Error message: {}'.format(e))
print("Check your connections.")
print('Deep sleep for reading interval ({}) and try again'.format(reading_interval))
deep_sleep(reading_interval)
net_connected = False
if (secrets['sms_mode'] != "true"):
try:
## #########
## Internal "try" statement attempts to connect to both the tenant wifi
## and the heatseek wifi, so that in almost all cases we get a valid datetime
## on first boot and can use that to keep time during transit
try:
print("Connecting to %s"%secrets["tenant_wifi_ssid"])
wifi.radio.connect(secrets["tenant_wifi_ssid"], secrets["tenant_wifi_password"])
print("Connected to %s!"%secrets["tenant_wifi_ssid"])
print("My IP address is", wifi.radio.ipv4_address)
## Set up http request objects
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())
net_connected = True
flash_status(0,128,0,0.5,2)
except:
print("Connecting to fallback network %s"%secrets["heatseek_wifi_ssid"])
wifi.radio.connect(secrets["heatseek_wifi_ssid"], secrets["heatseek_wifi_password"])
print("Connected to %s!"%secrets["heatseek_wifi_ssid"])
print("My IP address is", wifi.radio.ipv4_address)
## Set up http request objects
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())
net_connected = True
flash_status(0,128,0,0.5,2)
## Was this a cold boot or a wake from sleep?
if not alarm.wake_alarm:
print("Cold boot.")
fade_up_status(0,128,0,3,1)
else:
print("Waking up from sleep, RTC value after deep sleep was ")
print(f"System Time: {r.datetime}")
## Always get a fresh time because this RTC will rapidly fall out
## of sync in deep sleep (it runs very fast)
print("Fetching updated time and setting realtime clock")
response = requests.get("http://worldtimeapi.org/api/timezone/America/New_York")
if response.status_code == 200:
r.datetime = time.localtime(response.json()['unixtime'])
print(f"Got new System Time From WorldTimeApi.org: {r.datetime}")
# ensure the time matches the RTC's time
print(f"setting RTC")
time.struct_time(r.datetime)
else:
print("Setting time failed")
except ConnectionError:
print("Could not connect to network.")
flash_warning()
net_connected = False
except ValueError:
print("Time response was invalid (no connection or bad data)")
flash_warning()
net_connected = False
except Exception as e:
print("An error occurred in the network connection and time setting block")
print('\nError message: {}'.format(e))
flash_warning()
net_connected = False
try:
with open("/errors.txt", "a") as fp:
print("writing to error log")
fp.write('{},{}\n'.format(time.time(), e))
fp.flush()
fp.close()
except:
print("couldn't write to error log")
## Check if the time is valid
## (greater than oct 10 2022 timestamp 1665240748), sleep if not
if(time.time() < 1665240748):
print('Time is invalid. Sleeping for ' + str(QUIET_MODE_SLEEP_LENGTH / 60) + ' minutes')
deep_sleep(QUIET_MODE_SLEEP_LENGTH)
## We have a valid time, write out temperature.txt and try to transmit
try:
with open("/temperature.txt", "a") as fp:
# do the C-to-F conversion here if you would like
print("writing to file")
print('{},{},{},{},{}'.format(time.time(), ((sensor.temperature * 1.8) + 32), sensor.relative_humidity, battery_sensor.power_mode, battery_sensor.cell_voltage))
fp.write('{},{},{},{},{}\n'.format(time.time(), ((sensor.temperature * 1.8) + 32), sensor.relative_humidity, battery_sensor.power_mode, battery_sensor.cell_voltage))
fp.flush()
fp.close()
handle_quiet_mode(battery_sensor.cell_voltage)
heatseek_data = {
"hub":"featherhub",
"cell": secrets["cell_id"],
"time": time.time(),
"temp": ((sensor.temperature * 1.8) + 32),
"humidity": sensor.relative_humidity,
"sp": secrets["reading_interval"],
"cell_version": CODE_VERSION,
}
send_success = False
if (secrets['sms_mode'] == "true"):
write_queue_file()
if 'queue' in os.listdir():
qfiles = os.listdir('/queue/')
if len(qfiles) >= SMS_QUEUE_LENGTH:
send_success = transmit_sms_queue()
elif(net_connected):
try:
response = requests.post(HEATSEEK_URL, data=heatseek_data)
if response.status_code == 200:
print("SUCCESS sending to Heat Seek at {}".format(time.time()))
send_success = True
flash_status(128,128,128, 0.5, 3)
transmit_queue(requests)
else:
print("Sending heatseek data failed")
except Exception as e:
## Something went wrong with the transmit, even though we had a connection
print("Exception when sending to heatseek")
print('\nError message: {}'.format(e))
send_success = False
if(send_success == False):
write_queue_file()
# Create an alarm that will trigger at the next reading interval seconds from now.
print('Deep sleep for reading interval ({}) until the next send'.format( reading_interval))
deep_sleep(reading_interval)
except Exception as e: # Typically when the filesystem isn't writeable...
flash_warning()
try:
with open("/errors.txt", "a") as fp:
print("writing to error log")
fp.write('{},{}\n'.format(time.time(), e))
fp.flush()
fp.close()
except:
print("couldn't write to error log")
print("\nEXCEPTION: Final Try/Exception block triggered")
print('\nError message: {}'.format(e))
print("\nnot writing temp to file, or sending to Heat Seek")
print("This is usually because sensor is not attached and the filesystem was writable by USB")
print("NOTE: This can also happen if you plug in the sensor after the USB cord or battery, without resetting")
print("If this is unexpected, be sure you reset the feather after plugging in the sensor to run boot.py again.")
print('Deep sleep for reading interval ({}) and try again'.format(reading_interval))
deep_sleep(reading_interval)