/
skeleton.py
499 lines (417 loc) · 14.4 KB
/
skeleton.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
import sensor, image, time, pyb, ujson, uos
from pyb import LED
from pyb import UART
from pyb import I2C
from time import sleep
##################################################
# Global Variables
##################################################
global pinPIR
global pinPowerSwitch
global uartObject
global arrDateTime
global pinI2C
global rtc
global totalPhotoCount
global pirFlag
#Configuration variables set to default values
global burstCount
global burstTime
global powerSaverTimeout
global triggerInterval
global name
global location
##################################################
# Helper function to convert the rtc input to
# human readable string
##################################################
def bcdDigits(chars):
for char in chars:
# char = ord(char)
for val in (char >> 4, char & 0xF):
if val==0xF:
return
return val
##################################################
#initializes camera to the correct settings
#every time it comes out of stop mode, invoke this
##################################################
def initCameraSensor():
sensor.reset() # Reset and initialize the sensor
sensor.set_pixformat(sensor.GRAYSCALE) # Set pixel format to b/w 8-bit
sensor.set_framesize(sensor.VGA) # Set frame size to 640x480
sensor.skip_frames() # Wait for settings 300ms
##################################################
#this function initializes all GPIO
#it also configures the PIR pin as an interrupt
#
# returns void
##################################################
def initGPIO():
global pinPIR
global pinPowerSwitch
pinPIR = pyb.ExtInt("P3", pyb.ExtInt.IRQ_RISING, pyb.Pin.PULL_DOWN, callback=handlePIR)
pinPowerSwitch = pyb.Pin("P6", pyb.Pin.OUT_PP)
##################################################
#this function initializes UART protocol
#
#returns false upon failure of initialization
#returns true upon success of initialization
##################################################
def initUART():
global uartObject
try:
#UART(1) uses pins 0 and 1
uartObject = UART(1)
uartObject.init(9600, bits = 8, parity = None, stop = 1, timeout_char = 1000)
except ValueError:
# print("Error: baud rate +- 5% out of range")
return False
return True
##################################################
#this function initializes I2C protocol
##################################################
def initI2C():
global pinI2C
#2 wire I2C communication, SCL = P4, SDA = P5
pinI2C = I2C(2, I2C.MASTER, baudrate=100000)
##################################################
#this function enters the microcontroller into
# low power mode
#
#returns void
##################################################
def enterLowPowerMode():
#must configure wakeup sources first
pyb.stop()
return
##################################################
#this function reads the PIR sensor from GPIO
#
#returns true upon sensing movement
#returns false upon no movement
##################################################
def readPIR():
global pinPIR
if pinPIR.value() == 1:
return True
return False
##################################################
#this function writes the time to the PIR sensor
#hard code time/date values
##################################################
def writeRTC():
global pinI2C
pinI2C.mem_write(0x00,0x68,0, timeout=1000)
pinI2C.mem_write(0x04,0x68,1, timeout=1000)
pinI2C.mem_write(0x15,0x68,2, timeout=1000)
pinI2C.mem_write(0x03,0x68,3, timeout=1000)
pinI2C.mem_write(0x17,0x68,4, timeout=1000)
pinI2C.mem_write(0x05,0x68,5, timeout=1000)
pinI2C.mem_write(0x19,0x68,6, timeout=1000)
##################################################
#this function reads the real time clock for the
#current time of day and the date and sets the
#global variable
##################################################
def readRTC():
global strDateTime
global arrDateTime
strDateTime = ""
for i in range (0,7):
#read the full data from RTC address
readFirst = (pinI2C.mem_read(1, 0x68, (6-i)))
#parse the ones and tens place of the data
readParse = (ord(readFirst) & 0x0F)
arrDateTime[i] = "" + str(bcdDigits(readFirst))+ str(readParse)
# yy-MM-dd hh-mm-ss (all numbers)
strDateTime += ("" + str(arrDateTime[6-i]) )
##################################################
#this function reads the real time clock on the
#actual board and returns a string for the time
##################################################
def readTime():
global rtc
unCleanTime = rtc.datetime()
cleanTime = [0,0,0,0,0,0]
for i in range (0,6):
if i<3:
cleanTime[i] = unCleanTime[i]
else:
cleanTime[i] = unCleanTime[i+1]
return cleanTime
##################################################
#this function reads the RFID module and will
#return a string based on the tag read or if no
#tag is read then a null string will be returned
#
#returns a string with the tag id or a null string
##################################################
def readRFID():
global uartObject
uartObject.write("RAT\r")
read = uartObject.read(60)
return read
##################################################
#this function toggles the power to the RFID
#
#returns 1 if it set it to high
#returns 0 if it set it to low
##################################################
def powerToggleRFID():
global pinPowerSwitch
if pinPowerSwitch.value() == 1:
pinPowerSwitch.low()
return 0
else:
pinPowerSwitch.high()
return 1
##################################################
#this function takes one image from the camera and
#saves it to the SD card memory in the micro
#
#returns void
##################################################
def takePhoto(filename):
ir_led = pyb.LED(4)
ir_led.on()
sensor.snapshot().save(filename)
ir_led.off()
##################################################
#this function adds a row to the CSV file based on
#the input parameters given
#
#param dateTime string for the date and time
#param tagID string for the 15 digit tag id
#param photoCount string for the number of photos
#returns false upon file edit failure
#returns true upon file edit success
##################################################
def addRowToCSV(dateTime, tagID, photoCount):
#expect datetime to be a string list
dateTime = str(dateTime).replace(",", "_")
with open('timestamps.csv', 'a') as timeFile:
timeFile.write(dateTime + "," + str(tagID) + "," + str(photoCount) + "\n")
return True
##################################################
#this function enables the PIR interrupt
##################################################
def enableInterruptPIR():
global pinPIR
pinPIR.enable()
##################################################
#this function disables the PIR interrupt
##################################################
def disableInterruptPIR():
global pinPIR
pinPIR.disable()
##################################################
#this function enables the timer interrupt
##################################################
def enableInterruptTimer( wakeTime ):
global rtc
rtc.wakeup(wakeTime, handleTimer)
##################################################
#this function disables the timer interrupt
##################################################
def disableInterruptTimer():
enableInterruptTimer(None)
##################################################
#this function enables the RTC interrupt
#
#param wakeTime milliseconds to trigger interrupt
##################################################
def enableInterruptRTC( wakeTime ):
global rtc
rtc.wakeup(wakeTime, handleRTC)
##################################################
#this function disables the RTC interrupt
##################################################
def disableInterruptRTC():
enableInterruptRTC(None)
##################################################
#this function reads the JSON file from the SD
#card and sets the values read to local variables
#
#returns false upon unsuccessful read
#returns true upon successful read
##################################################
def readJSON():
fileJSON = 'configuration.json'
global burstCount
global burstTime
global powerSaverTimeout
global triggerInterval
global name
global location
with open(fileJSON, 'r') as f:
readData = ujson.load(f)
burstCount = readData["Burst Count"]
burstTime = readData["Burst Time"]
powerSaverTimeout = readData["Power Saver Timeout"]
triggerInterval = readData["Trigger Interval"]
name = readData["Name"]
location = readData["Location"]
##################################################
#this function handles the RTC interrupt
#it takes a photo and creates/names the directory
##################################################
def handleRTC(line):
global totalPhotoCount
global photoNum
list_timestamp = str(list(timestamp))
list_timestamp = str(list_timestamp).replace(",", "_")
new_dir = "Photos/" + list_timestamp
try:
uos.stat(new_dir)
except OSError:
uos.mkdir(new_dir)
photoName = new_dir + "/" + list_timestamp + "_" + str(totalPhotoCount)
takePhoto(photoName)
totalPhotoCount += 1
##################################################
#this function handles the PIR interrupt
#it sets a flag for the while loop in the main
##################################################
def handlePIR(line):
global pirFlag
pirFlag = True
##################################################
#this function handles the timer interrupt
#it sets a flag for the while loop in the main
##################################################
def handleTimer(line):
global timerFlag
timerFlag = True
if __name__ == "__main__":
#Configuration variables set to default values
pinPIR = None
pinPowerSwitch = None
uartObject = None
arrDateTime = None
pinI2C = None
rtc = pyb.RTC()
totalPhotoCount = 0
burstCount = 3
burstTime = 1
powerSaverTimeout = 3600
triggerInterval = 1200
name = "PIT Tracker #1"
location = "Michigan"
arrDateTime = [8]*7
#initialize everything
initCameraSensor()
initGPIO()
initI2C()
initUART()
#read the time from the timer and store it on the micro
readRTC()
rtc.datetime((int("20"+arrDateTime[0]),
int(arrDateTime[1]),
int(arrDateTime[2]),
int(arrDateTime[3]),
int(arrDateTime[4]),
int(arrDateTime[5]),
int(arrDateTime[6]),
0))
#read config file and set parameters
readJSON()
#enable interrupt for GPIO
enableInterruptPIR()
#enter low power mode
timerFlag = True
pirFlag = False
green_led = pyb.LED(2)
green_led.on()
pyb.delay(50000)
green_led.off()
try:
uos.stat("Photos")
except OSError:
uos.mkdir("Photos")
try:
uos.stat("timestamps.csv")
except OSError:
timeFile = open('timestamps.csv','w')
#timeFile.write('')
timeFile.close()
enterLowPowerMode()
#this while loop just has the system wait so the
#program does not terminate while it waits for the
#interrupt to occur from the IR sensor
while(1):
#print("test1")
if timerFlag == True:
#allow the PIR to be triggered again
enableInterruptPIR()
#print("test2")
#check for trigger
if pirFlag == True:
#print("triggered")
disableInterruptPIR()
readAgain = True
tempTagID = 0
tagID = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
num_loops = 0
photoNum = 0
timestamp = readTime()
initCameraSensor()
#list_timestamp = str(list(timestamp))
#new_dir = "Photos/" + list_timestamp
#try:
#uos.stat(new_dir)
#except OSError:
#uos.mkdir(new_dir)
#takePhoto(new_dir + "/" + list_timestamp + "_" + "0")
#we have read the tag, don't care if it didn't work
#start taking photos, we can read later
powerToggleRFID()
#spend the first 3 seconds reading
while(readAgain and num_loops < 3):
TempTagID = readRFID()
if TempTagID == b'?1\r' or TempTagID == "" or TempTagID == None or TempTagID == b'\x00':
readAgain = True
else:
readAgain = False
num_loops += 1
powerToggleRFID()
enableInterruptRTC(burstTime*1000)
#make sure the tag is legit
#if it is legit, format it so it is readable
if TempTagID is not None:
if len(TempTagID) == 18:
for i in range (0,16):
tagID[i] = chr(TempTagID[i+1])
elif len(TempTagID) == 20:
for i in range (0,16):
tagID[i] = chr(TempTagID[i+3])
elif len(TempTagID) > 4:
for i in range (0,16):
tagID[i] = chr(TempTagID[i])
else:
tagID = "NoTag"
tagID = "".join(tagID)
else:
tagID = "RFID Module Error"
#print(tagID)
#print(TempTagID)
while totalPhotoCount < burstCount:
pass
totalPhotoCount = 0
#add the data we collected
addRowToCSV(timestamp, tagID, burstCount)
#cleanup and get ready to go back to low power mode
disableInterruptRTC()
pirFlag = False
#prepare the powersavertimeout timer
enableInterruptTimer(powerSaverTimeout*1000)
timerFlag = False
blue_led = pyb.LED(3)
blue_led.on()
pyb.delay(1000)
blue_led.off()
#print("testing")
TempTagID=b'x00'
tagID = b'x00'
readRFID()
enterLowPowerMode()