-
Notifications
You must be signed in to change notification settings - Fork 0
/
AccessControl1.ino
415 lines (375 loc) · 11 KB
/
AccessControl1.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
/*
-----------------------------------------------------------
RFID access control project
Joint project with James Nylen and Daniel Near based on MFRC522 library.
Linking an Arduino and RPI to our user database to provide access control.
Workflow:
Query RFID reader every minute or so and verify it's accessible. Store this in a boolean flag
If the RFID reader is not present, attempt to reset/reinitialize it. If that fails, sleep some and attempt to repeat the process later.
If an RFID tag is present, read the UID and pass it to the serial interface.
Pass occasional heartbeat status to the serial port. Include the processor ticks value. This could alert the RPI if there's processor resets or errors that need addressing.
Set status LEDs to default states (solid BLUE LED when both RPI and RFID reader are both connected, solid red if the link is down.
RPI will send commands to the RPI to set status lights
Flash red if an invalid badge is presented or flash green if a validated badge is presented. Rapid blue flash when a badge is presented but not yet validated.
Output one pin to activate a relay or MOSFET that will release the door latch.
Typical pin layout used:
-----------------------------------------------------------------------------------------
MFRC522 Arduino Arduino Arduino Arduino Arduino
Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro
Signal Pin Pin Pin Pin Pin Pin
-----------------------------------------------------------------------------------------
RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
SPI SS SDA(SS) 10 53 D10 10 10
SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
*/
#include <MFRC522.h> //library for Mifare FC522 devices
#include <SPI.h>
/*
For visualizing whats going on hardware we need some leds and to control door lock a relay and a wipe button
(or some other hardware) Used common anode led,digitalWriting HIGH turns OFF led Mind that if you are going
to use common cathode led or just seperate leds, simply comment out #define COMMON_ANODE,
*/
//#define COMMON_ANODE
#ifdef COMMON_ANODE
#define LED_ON LOW
#define LED_OFF HIGH
#else
#define LED_ON HIGH
#define LED_OFF LOW
#endif
#define redLed 7 // Set Led Pins
#define greenLed 6
#define blueLed 5
uint8_t successRead; // Variable integer to keep if we have Successful Read from Reader
#define relay 4 // Set Relay Pin
unsigned long ul_PreviousMillis = 0UL;
unsigned long ul_PreviousReaderMillis = 0UL;
unsigned long ul_WatchDog = 0UL;
unsigned long ul_Interval = 1000UL;
unsigned long ul_CheckReaderInterval = 30000UL;
byte readCard[4]; // Stores scanned ID read from RFID Module
int setDelay = 5000; //5 seconds lock delay when valid card present
// Create MFRC522 instance.
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);
char PiConnected = -1;
char MFCConnected = -1;
char blueState = 0;
char redState = 0;
char greenState = 0;
char AccessState = 1;
char watchDogState = 0;
void setup() {
// put your setup code here, to run once:
//Arduino Pin Configuration
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(blueLed, OUTPUT);
pinMode(relay, OUTPUT);
digitalWrite(relay, HIGH); // Make sure door is locked
digitalWrite(redLed, LED_OFF); // Make sure led is off
digitalWrite(greenLed, LED_OFF); // Make sure led is off
digitalWrite(blueLed, LED_OFF); // Make sure led is off
//Protocol Configuration
Serial.begin(9600); // Initialize serial communications with PC
SPI.begin(); // MFRC522 Hardware uses SPI protocol
mfrc522.PCD_Init(); // Initialize MFRC522 Hardware
//If you set Antenna Gain to Max it will increase reading distance
//mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max);
Serial.println(F("RMM Access Control v0.1")); // For debugging purposes
ShowReaderDetails(); // Show details of PCD - MFRC522 Card Reader details
ul_PreviousMillis = millis();
ul_WatchDog = millis();
}
void loop() {
// put your main code here, to run repeatedly:
unsigned long ul_CurrentMillis = millis();
if ( ul_CurrentMillis - ul_PreviousMillis > ul_Interval)
{
ul_PreviousMillis += ul_Interval;
if (AccessState >= 0)
{
if ( watchDogState >= 0)
{
toggleBlue();
}
else
{
blueOFF();
}
}
successRead = getID(); // sets successRead to 1 when we get read from reader otherwise 0
if (successRead == true)
{
blueON();
//send it to serial
//well, currently the getID() process spits data out serial.. should move that here..
}
// (!successRead); //the program will not go further while you are not getting a successful read
/* if (PiConnected==1 && MFCConnected==1)
{
if( AccessState==-1)
{
AccessState==1;
}
}
elseif (PiConnected==-1 || MFCConnected==-1)
{
AccessState=-1;
}*/
if (MFCConnected > 0)
{
AccessState = 1;
}
else
{
AccessState=-1;
}
}
handleSerial();//look for incoming data
testWatchDog();//see if watchdog is actively pinged, set RED LED as status
checkReader();
}
void checkReader();
{
unsigned long ul_CurrentMillis = millis();
if (ul_CurrentMillis - ul_PreviousReaderMillis > ul_CheckReaderInterval)
{
ul_PreviousReaderMillis += ul_CheckReaderInterval;
byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
if (v == 0x91)
{
MFCConnected = 1;
}
else if (v == 0x92)
{
MFCConnected = 1;
}
else
{
MFCConnected = -2;
}
// When 0x00 or 0xFF is returned, communication probably failed
if ((v == 0x00) || (v == 0xFF))
{
MFCConnected = -1;
Serial.println(F("WARNING: Communication failure; no response from MFRC522. Is it properly connected?"));
mfrc522.PCD_Init(); // Initialize MFRC522 Hardware (Will this restore it?)
}
}
}
void granted ( uint16_t setDelay)
{
digitalWrite(redLed, LED_OFF); // Turn off red LED
digitalWrite(greenLed, LED_ON); // Turn on green LED
digitalWrite(relay, LOW); // Unlock door!
delay(setDelay); // Hold door lock open for given seconds
digitalWrite(relay, HIGH); // Relock door
delay(1000); // Hold green LED on for a second
}
///////////////////////////////////////// Access Denied ///////////////////////////////////
void denied()
{
digitalWrite(greenLed, LED_OFF); // Make sure green LED is off
digitalWrite(redLed, LED_ON); // Turn on red LED
delay(1000);
}
///////////////////////////////////////// Get PICC's UID ///////////////////////////////////
uint8_t getID()
{
// Getting ready for Reading PICCs
if ( ! mfrc522.PICC_IsNewCardPresent()) { //If a new PICC placed to RFID reader continue
return 0;
}
if ( ! mfrc522.PICC_ReadCardSerial()) { //Since a PICC placed get Serial and continue
return 0;
}
// There are Mifare PICCs which have 4 byte or 7 byte UID care if you use 7 byte PICC
// I think we should assume every PICC as they have 4 byte UID
// Until we support 7 byte PICCs
Serial.print("UID=");
for ( uint8_t i = 0; i < 4; i++) { //
readCard[i] = mfrc522.uid.uidByte[i];
if (readCard[i] < 0x10)
{
Serial.print('0');
}
Serial.print(readCard[i], HEX);
}
Serial.println("");
mfrc522.PICC_HaltA(); // Stop reading
return 1;
}
void ShowReaderDetails() {
// Get the MFRC522 software version
byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
Serial.print(F("MFRC522 Software Version: 0x"));
Serial.print(v, HEX);
if (v == 0x91)
{
Serial.print(F(" = v1.0"));
MFCConnected = 1;
}
else if (v == 0x92)
{
Serial.print(F(" = v2.0"));
MFCConnected = 1;
}
else
{
Serial.print(F(" (unknown),probably a chinese clone?"));
MFCConnected = -2;
}
Serial.println("");
// When 0x00 or 0xFF is returned, communication probably failed
if ((v == 0x00) || (v == 0xFF)) {
MFCConnected = -1;
Serial.println(F("WARNING: Communication failure; no response from MFRC522. Is it properly connected?"));
//Serial.println(F("SYSTEM HALTED: Check connections."));
}
}
//////////////////////////////////////// Normal Mode Led ///////////////////////////////////
void normalModeOn ()
{
digitalWrite(redLed, LED_OFF); // Make sure Red LED is off
digitalWrite(greenLed, LED_OFF); // Make sure Green LED is off
digitalWrite(relay, HIGH); // Make sure Door is Locked
}
void toggleRed()
{
if (redState == 0)
{
redON();
}
else
{
redOFF();
}
}
void redON()
{
redState = 1;
digitalWrite(redLed, LED_ON);
}
void redOFF()
{
redState = 0;
digitalWrite(redLed, LED_OFF);
}
void toggleBlue()
{
if (blueState == 0)
{
blueON();
}
else
{
blueOFF();
}
}
void blueOFF()
{
blueState = 0;
digitalWrite(blueLed, LED_OFF);
}
void blueON()
{
blueState = 1;
digitalWrite(blueLed, LED_ON);
}
void toggleGreen()
{
if (greenState == 0)
{
greenON();
}
else
{
greenOFF();
}
}
void greenOFF()
{
greenState = 0;
digitalWrite(greenLed, LED_OFF);
}
void greenON()
{
greenState = 1;
digitalWrite(greenLed, LED_ON);
}
void handleSerial()
{
char inByte = 0;
if ( Serial.available() > 0)
{
//delay(1000);
inByte = Serial.read();
if (inByte == 'W') //Set/Reset watchdog timer
{
if (watchDogState != 0)
{
watchDogState = 0;
ul_WatchDog = millis();
Serial.print("S=");
Serial.println(AccessState, DEC);
//Serial.println();
}
}
if (inByte == 'w') //Set/Reset watchdog timer
{
if (watchDogState != 1 )
{
watchDogState = 1;
ul_WatchDog = millis();
Serial.print("S=");
Serial.println(AccessState, DEC);
//Serial.println();
}
}
if (inByte == 'u') //unlock door for 5 seconds
{
Serial.println("Unlocking");
digitalWrite(relay, HIGH);
for (char i = 0, i < 6, i++)
{
greenON();
delay(500)
greenOFF();
delay(500);
}
digitalWrite(relay, LOW);
Serial.println("Unlock Released!")
}
}
//greenOFF();
}
void testWatchDog()
{
unsigned long ul_CurrentMillis = millis();
if ( ul_CurrentMillis - ul_WatchDog > 300000UL)//5 minutes
{
watchDogState = -1;
ul_WatchDog = ul_CurrentMillis;
}
if ( watchDogState >= 0 && MFCConnected >= 0 && AccessState >= 0)
{
redOFF();
}
else
{
/*Serial.print("WDFail ");
Serial.print("WDS=");
Serial.print(watchDogState,DEC);
Serial.print(" MFC=");
Serial.print(MFCConnected,DEC);
Serial.print(" ACC=");
Serial.println(AccessState,DEC);
delay(1000);*/
redON();
blueOFF();
}
}