-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.py
309 lines (239 loc) · 7.82 KB
/
demo.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
import blynklib
import RPi.GPIO as GPIO
import os
import thread
import serial
import time
import pynmea2
import string
import fingerpi as fp # fps
from time import sleep
GPIO.setmode(GPIO.BOARD)
pins = [31, 33, 35, 37] # Board pins
BLYNK_AUTH = '3UfzuSNKfdtCn01CTrZ_QpkXOeYW00MZ'
blynk = blynklib.Blynk(BLYNK_AUTH)
# initialize pins
for pin in pins:
print("Pin " + str(pin) + " initialized!")
GPIO.setup(pin, GPIO.OUT)
# Function for +V on H-bridge
def high():
GPIO.output(pins[0], True)
GPIO.output(pins[1], False)
GPIO.output(pins[2], False)
GPIO.output(pins[3], True)
blynk.virtual_write(1, 255)
# Function for -V on H-Bridge
def low():
GPIO.output(pins[0], False)
GPIO.output(pins[1], True)
GPIO.output(pins[2], True)
GPIO.output(pins[3], False)
blynk.virtual_write(1, 0)
terminal = ""
portname = "/dev/ttyACM0"
f = fp.FingerPi(port = portname)
f.CmosLed(False)
# ------------------------------------------ FPS Bullshit --------------------------------------------
def setup_FPS():
port = "/dev/ttyACM0" #USB UART interface
baudrate = 9600
device_id = 0x01
timeout = 2
if not os.path.exists(port):
raise IOError("Port " + port + "can't be opened")
ser = serial.Serial(port, baudrate, timeout=5)
ser.flushInput()
#f = fp.FingerPi()
print("Opening connection to FPS")
f.Open(extra_info = True, check_baudrate = True)
f.ChangeBaudrate(115200)
f.CmosLed(True)
def blinkFPS(count):
for i in range(count):
f.CmosLed(False)
time.sleep(0.5)
f.CmosLed(True)
def checkPress():
time.sleep(0.1)
response = f.IsPressFinger()
pressed = False
if int(response[0]['Parameter']) < 1:
print("Finger Detected!")
pressed = True
return pressed
def Enroll():
# Need three enrolls to successfully enroll a user
username = raw_input("Enter a username: \n")
# find an open enroll id
enrollid = 0
usedid = True
while usedid is True:
msg = f.CheckEnrolled(enrollid)
if msg[0]['ACK'] is True:
enrollid = enrollid + 1
else:
usedid = False
print("New ID for " + username + ": " + str(enrollid))
# turn on backlight
blinkFPS(2)
f.CmosLed(True)
print("FPS LED On")
f.EnrollStart(enrollid)
#Check if finger is on fps
count = f.GetEnrollCount()
print(count)
pressed = False # Flag to check if finger is pressed
print("First enrollment. Please place finger on the scanner.")
#delay
while pressed == False:
#print(pressed)
time.sleep(0.5)
response = f.IsPressFinger()
if int(response[0]['Parameter']) < 1:
print("Finger Detected!")
pressed = True
#implement enroll1/2/3
bret = f.CaptureFinger(True)
iret = 0
if bret != False:
f.Enroll1()
print("Remove finger.")
while pressed == True:
#print(pressed)
time.sleep(0.5)
response = f.IsPressFinger()
if int(response[0]['Parameter']) > 1:
print("Finger Removed!")
pressed = False
print("Press Finger again")
while pressed == False:
#print(pressed)
time.sleep(0.5)
response = f.IsPressFinger()
if int(response[0]['Parameter']) < 1:
print("Finger Detected!")
pressed = True
bret = f.CaptureFinger(True)
if bret != False:
f.Enroll2()
print("Remove finger")
while pressed == True:
#print(pressed)
time.sleep(0.5)
response = f.IsPressFinger()
if int(response[0]['Parameter']) > 1:
print("Finger Detected!")
pressed = False
print("Press finger again")
while pressed == False:
#print(pressed)
time.sleep(0.5)
response = f.IsPressFinger()
if int(response[0]['Parameter']) < 1:
print("Finger Detected!")
pressed = True
bret = f.CaptureFinger(True)
if bret != False:
print("Remove finger")
iret = f.Enroll3()
if iret[0]['ACK'] == True:
print("Enroll Successfull")
else:
print("Error with enrolling code:")
print(iret)
else:
print("Failed to capture 3rd finger")
else:
print("Failed to capture 2nd finger")
else:
print("Failed to capture 1st finger")
f.CmosLed(False)
return True
def Identify():
f.CmosLed(True)
print("Place Finger on FPS")
pressed = False
while pressed == False:
#print(pressed)
time.sleep(0.2)
response = f.IsPressFinger()
if int(response[0]['Parameter']) < 1:
print("Finger Detected!")
pressed = True
f.CaptureFinger(False)
msg = f.Identify()
if msg[0]['ACK'] == True:
terminal = "User detected! \t User ID: " + str(msg[0]['Parameter'])
print("User detected! \t User ID: " + str(msg[0]['Parameter']))
print("Remove Finger.")
return True
else:
print("Unauthorized User!")
return False
# ------------------------------------------------------------------------------------------------------
# register handler for virtual pin V4 write event
WRITE_EVENT_PRINT_MSG = "[WRITE_VIRTUAL_PIN_EVENT] Pin: V{} Value: '{}'"
@blynk.handle_event('write V0')
def write_virtual_pin_handler(pin, value):
if value == ['1']:
print('Button ON')
high()
else:
print('Button OFF')
low()
# Need to update it to run on a separate thread
def gps():
port = "/dev/ttyAMA0"
ser = serial.Serial(port, baudrate=9600, timeout = 1)
dataout = pynmea2.NMEAStreamReader()
newdata = ser.readline()
if newdata[0:6] == "$GPRMC":
newmsg = pynmea2.parse(newdata)
lat = newmsg.latitude
lng = newmsg.longitude
gps = "Lat = " + str(lat) + " Long = " + str(lng)
print(gps)
blynk.virtual_write(3, str('{0:.3f}'.format(lat)))
blynk.virtual_write(2, str('{0:.3f}'.format(lng)))
def main():
blynk.run()
low()
setup_FPS()
blynk.virtual_write(3, 1)
'''
if option == "y":
f.CmosLed(True)
valid = Identify()
if valid:
high()
else:
low()
else:
pass
'''
# Car just turned ON
# Blink FPS until finger is pressed
# while finger is pressed is false
valid = False
while valid is False:
finger = checkPress()
while finger is False:
finger = checkPress() # check if finger is pressed
blinkFPS()
f.CmosLed(True)
valid = Identify() # Finger on FPS and check if authorized
high() # relay switch is ON
while True:
blynk.run()
gps()
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print("Closing program")
except:
print("Other error ocurred!")
finally:
low()
GPIO.cleanup()