-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodemuccode.ino
291 lines (269 loc) · 7.69 KB
/
nodemuccode.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
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <Adafruit_Fingerprint.h>
#include <SoftwareSerial.h>
#include <ArduinoJson.h>
//replace this with your wifi credentials
const char* ssid = "jasmitR";
const char* password = "12345678";
#if (defined(ESP8266)) && !defined(__AVR_ATmega2560_)
SoftwareSerial mySerial(14, 12); // Define software serial pins for NodeMCU
#else
#define mySerial Serial1
#endif
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
ESP8266WebServer server(80);
void setup() {
Serial.begin(115200);
delay(10);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
Serial.println(WiFi.localIP());
// Initialize fingerprint sensor
finger.begin(57600);
delay(5);
if (finger.verifyPassword()) {
Serial.println("Found fingerprint sensor!");
} else {
Serial.println("Did not find fingerprint sensor :(");
while (1) { delay(1); }
}
// Set up HTTP routes
server.on("/", HTTP_GET, handleRoot);
server.on("/enroll", HTTP_GET, handleEnroll);
server.on("/verify", HTTP_GET, handleVerify);
server.begin();
Serial.println("HTTP server started");
}
void loop() {
server.handleClient();
}
void handleRoot() {
server.send(200, "text/plain", "Hello, NodeMCU!");
}
void handleEnroll() {
String idStr = server.arg("id");
if (idStr != "") {
int id = idStr.toInt();
int result = enrollFingerprint(id);
if (result == 0) {
server.send(200, "application/json", "{\"status\":\"Enrolled\",\"id\":" + idStr + "}");
} else {
server.send(200, "application/json", "{\"status\":\"EnrollmentFailed\",\"id\":" + idStr + "}");
}
} else {
server.send(400, "application/json", "{\"error\":\"Invalid parameters\"}");
}
}
void handleVerify() {
String idStr = server.arg("id");
if (idStr != "") {
int id = idStr.toInt();
int result = verifyFingerprint(id);
if (result == 1) {
server.send(200, "application/json", "{\"status\":\"Match\",\"id\":" + idStr + "}");
} else if (result == 0) {
server.send(200, "application/json", "{\"status\":\"NoMatch\",\"id\":" + idStr + "}");
} else {
server.send(200, "application/json", "{\"status\":\"VerificationFailed\",\"id\":" + idStr + "}");
}
} else {
server.send(400, "application/json", "{\"error\":\"Invalid parameters\"}");
}
}
int enrollFingerprint(int id) {
int p = -1;
Serial.print("Waiting for valid finger to enroll as #"); Serial.println(id);
while (p != FINGERPRINT_OK) {
p = finger.getImage();
switch (p) {
case FINGERPRINT_OK:
Serial.println("Image taken");
break;
case FINGERPRINT_NOFINGER:
Serial.println(".");
break;
case FINGERPRINT_PACKETRECIEVEERR:
Serial.println("Communication error");
return -1;
case FINGERPRINT_IMAGEFAIL:
Serial.println("Imaging error");
return -1;
default:
Serial.println("Unknown error");
return -1;
}
}
p = finger.image2Tz(1);
switch (p) {
case FINGERPRINT_OK:
Serial.println("Image converted");
break;
case FINGERPRINT_IMAGEMESS:
Serial.println("Image too messy");
return -1;
case FINGERPRINT_PACKETRECIEVEERR:
Serial.println("Communication error");
return -1;
case FINGERPRINT_FEATUREFAIL:
Serial.println("Could not find fingerprint features");
return -1;
case FINGERPRINT_INVALIDIMAGE:
Serial.println("Could not find fingerprint features");
return -1;
default:
Serial.println("Unknown error");
return -1;
}
Serial.println("Remove finger");
delay(2000);
p = 0;
while (p != FINGERPRINT_NOFINGER) {
p = finger.getImage();
}
Serial.print("ID "); Serial.println(id);
p = -1;
Serial.println("Place the same finger again");
while (p != FINGERPRINT_OK) {
p = finger.getImage();
switch (p) {
case FINGERPRINT_OK:
Serial.println("Image taken");
break;
case FINGERPRINT_NOFINGER:
Serial.print(".");
break;
case FINGERPRINT_PACKETRECIEVEERR:
Serial.println("Communication error");
return -1;
case FINGERPRINT_IMAGEFAIL:
Serial.println("Imaging error");
return -1;
default:
Serial.println("Unknown error");
return -1;
}
}
p = finger.image2Tz(2);
switch (p) {
case FINGERPRINT_OK:
Serial.println("Image converted");
break;
case FINGERPRINT_IMAGEMESS:
Serial.println("Image too messy");
return -1;
case FINGERPRINT_PACKETRECIEVEERR:
Serial.println("Communication error");
return -1;
case FINGERPRINT_FEATUREFAIL:
Serial.println("Could not find fingerprint features");
return -1;
case FINGERPRINT_INVALIDIMAGE:
Serial.println("Could not find fingerprint features");
return -1;
default:
Serial.println("Unknown error");
return -1;
}
p = finger.createModel();
if (p == FINGERPRINT_OK) {
Serial.println("Prints matched!");
} else if (p == FINGERPRINT_PACKETRECIEVEERR) {
Serial.println("Communication error");
return -1;
} else if (p == FINGERPRINT_ENROLLMISMATCH) {
Serial.println("Fingerprints did not match");
return -1;
} else {
Serial.println("Unknown error");
return -1;
}
p = finger.storeModel(id);
if (p == FINGERPRINT_OK) {
Serial.println("Stored!");
return 0; // Success
} else if (p == FINGERPRINT_PACKETRECIEVEERR) {
Serial.println("Communication error");
return -1;
} else if (p == FINGERPRINT_BADLOCATION) {
Serial.println("Could not store in that location");
return -1;
} else if (p == FINGERPRINT_FLASHERR) {
Serial.println("Error writing to flash");
return -1;
} else {
Serial.println("Unknown error");
return -1;
}
}
int verifyFingerprint(int id) {
int p = -1;
Serial.print("Waiting for valid finger...");
while (p != FINGERPRINT_OK) {
p = finger.getImage();
switch (p) {
case FINGERPRINT_OK:
Serial.println("Image taken");
break;
case FINGERPRINT_NOFINGER:
Serial.println(".");
break;
case FINGERPRINT_PACKETRECIEVEERR:
Serial.println("Communication error");
return -1;
case FINGERPRINT_IMAGEFAIL:
Serial.println("Imaging error");
return -1;
default:
Serial.println("Unknown error");
return -1;
}
}
p = finger.image2Tz();
switch (p) {
case FINGERPRINT_OK:
Serial.println("Image converted");
break;
case FINGERPRINT_IMAGEMESS:
Serial.println("Image too messy");
return 0; // No match
case FINGERPRINT_PACKETRECIEVEERR:
Serial.println("Communication error");
return -1;
case FINGERPRINT_FEATUREFAIL:
Serial.println("Could not find fingerprint features");
return -1;
case FINGERPRINT_INVALIDIMAGE:
Serial.println("Could not find fingerprint features");
return -1;
default:
Serial.println("Unknown error");
return -1;
}
p = finger.fingerSearch();
if (p == FINGERPRINT_OK) {
Serial.println("Found a print match!");
if (finger.fingerID == id) {
Serial.print("Matched ID #"); Serial.println(id);
return 1; // Matched
} else {
Serial.print("Mismatched ID. Expected ID #"); Serial.println(id);
return 0; // No match
}
} else if (p == FINGERPRINT_PACKETRECIEVEERR) {
Serial.println("Communication error");
return -1;
} else if (p == FINGERPRINT_NOTFOUND) {
Serial.println("Did not find a match");
return 0; // No match
} else {
Serial.println("Unknown error");
return -1;
}
}