-
Notifications
You must be signed in to change notification settings - Fork 0
/
RfidEntrySystem.ino
367 lines (338 loc) · 9.95 KB
/
RfidEntrySystem.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
/*
* ----------------------------------------------------------------------------
* This sketch uses the MFRC522 library ; see https://github.com/miguelbalboa/rfid
* for further details and other examples.
*
* NOTE: The library file MFRC522.h has a lot of useful info. Please read it.
*
* This sketch show a simple locking mechanism using the RC522 RFID module.
* ----------------------------------------------------------------------------
* Typical pin layout used:
* -----------------------------------------------------------------------------------------
* MFRC522 Arduino Arduino Arduino Arduino Arduino
* Reader/PCD Uno 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 <SPI.h>
#include <MFRC522.h>
#include <LiquidCrystal_I2C.h>
#include <time.h>
#include <TimeLib.h>
#include <Ethernet.h>
#include <HttpClient.h>
#define RST_PIN 49 // Configurable, see typical pin layout above
#define SS_PIN 53 // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
String read_rfid; // Add how many you need and don't forget to include the UID.
String ok_rfid_1="f47fef20"; // This is for my main RFID Card. aka. The one I will be using to turn on my PC. Can also be used to shut it down if you want to.
String ok_rfid_2="dab93247"; // This is for the RFID Keyfob. aka. Shutdown Keyfob. Not advisable tho. Just shutdown your PC normally.
int lock = 7;// For the Card.
int mod=0;//0-standby,1-ulaz,2-sluzbeno,3-privatno,4-pauza,5-izlaz
bool triggeredByRFID=false;
//bool modExpired=false;
bool modChanged=false;
//int lock2 = 7; // For the Keyfob.
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
int taster = analogRead(A0); // pin tastera
int ostaliTasteri=analogRead(A1);
int LEDdioda = 8; // pin LED dioda
// ova promenjiva se menja pritiskom na taster
int stanjetastera = 0; //promenjiva koj
int stanjeOstalihTastera=0;
//defunct
struct tm* timeinfo;
time_t rawtime;
EthernetClient client;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 119);
int httpPort=80;
char hostName[]="192.168.1.112";
/*
* Initialize.
*/
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
//Choose which lock below:
pinMode(lock, OUTPUT);
// pinMode(lock2, OUTPUT);
pinMode(LEDdioda, OUTPUT);
pinMode(taster, INPUT);
pinMode(ostaliTasteri,INPUT);
lcd.init(); // initialize the lcd
time (&rawtime);
timeinfo = localtime (&rawtime);
Ethernet.begin(mac, ip);
// if (Ethernet.begin(mac) == 0) {
// Serial.println("Failed to obtaining an IP address using DHCP");
// Ethernet.begin(mac, ip);
// }
}
/*
* Helper routine to dump a byte array as hex values to Serial.
*/
void dump_byte_array(byte *buffer, byte bufferSize) {
read_rfid="";
for (byte i = 0; i < bufferSize; i++) {
read_rfid=read_rfid + String(buffer[i], HEX);
}
}
//0-standby,1-ulaz,2-sluzbeno,3-privatno,4-pauza,5-izlaz
String modMapIntToString(int modInt)
{
switch(modInt)
{
case 0:
return "SPREMNO!";
case 1:
return "ULAZ!";
case 2:
return "SLUZBENO!";
case 3:
return "PRIVATNO!";
case 4:
return "PAUZA!";
case 5:
return "IZLAZ!";
}
}
void open_lock(bool successful=true,int entryMod=0) {
//Use this routine when working with Relays and Solenoids etc.
lcd.clear();
if(successful)
{
digitalWrite(lock,HIGH);
digitalWrite(LEDdioda, HIGH);
if(!entryMod)
{
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Vrata Otvorena");
lcd.setCursor(4,1);
lcd.print("IZADjITE!");
delay(2000);
}
else
{
lcd.backlight();
lcd.setCursor(0,0);
lcd.print(modMapIntToString(entryMod));
lcd.setCursor(4,1);
lcd.print("USPESNO!");
delay(2000);
}
digitalWrite(LEDdioda, LOW);
digitalWrite(lock,LOW);
}
else
{
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Greska");
lcd.setCursor(0,1);
lcd.print("Neispravan ID");
delay(2000);
}
// modChanged=true;
}
void displayEntry(int entryMod)
{
// char* printTime;
// sprintf(printTime,"asdf");
if(modChanged)
lcd.clear();
if(mod)
{
lcd.backlight();
lcd.setCursor(5,0);
lcd.print(String(hour())+":"+String(minute()));
lcd.setCursor(4,1);
lcd.print(modMapIntToString(entryMod));
}
else
{
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Vrata Zatvorena");
lcd.setCursor(4,1);
lcd.print("SPREMNO!");
}
// delay(2000);
}
//bool modExpire(void *argument)
//{
// mod=0;
// lcd.clear();
// lcd.backlight();
// lcd.setCursor(0,0);
// lcd.print("Vrata Zatvorena");
// lcd.setCursor(4,1);
// lcd.print("SPREMNO!");
// modExpired=true;
// return false;
//}
//void modExpireRead()
//{
// mod=0;
// lcd.clear();
// lcd.backlight();
// lcd.setCursor(0,0);
// lcd.print("Vrata Zatvorena");
// lcd.setCursor(4,1);
// lcd.print("SPREMNO!");
// modExpired=true;
//}
void logEntryDB(bool validEntry=true)
{
String httpMethod="GET";
String pathName="/rfid/logEntry.php";
String queryString="?mode="+(validEntry ? String(mod) : String("-1"))+"&rfid="+String(read_rfid);
if(client.connect(hostName, httpPort)) {
// if connected:
Serial.println("Connected to server");
// make a HTTP request:
// send HTTP header
client.println("GET " + pathName + queryString + " HTTP/1.1");
client.println("Host: " + String(hostName));
client.println("Connection: close");
client.println(); // end HTTP header
Serial.println("Response:");
while(client.connected()) {
if(client.available()){
// read an incoming byte from the server and print it to serial monitor:
char c = client.read();
Serial.print(c);
}
}
// the server's disconnected, stop the client:
client.stop();
Serial.println();
Serial.println("disconnected");
} else {// if not connected:
Serial.println("connection failed");
}
}
void readRFID(int entryMod=0)
{
modChanged=false;//ako ne procita nece da clearuje display
if ( ! mfrc522.PICC_IsNewCardPresent())
{
triggeredByRFID=false;
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
{
triggeredByRFID=false;
return;
}
dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
// Serial.println(read_rfid,entryMod);read_rfid==ok_rfid_1 || read_rfid==ok_rfid_2
if (rfidValidate()) {
//ok, open the door.
triggeredByRFID=true;
open_lock(true,entryMod);
logEntryDB();
}
else
{
open_lock(false,entryMod);
logEntryDB(false);
}
modChanged=true;//ako procita obelezava da display treba da se clear prvo pa onda ispisuje
Serial.println(read_rfid);
}
bool readRFIDCallback(void *argument)
{
readRFID(mod);
return false;
}
bool rfidValidate()
{
bool valid=false;
String httpMethod="GET";
String pathName="/rfid/validateEntry.php";
String queryString="?rfid="+String(read_rfid);
if(client.connect(hostName, httpPort)) {
// if connected:
Serial.println("Connected to server");
// make a HTTP request:
// send HTTP header
client.println("GET " + pathName + queryString + " HTTP/1.1");
client.println("Host: " + String(hostName));
client.println("Connection: close");
client.println(); // end HTTP header
char previous='-';
Serial.println("Response:");
while(client.connected()) {
if(client.available()){
// read an incoming byte from the server and print it to serial monitor:
char c = client.read();
if(previous=='#')
{
Serial.print(c);
c=='1'? valid=true : valid=false;
}
previous=c;
}
}
// the server's disconnected, stop the client:
client.stop();
Serial.println();
Serial.println("disconnected");
} else {// if not connected:
Serial.println("connection failed");
}
return valid;
}
void loop() {
stanjetastera = analogRead(A0);
stanjeOstalihTastera=analogRead(A1);
time (&rawtime);
timeinfo = localtime (&rawtime);
// Serial.println(stanjeOstalihTastera);
delay(100);
if ( !triggeredByRFID and stanjetastera > 0 ) {
mod=0;
open_lock();
// modChanged=true;
}
//0-standby,1-ulaz,2-sluzbeno,3-privatno,4-pauza,5-izlaz
if (stanjeOstalihTastera >950 and (stanjeOstalihTastera <1100 ))
{
mod=1;
modChanged=true;
}
if (stanjeOstalihTastera >750 and (stanjeOstalihTastera <850 ))
{
mod=2;
modChanged=true;
}
if (stanjeOstalihTastera >550 and (stanjeOstalihTastera <750 ))
{
mod=3;
modChanged=true;
}
if (stanjeOstalihTastera >350 and (stanjeOstalihTastera <450 ))
{
mod=4;
modChanged=true;
}
if (stanjeOstalihTastera >150 and (stanjeOstalihTastera <250 ))
{
mod=5;
modChanged=true;
}
displayEntry(mod);
readRFID(mod);
// modChanged=false;
}