-
Notifications
You must be signed in to change notification settings - Fork 0
/
doorGuard.ino
343 lines (305 loc) · 7.44 KB
/
doorGuard.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
/*
Door Guardian System
Created by YWJamesLin@OpenNCU on 2017.02
*/
#include <stdlib.h>
#include <string.h>
#include <Wire.h>
#include <SPI.h>
#include <Ethernet.h>
#include <avr/wdt.h>
#include <Keypad.h>
#include <Keypad_I2C.h>
#include <LiquidCrystal_I2C.h>
#include <PN532_HSU.h>
#include <snep.h>
#include <NdefMessage.h>
#define production
#ifdef production
#include "definition_prod.h"
#else
#include "definition.h"
#endif
#define COUNT_LIMIT 2000
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
{'F', 'E', 'D', 'C'},
{'B', '3', '6', '9'},
{'A', '2', '5', '8'},
{'0', '1', '4', '7'}
};
byte rowPins[ROWS] = {4, 5, 6, 7}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {0, 1, 2, 3}; //connect to the column pinouts of the keypad
Keypad_I2C kpd (makeKeymap(keys), rowPins, colPins, ROWS, COLS, KBI2CAddr, PCF8574);
LiquidCrystal_I2C lcd (LCDI2CAddr, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
PN532_HSU pn532hsu (Serial1);
SNEP nfc (pn532hsu);
char messages[4][16] = {DOORNAME, "Input:", "Find NFC device", "Receiving code"};
char openmsg[6] = "Open!";
char wrongmsg[6] = "Wrong";
char errormsg[6] = "Error";
char conerrmsg[17] = "Connection Error";
char timeoutmsg[8] = "TimeOut";
char input[17], request[50], reply[13], status[4], code[6];
int length, i, phase, time, count;
char key;
boolean quit;
EthernetClient client, logClient;
NdefMessage message;
NdefRecord tempRecord;
int messageSize, ndefSize, recordSize, payloadSize;
uint8_t ndefBuf[128];
byte tempArr[100];
void printStr (char* str) {
int i;
for (i = 0; str[i] != '\0'; ++ i) {
lcd.print (str[i]);
}
}
//init LCD with initMessage
void LCDInit (char* initMessage) {
lcd.clear ();
lcd.setCursor (0, 0);
printStr (initMessage);
lcd.setCursor (0, 1);
delay (500);
}
void LCDInit () {
lcd.clear ();
lcd.setCursor (0, 0);
delay (500);
}
// trigger the relay to open the entity
void triggerDoor (int doorN) {
digitalWrite (relayTriggerPin[doorN], HIGH);
delay (200);
digitalWrite (relayTriggerPin[doorN], LOW);
LCDInit (openmsg);
}
//get input from KeyPad
void keyInput () {
int i;
char key;
length = 0;
quit = false;
count = 0;
wdt_reset ();
while (! quit) {
++ count;
if (count == COUNT_LIMIT) {
count = 0;
wdt_reset ();
}
key = kpd.getKey();
if (key) {
switch (key) {
// back to the main
case 'F' :
length = 0;
quit = true;
break;
//Reset screen but not the existing string
case 'E' :
lcd.begin (16, 2);
lcd.clear ();
lcd.setCursor (0, 0);
printStr (messages[phase]);
lcd.setCursor (0, 1);
for (i = 0; i < length; ++ i) {
lcd.print (input[i]);
}
delay (500);
break;
//Delete (from left to right)
case 'D' :
if (length) {
for (i = 1; i < length; ++ i) {
input[i - 1] = input[i];
}
-- length;
lcd.clear ();
lcd.setCursor (0, 0);
printStr (messages[phase]);
lcd.setCursor (0, 1);
for (i = 0; i < length; ++ i) {
lcd.print (input[i]);
}
}
break;
//Clear all
case 'C' :
length = 0;
LCDInit (messages[phase]);
break;
//Backspace (delete from right to left)
case 'B' :
if (length) {
-- length;
lcd.setCursor (length, 1);
lcd.print (' ');
lcd.setCursor (length, 1);
}
break;
//Accept
case 'A' :
input[length] = '\0';
quit = true;
break;
//Input digits
default :
if (length != 16) {
input[length] = key;
lcd.print(key);
++ length;
}
delay (50);
break;
}
}
}
}
// check whether input is a number string
int checkInput (char* input, int length) {
for (int i = 0; i < length ; ++ i) {
if (input[i] < 48 || input[i] > 57) {
return 0;
}
}
return 1;
}
// send entityUUID and receive code
void beamData () {
int j;
length = 0;
count = 0;
wdt_reset ();
message = NdefMessage ();
message.addMimeMediaRecord (mimeType, entityUUID);
messageSize = message.getEncodedSize ();
message.encode (ndefBuf);
if (nfc.write (ndefBuf, messageSize, 5000) > 0) {
phase = 3;
LCDInit (messages[phase]);
for (j = 0; j < 5; ++ j) {
wdt_reset ();
ndefSize = nfc.read (ndefBuf, messageSize, 2500);
if (ndefSize > 0) {
message = NdefMessage (ndefBuf, ndefSize);
recordSize = message.getRecordCount ();
for (i = 0; i < recordSize; ++ i) {
tempRecord = message.getRecord (i);
payloadSize = tempRecord.getPayloadLength ();
if (payloadSize > 0) {
tempRecord.getPayload (tempArr);
strncpy (input, (const char *)(tempArr + payloadSize - 5), 5);
if (checkInput (input, 5)) {
length = 5;
break;
}
}
}
}
if (length == 5) {
break;
}
}
}
}
//connect to the server to verify the input
void verify () {
count = 0;
wdt_reset ();
input [length] = '\0';
client.connect (byteSrv, 80);
if ( ! client.connected ()) {
LCDInit (conerrmsg);
} else {
while (client.available ()) { client.read (); }
snprintf (code, sizeof(code), "%s", input);
snprintf (request, sizeof (request), "GET %s%s HTTP/1.1", query, code);
client.println (request);
snprintf (request, sizeof (request), "Host: %s", serverStr);
client.println (request);
client.println ("Connection: close");
client.println ();
client.flush ();
delay (500);
time = 0;
while ( ! client.available () && time != 3) {
delay (250);
++ time;
}
if ( ! client.available ()) {
LCDInit (timeoutmsg);
} else {
length = 0;
while (client.available () && length != 12) {
reply[length] = client.read ();
++ length;
}
reply[length] = '\0';
if (strstr (reply, "204") != NULL) {
triggerDoor (0);
} else if (strstr (reply, "404") != NULL) {
LCDInit (wrongmsg);
} else {
LCDInit (errormsg);
}
client.stop ();
length = 0;
}
}
}
void setup () {
//initialize KeyPad, LCD and backlight, Ethernet
Wire.begin ();
kpd.begin ();
lcd.begin (16, 2);
lcd.backlight ();
Ethernet.begin (mac, byteIP, byteDNS, byteGW, byteSN);
//initalize the relay pin
for (i = 0; i < relayNum; ++ i) {
pinMode (relayTriggerPin[i], OUTPUT);
}
//start main phase
phase = 0;
LCDInit (messages[phase]);
wdt_enable (WDTO_8S);
count = 0;
}
void loop () {
++ count;
if (count == COUNT_LIMIT) {
count = 0;
wdt_reset ();
}
key = kpd.getKey ();
switch (key) {
case 'C' :
count = 0;
phase = 2;
LCDInit (messages[phase]);
beamData ();
break;
case 'F' :
count = 0;
phase = 1;
LCDInit (messages[phase]);
keyInput ();
break;
case 'E' :
lcd.begin (16, 2);
lcd.clear ();
lcd.setCursor (0, 0);
printStr (messages[phase]);
break;
}
if (key == 'C' || key == 'F') {
if (length) { verify (); }
phase = 0;
LCDInit (messages[phase]);
count = 0;
wdt_reset ();
}
}