-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinger_print_authentication.ino
More file actions
531 lines (447 loc) · 13.8 KB
/
finger_print_authentication.ino
File metadata and controls
531 lines (447 loc) · 13.8 KB
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
/**
This sketch is used for biometric verification using finger scanner r30x series.
Written by Batman for Meem Enterprise.
This sketch uses a library: BMA-R30X
Git of the library:
https://github.com/ParanoidBat/BMA-R30X
*/
#include <BMA_R30X.h>
#include "RTClib.h"
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <ESP8266HTTPClient.h>
// For communication with Android client
#define ACKNOWLEDGE 0x00
#define FAILURE 0x01
#define TCP_HEADER '2'
#define CMD_NETWORK '4'
#define CMD_PASSWORD '5'
#define CMD_FINISH '9'
#define BTN_ENROLL D3
#define BTN_SETUP D4
#define BTN_CHECKIN D0
#define BTN_CHECKOUT D5
#define LED_RED D6
#define LED_GREEN D7
#define LED_BLUE D8
char* userUri = "https://bma-api-v1.herokuapp.com/user/create_from_device";
char* checkInUri = "https://bma-api-v1.herokuapp.com/attendance/";
char* checkOutUri = "https://bma-api-v1.herokuapp.com/attendance/checkout";
char body[109];
byte state = 1; // button state
WiFiClientSecure httpsClient;
HTTPClient http;
RTC_DS1307 rtc;
DateTime now;
BMA bma;
char choice = '0';
int responseCode;
uint32_t start_counter = millis();
uint8_t attendances = 0;
Attendance attendance;
void turnOnLED(String led){
if (led == "red"){
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_BLUE, LOW);
digitalWrite(LED_RED, HIGH);
}
else if (led == "green"){
digitalWrite(LED_BLUE, LOW);
digitalWrite(LED_RED, LOW);
digitalWrite(LED_GREEN, HIGH);
}
else if (led == "blue"){
digitalWrite(LED_RED, LOW);
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_BLUE, HIGH);
}
}
bool writeEEPROMAtSetup(String key, String value){
if(key == "network") {
EEPROM.put(NETWORK_LENGTH, (uint8_t)value.length());
for (int i = 2, j = 0; j < value.length(); i++, j++) EEPROM.put(i, value[j]);
}
else if(key == "password") {
uint8_t ssid_len;
EEPROM.get(NETWORK_LENGTH, ssid_len);
EEPROM.put(PASSWORD_LENGTH, (uint8_t)value.length());
for (int i = 2 + ssid_len, j = 0; j < value.length(); i++, j++ ) EEPROM.put(i, value[j]);
}
else if(key == "id") {
uint8_t ssid_len, pass_len;
EEPROM.get(NETWORK_LENGTH, ssid_len);
EEPROM.get(PASSWORD_LENGTH, pass_len);
for (int i = 2 + ssid_len + pass_len, j = 0; j < value.length(); i++, j++) EEPROM.put(i, value[j]);
// 5 bytes for organization id
bma.finger_location = 2 + ssid_len + pass_len + 5;
bma.attendance_count = 2 + ssid_len + pass_len + 5 + 2;
bma.attendance_store = 2 + ssid_len + pass_len + 5 + 2 + 1;
EEPROM.put(bma.finger_location, (uint16_t)0);
EEPROM.put(bma.attendance_count, (uint8_t)0);
}
else return false;
EEPROM.commit();
return true;
}
String generateSSID(uint8_t *mac){
String ssid = "";
for(int i = 0; i < 6; i++){
uint8_t r = mac[i];
if(r > 122){
uint8_t x = mac[i]%122;
if(x > 122) r = 122 - x;
else if(x < 48) r = 48 + x;
else r = x;
}
else if(r < 48){
if (r == 0) r = 48;
else r = 48%r + 48;
}
ssid += (char)r;
}
return ssid;
}
String generatePassword(String ssid){
String password = "00";
for(int i = 4; i < ssid.length(); i++){
password += ssid[i] + 5;
}
return password;
}
void initialSetup(){
turnOnLED("blue");
uint8_t mac[6];
WiFi.macAddress(mac);
String string_mac = String(mac[5]);
for(int i = 4; i >= 0; i--){
string_mac += ":";
string_mac += mac[i];
}
String ap_ssid = "BMA-" + generateSSID(mac);
String ap_password = generatePassword(ap_ssid);
WiFiServer server(80);
WiFiClient client;
WiFi.mode(WIFI_AP);
WiFi.softAP(ap_ssid, ap_password);
server.begin();
Serial.println("Listening on server.");
EEPROM.begin(512);
while(1){
if(!client.connected()){
client = server.available();
}
else if(client.available() > 0){
String response = client.readStringUntil('/');
if(response != "" && response[0] == TCP_HEADER){
String data = response.substring(2);
if(response[1] == CMD_FINISH){
if(writeEEPROMAtSetup("id", data)) {
client.print(ACKNOWLEDGE);
client.flush();
}
else client.print(FAILURE);
EEPROM.end();
WiFi.softAPdisconnect(true);
WiFi.mode(WIFI_STA);
WiFi.begin(BMA.ssid, BMA.password);
delay(2000);
if (WiFi.status() == WL_CONNECTED){
Serial.print("Wifi connected with ip: ");
Serial.println(WiFi.localIP());
httpsClient.connect("https://bma-api-v1.herokuapp.com/user/org_product/", 443);
http.begin(httpsClient, userUri);
http.addHeader("Content-Type", "application/json");
char org_product_body[84];
sprintf(org_product_body, "{\"org_id\":\"%s\",\"product_name\":\"FP1\",\"product_mac\":\"%s\"}", bma.organizationID.c_str(), string_mac.c_str());
responseCode = http.POST(body);
if(responseCode < 0) {
bma.displayOLED("Try Again.");
turnOnLED("red");
Serial.printf("[HTTPS] failed, error: %s\n", http.errorToString(responseCode).c_str());
}
else{
Serial.print("HTTPS Response code: ");
Serial.println(responseCode);
String payload = http.getString();
Serial.println(payload);
if(payload[2] == 'd'){
turnOnLED("green");
}
else {
bma.displayOLED("Try Again.");
turnOnLED("red");
}
}
http.end();
}
break;
}
switch(response[1]){
case CMD_NETWORK:
if(writeEEPROMAtSetup("network", data)) client.print(ACKNOWLEDGE);
else client.print(FAILURE);
break;
case CMD_PASSWORD:
if(writeEEPROMAtSetup("password", data)) client.print(ACKNOWLEDGE);
else client.print(FAILURE);
break;
default:
client.print(FAILURE);
}
}
else client.print(0x08);
}
}
}
void enroll(){
turnOnLED("blue");
uint16_t authID = bma.enrollFinger();
if(authID > 0) {
if(WiFi.status() == WL_CONNECTED){
httpsClient.connect(userUri, 443);
http.begin(httpsClient, userUri);
http.addHeader("Content-Type", "application/json");
http.addHeader("x-access-token", "bma_token_03");
sprintf(body, "{\"name\":\"dummy\",\"finger_id\":%d,\"organization_id\":\"%s\"}", authID, bma.organizationID.c_str());
responseCode = http.POST(body);
if(responseCode < 0) {
bma.displayOLED("Try Again.");
turnOnLED("red");
Serial.printf("[HTTPS] failed, error: %s\n", http.errorToString(responseCode).c_str());
}
else{
Serial.print("HTTPS Response code: ");
Serial.println(responseCode);
String payload = http.getString();
Serial.println(payload);
if(payload[2] == 'd'){
turnOnLED("green");
sprintf(body, "Finger ID: %d", authID);
bma.displayOLED(body);
}
else {
bma.displayOLED("Try Again.");
turnOnLED("red");
}
}
http.end();
}
else {
turnOnLED("red");
bma.displayOLED("No Network..");
}
}
else {
turnOnLED("red");
bma.displayOLED("Try Again.");
}
}
void check_in(){
turnOnLED("blue");
if(bma.fingerSearch()) {
uint16_t authID = bma.rx_data[bma.rx_data_length-2] + bma.rx_data[bma.rx_data_length-1];
Serial.print("\nauthID: "); Serial.println(authID);
now = rtc.now();
if(WiFi.status() == WL_CONNECTED){
Serial.println(F("sending"));
sprintf(body, "{\"date\":\"%d-%d-%d\",\"check_in\":\"%d:%d:%d\",\"finger_id\":%d,\"organization_id\":\"%s\"}",
now.year(), now.month(), now.day(),
now.twelveHour(), now.minute(), now.second(),
authID, bma.organizationID.c_str());
httpsClient.connect(checkInUri, 443);
http.begin(httpsClient, checkInUri);
http.addHeader("Content-Type", "application/json");
http.addHeader("x-access-token", "bma_token_03");
responseCode = http.POST(body);
if(responseCode < 0) {
turnOnLED("red");
Serial.printf("[HTTPS] failed, error: %s\n", http.errorToString(responseCode).c_str());
}
else{
Serial.print("HTTPS Response code: ");
Serial.println(responseCode);
String payload = http.getString();
Serial.println(payload);
if(payload[2] == 'd'){
bma.displayOLED("Successfull");
turnOnLED("green");
}
else {
turnOnLED("red");
bma.displayOLED("Try Again");
}
}
http.end();
}
else{
Attendance att{
authID,
now.year(),
now.month(),
now.day(),
now.twelveHour(),
now.minute(),
now.second()
};
EEPROM.begin(512);
uint8_t count = -1;
EEPROM.get(bma.attendance_count, count);
if(count > -1){
int location = bma.attendance_store + count * 9;
// TODO: check if there is capacity before putting in
EEPROM.put(location, att);
EEPROM.put(bma.attendance_count, count+1);
EEPROM.commit();
EEPROM.end();
bma.displayOLED("Successfull.");
turnOnLED("green");
}
else {
turnOnLED("red");
bma.displayOLED("Try Again.");
}
}
}
else {
bma.displayOLED("No Match.");
turnOnLED("red");
}
if(bma.rx_data != NULL) {
delete [] bma.rx_data;
bma.rx_data = NULL;
bma.rx_data_length = 0;
}
}
void check_out(){
turnOnLED("blue");
if(bma.fingerSearch()) {
uint16_t authID = bma.rx_data[bma.rx_data_length-2] + bma.rx_data[bma.rx_data_length-1];
Serial.print("\nauthID: "); Serial.println(authID);
now = rtc.now();
if(WiFi.status() == WL_CONNECTED){
Serial.println(F("sending"));
sprintf(body, "{\"date\":\"%d-%d-%d\",\"check_out\":\"%d:%d:%d\",\"finger_id\":%d,\"organization_id\":\"%s\"}",
now.year(), now.month(), now.day(),
now.twelveHour(), now.minute(), now.second(),
authID, bma.organizationID.c_str());
httpsClient.connect(checkOutUri, 443);
http.begin(httpsClient, checkOutUri);
http.addHeader("Content-Type", "application/json");
http.addHeader("x-access-token", "bma_token_03");
responseCode = http.POST(body);
if(responseCode < 0) {
turnOnLED("red");
Serial.printf("[HTTPS] failed, error: %s\n", http.errorToString(responseCode).c_str());
}
else{
Serial.print("HTTPS Response code: ");
Serial.println(responseCode);
String payload = http.getString();
Serial.println(payload);
if(payload[2] == 'd'){
bma.displayOLED("Checked out");
turnOnLED("green");
}
else {
turnOnLED("red");
bma.displayOLED("Try Again");
}
}
http.end();
}
else{
bma.displayOLED("No connection");
}
}
void setup() {
Serial.begin(57600);
if(bma.verifyPassword()) bma.displayOLED("Password Verified");
else bma.displayOLED("Password Unverified");
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
}
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running, let's set the time!");
}
WiFi.begin(ssid, password);
delay(2000);
if(WiFi.status() == WL_CONNECTED){
Serial.print("Wifi connected with ip: ");
Serial.println(WiFi.localIP());
}
httpsClient.setInsecure();
pinMode(BTN_ENROLL, INPUT_PULLUP);
pinMode(BTN_SETUP, INPUT_PULLUP);
pinMode(BTN_CHECKIN, INPUT_PULLUP);
pinMode(BTN_CHECKOUT, INPUT_PULLUP);
pinMode(LED_RED, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_BLUE, OUTPUT);
digitalWrite(LED_RED, LOW);
digitalWrite(LED_BLUE, HIGH);
digitalWrite(LED_GREEN, LOW);
}
void loop() {
if (!digitalRead(BTN_ENROLL)) {
if(state != 3){
state = 3;
enroll();
}
}
else if (state == 3) state = 1;
if (!digitalRead(BTN_SETUP)) {
if (state != 4){
state = 4;
initialSetup();
}
}
else if (state == 4) state = 1;
if (!digitalRead(BTN_CHECKIN)) {
if (state != 0){
state = 0;
check_in();
}
}
else if (state == 0) state = 1;
if (!digitalRead(BTN_CHECKOUT)) {
if (state != 5){
state = 5;
check_out();
}
}
else if (state == 5) state = 1;
if(millis() - start_counter > 1800000){ // 30 mins
start_counter = millis();
EEPROM.begin(512);
EEPROM.get(bma.attendance_count, attendances);
if(attendances > 0 && WiFi.status() == WL_CONNECTED){
bma.displayOLED("Uploading Data..");
httpsClient.connect(checkInUri, 443);
http.begin(httpsClient, checkInUri);
http.addHeader("Content-Type", "application/json");
while(attendances > 0){
EEPROM.get((bma.attendance_store + (attendances -1) * 9), attendance); // implemented as stack
sprintf(body, "{\"date\":\"%d/%d/%d\",\"check_in\":\"%d:%d:%d\",\"finger_id\":%d,\"organizationI_id\":\"%s\"}",
attendance.current_year, attendance.current_month, attendance.current_date,
attendance.current_hour, attendance.current_minute, attendance.current_second,
attendance.authID, bma.organizationID.c_str());
responseCode = http.POST(body);
if(responseCode < 0) {
Serial.printf("[HTTPS] failed, error: %s\n", http.errorToString(responseCode).c_str());
break;
}
if(WiFi.status() != WL_CONNECTED) {
Serial.println("Wifi disconnected");
bma.displayOLED("No Network");
break;
}
attendances --;
EEPROM.put(bma.attendance_count, attendances);
EEPROM.commit();
}
}
http.end();
EEPROM.end();
}
}