-
Notifications
You must be signed in to change notification settings - Fork 9
/
RFIDAccessControlSingle.pde
210 lines (183 loc) · 7.08 KB
/
RFIDAccessControlSingle.pde
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
/**
* RFID Access Control Single
*
* This project implements a single stand-alone RFID access control
* system that can operate independently of a host computer or any
* other device. It uses an ID-12 RFID reader module from ID
* Innovations to scan for 125KHz "Unique" RFID tags, and when a
* recognised tag is identified it toggles an output for a configurable
* duration, typically 2 seconds. The output can then be used to
* control a relay to trip an electric striker plate to release a door
* lock.
*
* Because this project is intended to provide a minimal working system
* it does not have any provision for database updates to be managed
* externally from a host, so updates to the accepted cards must be
* made by changing the values in the code, recompiling the program,
* and re-uploading it to the Arduino. It does however report card
* readings (both successful and unsuccessful) via the serial
* connection so you can monitor the system using a connected computer.
*
* Some of this code was inspired by Tom Igoe's excellent RFID tutorial
* which is detailed on his blog at:
* http://www.tigoe.net/pcomp/code/category/PHP/347
* And also from the ID-12 example code on the Arduino Playground at:
* http://www.arduino.cc/playground/Code/ID12
*
* Copyright Jonathan Oxer <jon@oxer.com.au>
* http://www.practicalarduino.com/projects/medium/rfid-access-control
*/
// Set up the serial connection to the RFID reader module. The module's
// TX pin needs to be connected to RX (pin 2) on the Arduino. Module
// RX doesn't need to be connected to anything since we won't send
// commands to it, but SoftwareSerial requires us to define a pin for
// TX anyway so you can either connect module RX to Arduino TX or just
// leave them disconnected.
#include <SoftwareSerial.h>
#define rxPin 2
#define txPin 3
// Create a software serial object for the connection to the RFID module
SoftwareSerial rfid = SoftwareSerial( rxPin, txPin );
// Set up outputs
#define strikerPlate 12 // Output pin connected to door lock
#define ledPin 13 // LED status output
#define unlockSeconds 2 // Seconds to hold door lock open
// The tag database consists of two parts. The first part is an array of
// tag values with each tag taking up 5 bytes. The second is a list of
// names with one name for each tag (ie: group of 5 bytes).
/*byte allowedTag[10] = {
0x01, 0x04, 0xF5, 0xB5, 0x22, // Tag 1
0x04, 0x14, 0x6E, 0x8B, 0xDD, // Tag 2
};*/
char* allowedTags[] = {
"0104F5B522", // Tag 1
"04146E8BDE", // Tag 2
"0413BBBF22", // Tag 3
};
// List of names to associate with the matching tag IDs
char* tagName[] = {
"Jonathan Oxer", // Tag 1
"Michael Oxer", // Tag 2
"Dog Implant", // Tag 3
};
// Check the number of tags defined
int numberOfTags = sizeof(allowedTags)/sizeof(allowedTags[0]);
int incomingByte = 0; // To store incoming serial data
/**
* Setup
*/
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(38400); // Serial port for connection to host
rfid.begin(9600); // Serial port for connection to RFID module
Serial.println("RFID reader starting up");
}
/**
* Loop
*/
void loop() {
byte i = 0;
byte val = 0;
byte checksum = 0;
byte bytesRead = 0;
byte tempByte = 0;
byte tagBytes[6]; // "Unique" tags are only 5 bytes but we need an extra byte for the checksum
char tagValue[10];
// Read from the RFID module. Because this connection uses SoftwareSerial
// there is no equivalent to the Serial.available() function, so at this
// point the program blocks while waiting for a value from the module
if((val = rfid.read()) == 2) { // Check for header
bytesRead = 0;
while (bytesRead < 12) { // Read 10 digit code + 2 digit checksum
val = rfid.read();
// Append the first 10 bytes (0 to 9) to the raw tag value
if (bytesRead < 10)
{
tagValue[bytesRead] = val;
}
// Check if this is a header or stop byte before the 10 digit reading is complete
if((val == 0x0D)||(val == 0x0A)||(val == 0x03)||(val == 0x02)) {
break; // Stop reading
}
// Ascii/Hex conversion:
if ((val >= '0') && (val <= '9')) {
val = val - '0';
}
else if ((val >= 'A') && (val <= 'F')) {
val = 10 + val - 'A';
}
// Every two hex-digits, add a byte to the code:
if (bytesRead & 1 == 1) {
// Make space for this hex-digit by shifting the previous digit 4 bits to the left
tagBytes[bytesRead >> 1] = (val | (tempByte << 4));
if (bytesRead >> 1 != 5) { // If we're at the checksum byte,
checksum ^= tagBytes[bytesRead >> 1]; // Calculate the checksum... (XOR)
};
} else {
tempByte = val; // Store the first hex digit first
};
bytesRead++; // Ready to read next digit
}
// Send the result to the host connected via USB
if (bytesRead == 12) { // 12 digit read is complete
tagValue[10] = '\0'; // Null-terminate the string
Serial.print("Tag read: ");
for (i=0; i<5; i++) {
// Add a leading 0 to pad out values below 16
if (tagBytes[i] < 16) {
Serial.print("0");
}
Serial.print(tagBytes[i], HEX);
}
Serial.println();
Serial.print("Checksum: ");
Serial.print(tagBytes[5], HEX);
Serial.println(tagBytes[5] == checksum ? " -- passed." : " -- error.");
// Show the raw tag value
//Serial.print("VALUE: ");
//Serial.println(tagValue);
// Search the tag database for this particular tag
int tagId = findTag( tagValue );
// Only fire the striker plate if this tag was found in the database
if( tagId > 0 )
{
Serial.print("Authorized tag ID ");
Serial.print(tagId);
Serial.print(": unlocking for ");
Serial.println(tagName[tagId - 1]); // Get the name for this tag from the database
unlock(); // Fire the striker plate to open the lock
} else {
Serial.println("Tag not authorized");
}
Serial.println(); // Blank separator line in output
}
bytesRead = 0;
}
}
/**
* Fire the relay to activate the striker plate for the configured
* number of seconds.
*/
void unlock() {
digitalWrite(ledPin, HIGH);
digitalWrite(strikerPlate, HIGH);
delay(unlockSeconds * 1000);
digitalWrite(strikerPlate, LOW);
digitalWrite(ledPin, LOW);
}
/**
* Search for a specific tag in the database
*/
int findTag( char tagValue[10] ) {
for (int thisCard = 0; thisCard < numberOfTags; thisCard++) {
// Check if the tag value matches this row in the tag database
if(strcmp(tagValue, allowedTags[thisCard]) == 0)
{
// The row in the database starts at 0, so add 1 to the result so
// that the card ID starts from 1 instead (0 represents "no match")
return(thisCard + 1);
}
}
// If we don't find the tag return a tag ID of 0 to show there was no match
return(0);
}