//The DIY Life
//Michael Klements
//27 January 2020
//https://www.the-diy-life.com/arduino-based-rfid-door-lock-make-your-own/#google_vignette
#include <SPI.h>
#include <RFID.h>
RFID rfid(10, 9); //D10:pin of tag reader SDA. D9:pin of tag reader RST
unsigned char status;
unsigned char str[MAX_LEN]; //MAX_LEN is 16: size of the array
String accessGranted [62] = {
"3111361314312",/*101*/
"71413141314312",/*102*/
"1021511314312",/*103*/
"6110111314312",/*104*/
"156341315312",/*105*/
"8210101314312",/*106*/
"105610612313",/*107*/
"10121381314312",/*108*/
"313921314312",/*109*/
"5101171314312",/*110*/
"1381551314312",/*111*/
"1010981314312",/*112*/
"0101171314312",/*113*/
"0310111314312",/*114*/
"12111531314312",/*115*/
"601521314312",/*116*/
"1091131314312",/*117*/
"0131401314312",/*118*/
"061311314312",/*119*/
"7710151314312",/*120*/
"1413101415903",/*121*/
"138415903",/*122*/
"12804151003",/*123*/
"141140151003",/*124*/
"151200151003",/*125*/
"112131415903",/*126*/
"151428151003",/*127*/
"15126615903",/*128*/
"0129151003",/*129*/
"120121015903",/*130*/
"770815903",/*131*/
"91011815903",/*132*/
"2761015903",/*133*/
"7812615903",/*134*/
"812310151003",/*135*/
"13158915903",/*136*/
"511141215903",/*137*/
"779815903",/*138*/
"151220151003",/*139*/
"7115815903",/*140*/
"115314151003",/*141*/
"116115903",/*142*/
"1017715803",/*143*/
"4148315903",/*144*/
"1354415803",/*145*/
"81106151003",/*146*/
"15106151003",/*147*/
"6031415903",/*148*/
"6410115803",/*149*/
"81506151003",/*150*/
"1565415803",/*151*/
"1514121015803",/*152*/
"30101115103",/*153*/
"270110104",/*154*/
"895915803",/*155*/
"11109215803",/*156*/
"12201415903",/*157*/
"289315803",/*158*/
"121301315903",/*159*/
"3121101203",/*160*/
/*STARI_PLAVI*/
"9595111529",/*30*/
"820131669"/*69*/
}; //RFID serial numbers to grant access to
int accessGrantedSize = 62; //The number of serial numbers
#define buzzer 5 //buzer zvucnik
int lock = 4; //RELAY-BRAVA-Which pin the lock will be on if using a relay or solenoid or similar
boolean locked = true;
void setup()
{
Serial.begin(9600); //Serial monitor is only required to get tag ID numbers and for troubleshooting
SPI.begin(); //Start SPI communication with reader
rfid.init(); //initialization
//Choose which lock below:
pinMode(lock, OUTPUT);
pinMode(buzzer, OUTPUT);
digitalWrite(buzzer, LOW); // Make sure led is off
Serial.println("Place card/tag near reader...");
}
void loop()
{
if (rfid.findCard(PICC_REQIDL, str) == MI_OK) //Wait for a tag to be placed near the reader
{
Serial.println("Card found");
String temp = ""; //Temporary variable to store the read RFID number
if (rfid.anticoll(str) == MI_OK) //Anti-collision detection, read tag serial number
{
Serial.print("The card's ID number is : ");
for (int i = 0; i < 4; i++) //Record and display the tag serial number
{
temp = temp + (0x0F & (str[i] >> 4));
temp = temp + (0x0F & str[i]);
}
Serial.println (temp);
checkAccess (temp); //Check if the identified tag is an allowed to open tag
}
rfid.selectTag(str); //Lock card to prevent a redundant read, removing the line will make the sketch read cards continually
}
rfid.halt();
}
void checkAccess (String temp) //Function to check if an identified tag is registered to allow access
{
boolean granted = false;
for (int i = 0; i <= (accessGrantedSize - 1); i++) //Runs through all tag ID numbers registered in the array
{
if (accessGranted[i] == temp) //If a tag is found then open/close the lock
{
Serial.println ("Access Granted");
granted = true;
open_lock();
}
}
if (granted == false) //If the tag is not found
{
Serial.println ("Access Denied");
buzz();
}
}
void open_lock() {
//Use this routine when working with Relays and Solenoids etc.
digitalWrite(lock, HIGH);
delay(3000);
digitalWrite(lock, LOW);
delay(500);
}
void buzz() {
digitalWrite(buzzer, HIGH); // Make sure red LED is off
delay(500);
digitalWrite(buzzer, LOW); // Make sure blue LED is off
delay(200);
}