-
Notifications
You must be signed in to change notification settings - Fork 0
/
KNX-FPReader.ino
319 lines (260 loc) · 7.17 KB
/
KNX-FPReader.ino
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
#include <Adafruit_Fingerprint.h>
#include <KnxTelegram.h>
#include <KnxTpUart.h>
#define SENSOR_TX 6 //GPIO Pin for RX
#define SENSOR_RX 5 //GPIO Pin for TX
#define PHYSICAL_ADDRESS "1.1.250"
#define MatchGroupAddress "0/0/0"
#define LearnGroupAddress "0/0/0"
#define DeleteGroupAddress "0/0/0"
#define LastIDGroupAddress "0/0/0"
#define LastConfidenceScoreGroupAddress "0/0/0"
#define DoorlockGroupAddress "0/0/0"
SoftwareSerial mySerial(SENSOR_TX, SENSOR_RX);
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
KnxTpUart knx(&Serial, PHYSICAL_ADDRESS);
uint8_t id = 0; //Stores the current fingerprint ID
uint8_t lastID = 0; //Stores the last matched ID
uint8_t lastConfidenceScore = 0; //Stores the last matched confidence score
boolean modeLearning = false;
boolean modeReading = true;
boolean modeDelete = false;
void setup()
{
//Serial.begin(57600);
Serial.begin(19200, SERIAL_8E1);
knx.uartReset();
knx.addListenGroupAddress(LearnGroupAddress);
knx.addListenGroupAddress(DeleteGroupAddress);
while (!Serial);
delay(100);
// set the data rate for the sensor serial port
finger.begin(57600);
delay(5);
if (finger.verifyPassword()) {
} else {
// finger.LEDcontrol(1,100,2,1); // code(1-6),speed(1-255),color(1-R/2-B/3_P),time(1-255)
while (1) {
delay(1);
}
}
}
void loop() {
if (modeReading == true && modeLearning == false) {
uint8_t result = getFingerprintID();
if (result == FINGERPRINT_OK) {
knx.groupWriteBool(MatchGroupAddress, true); //match
knx.groupWriteBool(DoorlockGroupAddress, true);
delay(500);
} else if (result == FINGERPRINT_NOTFOUND) {
knx.groupWriteBool(MatchGroupAddress, false); //match
delay(500);
} else if (result == FINGERPRINT_NOFINGER) {
} else {
}
}
delay(100); //don't need to run this at full speed.
}
uint8_t getFingerprintID() {
uint8_t p = finger.getImage();
switch (p) {
case FINGERPRINT_OK:
break;
case FINGERPRINT_NOFINGER:
return p;
case FINGERPRINT_PACKETRECIEVEERR:
return p;
case FINGERPRINT_IMAGEFAIL:
return p;
default:
return p;
}
// OK success!
p = finger.image2Tz();
switch (p) {
case FINGERPRINT_OK:
break;
case FINGERPRINT_IMAGEMESS:
return p;
case FINGERPRINT_PACKETRECIEVEERR:
return p;
case FINGERPRINT_FEATUREFAIL:
return p;
case FINGERPRINT_INVALIDIMAGE:
return p;
default:
return p;
}
// OK converted!
p = finger.fingerSearch();
if (p == FINGERPRINT_OK) {
finger.LEDcontrol(FINGERPRINT_LED_BREATHING, 150, FINGERPRINT_LED_BLUE, 1);
lastID = finger.fingerID;
lastConfidenceScore = finger.confidence;
knx.groupWrite1ByteInt(LastIDGroupAddress, lastID);
knx.groupWrite1ByteInt(LastConfidenceScoreGroupAddress, lastConfidenceScore);
return p;
} else if (p == FINGERPRINT_PACKETRECIEVEERR) {
return p;
} else if (p == FINGERPRINT_NOTFOUND) {
finger.LEDcontrol(FINGERPRINT_LED_BREATHING, 150, FINGERPRINT_LED_RED, 1);
return p;
} else {
//
return p;
}
return finger.fingerID;
}
uint8_t getFingerprintEnroll() {
int p = -1;
while (p != FINGERPRINT_OK) {
p = finger.getImage();
switch (p) {
case FINGERPRINT_OK:
finger.LEDcontrol(FINGERPRINT_LED_BREATHING, 100, FINGERPRINT_LED_BLUE, 1);
delay(1000);
break;
case FINGERPRINT_NOFINGER:
finger.LEDcontrol(FINGERPRINT_LED_BREATHING, 100, FINGERPRINT_LED_PURPLE);
delay(1000);
break;
case FINGERPRINT_PACKETRECIEVEERR:
break;
case FINGERPRINT_IMAGEFAIL:
break;
default:
break;
}
}
// OK success!
p = finger.image2Tz(1);
switch (p) {
case FINGERPRINT_OK:
break;
case FINGERPRINT_IMAGEMESS:
return p;
case FINGERPRINT_PACKETRECIEVEERR:
return p;
case FINGERPRINT_FEATUREFAIL:
return p;
case FINGERPRINT_INVALIDIMAGE:
return p;
default:
return p;
}
//Serial.println("Remove finger");
delay(1000);
p = 0;
while (p != FINGERPRINT_NOFINGER) {
p = finger.getImage();
}
p = -1;
while (p != FINGERPRINT_OK) {
p = finger.getImage();
switch (p) {
case FINGERPRINT_OK:
finger.LEDcontrol(FINGERPRINT_LED_BREATHING, 100, FINGERPRINT_LED_BLUE, 3);
break;
case FINGERPRINT_NOFINGER:
finger.LEDcontrol(FINGERPRINT_LED_BREATHING, 100, FINGERPRINT_LED_PURPLE);
delay(1000);
break;
case FINGERPRINT_PACKETRECIEVEERR:
break;
case FINGERPRINT_IMAGEFAIL:
break;
default:
break;
}
}
// OK success!
p = finger.image2Tz(2);
switch (p) {
case FINGERPRINT_OK:
break;
case FINGERPRINT_IMAGEMESS:
return p;
case FINGERPRINT_PACKETRECIEVEERR:
return p;
case FINGERPRINT_FEATUREFAIL:
return p;
case FINGERPRINT_INVALIDIMAGE:
return p;
default:
return p;
}
// OK converted!
p = finger.createModel();
if (p == FINGERPRINT_OK) {
} else if (p == FINGERPRINT_PACKETRECIEVEERR) {
return p;
} else if (p == FINGERPRINT_ENROLLMISMATCH) {
return p;
} else {
return p;
}
p = finger.storeModel(id);
if (p == FINGERPRINT_OK) {
return true;
} else if (p == FINGERPRINT_PACKETRECIEVEERR) {
return p;
} else if (p == FINGERPRINT_BADLOCATION) {
return p;
} else if (p == FINGERPRINT_FLASHERR) {
return p;
} else {
return p;
}
}
uint8_t deleteFingerprint() {
uint8_t p = -1;
p = finger.deleteModel(id);
if (p == FINGERPRINT_OK) {
finger.LEDcontrol(FINGERPRINT_LED_BREATHING, 25, FINGERPRINT_LED_RED, 3);
return true;
} else if (p == FINGERPRINT_PACKETRECIEVEERR) {
return p;
} else if (p == FINGERPRINT_BADLOCATION) {
return p;
} else if (p == FINGERPRINT_FLASHERR) {
return p;
} else {
return p;
}
}
void serialEvent() {
KnxTpUartSerialEventType eType = knx.serialEvent();
if (eType == TPUART_RESET_INDICATION) {
}
else if (eType == KNX_TELEGRAM) {
KnxTelegram* telegram = knx.getReceivedTelegram();
String target =
String(0 + telegram->getTargetMainGroup()) + "/" +
String(0 + telegram->getTargetMiddleGroup()) + "/" +
String(0 + telegram->getTargetSubGroup());
if (telegram->getCommand() == KNX_COMMAND_WRITE) {
if (target == LearnGroupAddress) {
int received_enroll_id = telegram->get1ByteIntValue();
id = received_enroll_id;
if (id > 0 && id < 128) {
while (!getFingerprintEnroll());
modeLearning = false;
modeReading = true;
modeDelete = false;
id = 0;
}
}
if (target == DeleteGroupAddress) {
int received_delete_id = telegram->get1ByteIntValue();
id = received_delete_id;
if (id > 0 && id < 128) {
while (! deleteFingerprint());
modeLearning = false;
modeReading = true;
modeDelete = false;
id = 0;
}
}
}
}
}