-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp32.ino
803 lines (762 loc) · 27.4 KB
/
esp32.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
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
//*******************************libraries********************************
// ESP32--------------------------
#include <WiFi.h>
#include <HTTPClient.h>
#include <SimpleTimer.h> //https://github.com/jfturcot/SimpleTimer
#include <HardwareSerial.h>
// OLED-----------------------------
#include <SPI.h>
#include <Wire.h>
#include "icons.h"
#include <Adafruit_GFX.h> //https://github.com/adafruit/Adafruit-GFX-Library
#include <Adafruit_SSD1306.h> //https://github.com/adafruit/Adafruit_SSD1306
#include <Adafruit_Fingerprint.h> //https://github.com/adafruit/Adafruit-Fingerprint-Sensor-Library
#include <ArduinoJson.h>
#include <string.h>
//************************************************************************
// Fingerprint scanner Pins
#define Finger_Rx 4 // Adjust as per your wiring
#define Finger_Tx 2 // Adjust as per your wiring
// Declaration for SSD1306 display connected using software I2C pins are(22 SCL, 21 SDA)
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
//************************************************************************
WiFiClient client;
SimpleTimer timer;
HardwareSerial mySerial(1);
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
//************************************************************************
/* Set these to your desired credentials. */
const char *ssid = "ICONNET";
const char *password = "11223344";
const char *device_token = "UeXDtOessawgPbHfrd1X6n9xY6hWbytiYnCJXwEiTgfsNDgz6Do5JRNWZLok";
//************************************************************************
String getData, Link, finger_uuid;
String URL = "http://192.168.1.5:8001/api/v1/2024/"; // computer IP or the server domain
//************************************************************************
int FingerID = 0, t1, t2, finger_id; // The Fingerprint ID from the scanner
bool device_Mode = false; // Default Mode Enrollment
bool firstConnect = false;
unsigned long previousMillis = 0;
//************************************************************************
void setup() {
Serial.begin(115200);
delay(1000);
//-----------initiate OLED display-------------
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for (;;)
; // Don't proceed, loop forever
}
// you can delet these three lines if you don't want to get the Adfruit logo appear
display.display();
delay(2000); // Pause for 2 seconds
display.clearDisplay();
//---------------------------------------------
connectToWiFi();
//---------------------------------------------
// Set the data rate for the sensor serial port
mySerial.begin(57600, SERIAL_8N1, Finger_Rx, Finger_Tx);
Serial.println("\n\nAdafruit finger detect test");
if (finger.verifyPassword()) {
Serial.println("Found fingerprint sensor!");
display.clearDisplay();
display.drawBitmap(34, 0, FinPr_valid_bits, FinPr_valid_width, FinPr_valid_height, WHITE);
display.display();
} else {
Serial.println("Did not find fingerprint sensor :(");
display.clearDisplay();
display.drawBitmap(32, 0, FinPr_failed_bits, FinPr_failed_width, FinPr_failed_height, WHITE);
display.display();
while (1) {
delay(1);
}
}
//---------------------------------------------
finger.getTemplateCount();
Serial.print("Sensor contains ");
Serial.print(finger.templateCount);
Serial.println(" templates");
Serial.println("Waiting for valid finger...");
// Timers---------------------------------------
timer.setInterval(25000L, CheckMode);
t1 = timer.setInterval(10000L, addFingerID); // Set an internal timer every 10sec to check if there a new fingerprint in the website to add it.
t2 = timer.setInterval(15000L, Delete); // Set an internal timer every 15sec to check wheater there an ID to delete in the website.
//---------------------------------------------
CheckMode();
}
//************************************************************************
void loop() {
timer.run(); // Keep the timer in the loop function in order to update the time as soon as possible
// check if there's a connection to Wi-Fi or not
if (!WiFi.isConnected()) {
if (millis() - previousMillis >= 10000) {
previousMillis = millis();
connectToWiFi(); // Retry to connect to Wi-Fi
}
}
checkFingerprintID();
delay(10);
}
//************Display the fingerprint ID state on the OLED*************
void DisplayFingerprintID() {
//Fingerprint has been detected
if (FingerID > 0) {
display.clearDisplay();
display.drawBitmap(34, 0, FinPr_valid_bits, FinPr_valid_width, FinPr_valid_height, WHITE);
display.display();
SendFingerprintID(FingerID); // Send the Fingerprint ID to the website.
delay(2000);
}
//---------------------------------------------
//No finger detected
else if (FingerID == 0) {
display.clearDisplay();
display.drawBitmap(32, 0, FinPr_start_bits, FinPr_start_width, FinPr_start_height, WHITE);
display.display();
}
//---------------------------------------------
//Didn't find a match
else if (FingerID == -1) {
display.clearDisplay();
display.drawBitmap(34, 0, FinPr_invalid_bits, FinPr_invalid_width, FinPr_invalid_height, WHITE);
display.display();
}
//---------------------------------------------
//Didn't find the scanner or there an error
else if (FingerID == -2) {
display.clearDisplay();
display.drawBitmap(32, 0, FinPr_failed_bits, FinPr_failed_width, FinPr_failed_height, WHITE);
display.display();
}
}
//********************Check Finger Print ID******************
void checkFingerprintID() {
FingerID = getFingerprintID();
DisplayFingerprintID();
}
void addFingerID() {
getFingerprintEnroll();
}
void Delete() {
deleteFingerID(finger_id);
}
//********************Send Delete Finger ID******************
void SendDeleteFingerID() {
if (WiFi.isConnected()) {
HTTPClient http;
Link = URL + "attendance" + "/" + finger_uuid;
http.begin(Link);
int httpCode = http.sendRequest("DELETE");
Serial.println(httpCode);
if (httpCode == 200) {
http.end();
timer.disable(t2);
Serial.println("Deleted Success!");
delay(1000);
}
http.end();
}
}
//********************Delete Finger ID******************
uint8_t deleteFingerID(int finger_id) {
uint8_t p = -1;
p = finger.deleteModel(finger_id);
if (p == FINGERPRINT_OK) {
display.clearDisplay();
display.setTextSize(2); // Normal 2:2 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print(F("Deleted!\n"));
display.display();
SendDeleteFingerID();
return p;
} else if (p == FINGERPRINT_PACKETRECIEVEERR) {
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print(F("Communication error!\n"));
display.display();
return p;
} else if (p == FINGERPRINT_BADLOCATION) {
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print(F("Could not delete in that location!\n"));
display.display();
return p;
} else if (p == FINGERPRINT_FLASHERR) {
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print(F("Error writing to flash!\n"));
display.display();
return p;
} else {
display.clearDisplay();
display.setTextSize(2); // Normal 2:2 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print(F("Unknown error:\n"));
display.display();
return p;
}
}
//********************Send Finger ID******************
void SendFingerprintID(int finger_id) {
Serial.println("Sending the Fingerprint ID");
if (WiFi.isConnected()) {
HTTPClient http;
Link = URL + "attendance" + "/";
http.begin(Link);
http.addHeader("Content-Type", "application/json");
http.addHeader("Authorization", "Token " + String(device_token));
String postData = "{\"finger_id\": " + String(finger_id) + "}";
int httpCode = http.POST(postData);
Serial.println(httpCode);
if (httpCode == 201) {
StaticJsonDocument<256> doc;
DeserializationError error = deserializeJson(doc, http.getString());
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.c_str());
return;
}
finger_uuid = doc["uuid"].as<String>();
Serial.println(finger_uuid);
http.end();
delay(1000);
} else {
http.end();
}
}
}
//********************Enroll New Finger Print******************
uint8_t getFingerprintEnroll() {
uint8_t p = -1;
int finger_id = 0;
//--------------------------------------------
if (WiFi.isConnected()) {
HTTPClient http;
Link = URL + "fingerid" + "/";
http.begin(Link);
http.addHeader("Content-Type", "application/json");
http.addHeader("Authorization", "Token " + String(device_token));
int httpCode = http.GET();
Serial.println(httpCode);
if (httpCode == 200) {
StaticJsonDocument<256> doc;
DeserializationError error = deserializeJson(doc, http.getString());
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.c_str());
return p;
}
finger_id = doc["finger_id"].as<int>();
Serial.println(finger_id);
http.end();
delay(1000);
} else {
http.end();
}
}
//--------------------------------------------
if (finger_id == 0) {
display.clearDisplay();
display.setTextSize(2); // Normal 2:2 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print(F("No Data"));
display.display();
return p;
}
//--------------------------------------------
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print(F("Waiting for valid finger to enroll as #"));
display.print(finger_id);
display.display();
while (p != FINGERPRINT_OK) {
p = finger.getImage();
switch (p) {
case FINGERPRINT_OK:
Serial.println("Image taken");
break;
case FINGERPRINT_NOFINGER:
Serial.println(".");
display.clearDisplay();
display.setTextSize(2); // Normal 2:2 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print(F("No finger"));
display.display();
break;
case FINGERPRINT_PACKETRECIEVEERR:
Serial.println("Communication error");
display.clearDisplay();
display.setTextSize(2); // Normal 2:2 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print(F("Com error"));
display.display();
break;
case FINGERPRINT_IMAGEFAIL:
Serial.println("Imaging error");
display.clearDisplay();
display.setTextSize(2); // Normal 2:2 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print(F("Img error"));
display.display();
break;
default:
Serial.println("Unknown error");
display.clearDisplay();
display.setTextSize(2); // Normal 2:2 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print(F("Unk error"));
display.display();
break;
}
}
// OK success!
p = finger.image2Tz(1);
switch (p) {
case FINGERPRINT_OK:
Serial.println("Image converted");
display.clearDisplay();
display.setTextSize(2); // Normal 2:2 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print(F("Img convrt"));
display.display();
break;
case FINGERPRINT_IMAGEMESS:
Serial.println("Image too messy");
display.clearDisplay();
display.setTextSize(2); // Normal 2:2 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print(F("Img messy"));
display.display();
return p;
case FINGERPRINT_PACKETRECIEVEERR:
Serial.println("Communication error");
display.clearDisplay();
display.setTextSize(2); // Normal 2:2 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print(F("Com error"));
display.display();
return p;
case FINGERPRINT_FEATUREFAIL:
Serial.println("Could not find fingerprint features");
display.clearDisplay();
display.setTextSize(2); // Normal 2:2 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print(F("Feature fail"));
display.display();
return p;
case FINGERPRINT_INVALIDIMAGE:
Serial.println("Could not find fingerprint features");
display.clearDisplay();
display.setTextSize(2); // Normal 2:2 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print(F("Invalid img"));
display.display();
return p;
default:
Serial.println("Unknown error");
display.clearDisplay();
display.setTextSize(2); // Normal 2:2 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print(F("Unk error"));
display.display();
return p;
}
Serial.println("Remove finger");
display.clearDisplay();
display.setTextSize(2); // Normal 2:2 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print(F("Remove\n finger"));
display.display();
delay(2000);
p = 0;
while (p != FINGERPRINT_NOFINGER) {
p = finger.getImage();
}
Serial.print("ID ");
Serial.println(finger_id);
p = -1;
Serial.println("Place same finger again");
display.clearDisplay();
display.setTextSize(2); // Normal 2:2 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print(F("Place same\n finger"));
display.display();
while (p != FINGERPRINT_OK) {
p = finger.getImage();
switch (p) {
case FINGERPRINT_OK:
Serial.println("Image taken");
display.clearDisplay();
display.setTextSize(2); // Normal 2:2 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print(F("Image taken"));
display.display();
break;
case FINGERPRINT_NOFINGER:
Serial.print(".");
display.clearDisplay();
display.setTextSize(2); // Normal 2:2 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print(F("No finger"));
display.display();
break;
case FINGERPRINT_PACKETRECIEVEERR:
Serial.println("Communication error");
display.clearDisplay();
display.setTextSize(2); // Normal 2:2 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print(F("Com error"));
display.display();
break;
case FINGERPRINT_IMAGEFAIL:
Serial.println("Imaging error");
display.clearDisplay();
display.setTextSize(2); // Normal 2:2 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print(F("Img error"));
display.display();
break;
default:
Serial.println("Unknown error");
display.clearDisplay();
display.setTextSize(2); // Normal 2:2 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print(F("Unk error"));
display.display();
break;
}
}
// OK success!
p = finger.image2Tz(2);
switch (p) {
case FINGERPRINT_OK:
Serial.println("Image converted");
display.clearDisplay();
display.setTextSize(2); // Normal 2:2 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print(F("Img convrt"));
display.display();
break;
case FINGERPRINT_IMAGEMESS:
Serial.println("Image too messy");
display.clearDisplay();
display.setTextSize(2); // Normal 2:2 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print(F("Img messy"));
display.display();
return p;
case FINGERPRINT_PACKETRECIEVEERR:
Serial.println("Communication error");
display.clearDisplay();
display.setTextSize(2); // Normal 2:2 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print(F("Com error"));
display.display();
return p;
case FINGERPRINT_FEATUREFAIL:
Serial.println("Could not find fingerprint features");
display.clearDisplay();
display.setTextSize(2); // Normal 2:2 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print(F("Feature fail"));
display.display();
return p;
case FINGERPRINT_INVALIDIMAGE:
Serial.println("Could not find fingerprint features");
display.clearDisplay();
display.setTextSize(2); // Normal 2:2 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print(F("Invalid img"));
display.display();
return p;
default:
Serial.println("Unknown error");
display.clearDisplay();
display.setTextSize(2); // Normal 2:2 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print(F("Unk error"));
display.display();
return p;
}
// OK converted!
Serial.print("Creating model for #");
Serial.println(finger_id);
p = finger.createModel();
if (p == FINGERPRINT_OK) {
Serial.println("Prints matched!");
display.clearDisplay();
display.setTextSize(2); // Normal 2:2 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print(F("Match\n success"));
display.display();
} else if (p == FINGERPRINT_PACKETRECIEVEERR) {
Serial.println("Communication error");
display.clearDisplay();
display.setTextSize(2); // Normal 2:2 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print(F("Com error"));
display.display();
return p;
} else if (p == FINGERPRINT_ENROLLMISMATCH) {
Serial.println("Fingerprints did not match");
display.clearDisplay();
display.setTextSize(2); // Normal 2:2 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print(F("Not match"));
display.display();
return p;
} else {
Serial.println("Unknown error");
display.clearDisplay();
display.setTextSize(2); // Normal 2:2 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print(F("Unk error"));
display.display();
return p;
}
Serial.print("ID ");
Serial.println(finger_id);
p = finger.storeModel(finger_id);
if (p == FINGERPRINT_OK) {
Serial.println("Stored!");
display.clearDisplay();
display.setTextSize(2); // Normal 2:2 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print(F("Success\n Stored"));
display.display();
} else if (p == FINGERPRINT_PACKETRECIEVEERR) {
Serial.println("Communication error");
display.clearDisplay();
display.setTextSize(2); // Normal 2:2 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print(F("Com error"));
display.display();
return p;
} else if (p == FINGERPRINT_BADLOCATION) {
Serial.println("Could not store in that location");
display.clearDisplay();
display.setTextSize(2); // Normal 2:2 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print(F("Bad loc"));
display.display();
return p;
} else if (p == FINGERPRINT_FLASHERR) {
Serial.println("Error writing to flash");
display.clearDisplay();
display.setTextSize(2); // Normal 2:2 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print(F("Flash err"));
display.display();
return p;
} else {
Serial.println("Unknown error");
display.clearDisplay();
display.setTextSize(2); // Normal 2:2 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print(F("Unk error"));
display.display();
return p;
}
//----------------------------------------
if (WiFi.isConnected()) {
HTTPClient http;
Link = URL + "fingerprint" + "/";
http.begin(Link);
http.addHeader("Content-Type", "application/json");
http.addHeader("Authorization", "Token " + String(device_token));
String postData = "{\"finger_id\": " + String(finger_id) + "}";
int httpCode = http.POST(postData);
Serial.println(httpCode);
if (httpCode == 201) {
StaticJsonDocument<256> doc;
DeserializationError error = deserializeJson(doc, http.getString());
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.c_str());
return p;
}
finger_uuid = doc["uuid"].as<String>();
Serial.println(finger_uuid);
http.end();
delay(1000);
} else {
http.end();
}
}
return finger_id;
}
//*******************************************************
void setup() {
pinMode(Red, OUTPUT);
pinMode(Green, OUTPUT);
digitalWrite(Red, HIGH);
digitalWrite(Green, LOW);
delay(1000);
Serial.begin(9600);
while (!Serial)
;
Serial.println("\n\nAdafruit finger detect test");
// set the data rate for the sensor serial port
finger.begin(57600);
if (finger.verifyPassword()) {
Serial.println("Found fingerprint sensor!");
} else {
Serial.println("Did not find fingerprint sensor :(");
while (1) {
delay(1);
}
}
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
delay(1000);
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print(F("Wait for net"));
display.display();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
display.clearDisplay();
display.setTextSize(2); // Normal 2:2 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print(F("Connected"));
display.display();
delay(1000);
}
//**********************************************
uint8_t readnumber(void) {
uint8_t num = 0;
while (num == 0) {
while (!Serial.available())
;
num = Serial.parseInt();
}
return num;
}
//**********************************************
void loop() {
int x = 0;
// display fingerprint sensor data
Serial.println("Send any character to enroll a finger print");
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print(F("Send char"));
display.display();
while (!Serial.available())
;
while (Serial.available()) {
Serial.read();
}
x = getFingerprintEnroll();
delay(2000);
display.clearDisplay();
display.setTextSize(2); // Normal 2:2 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print(F("Success"));
display.display();
delay(1000);
}
//********************connect to the WiFi******************
void connectToWiFi() {
WiFi.mode(WIFI_OFF); // Prevents reconnection issue (taking too long to connect)
delay(1000);
WiFi.mode(WIFI_STA);
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print(F("Connecting to \n"));
display.setCursor(0, 50);
display.setTextSize(2);
display.print(ssid);
display.drawBitmap(73, 10, Wifi_start_bits, Wifi_start_width, Wifi_start_height, WHITE);
display.display();
uint32_t periodToConnect = 30000L;
for (uint32_t StartToConnect = millis(); (millis() - StartToConnect) < periodToConnect;) {
if (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
} else {
break;
}
}
if (WiFi.isConnected()) {
Serial.println("");
Serial.println("Connected");
display.clearDisplay();
display.setTextSize(2); // Normal 1:1 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(8, 0); // Start at top-left corner
display.print(F("Connected \n"));
display.drawBitmap(33, 15, Wifi_connected_bits, Wifi_connected_width, Wifi_connected_height, WHITE);
display.display();
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); // IP address assigned to your ESP
} else {
Serial.println("");
Serial.println("Not Connected");
WiFi.mode(WIFI_OFF);
delay(1000);
}
delay(1000);
}