#include <Servo.h>
#include <SPI.h>
#include <MFRC522.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
int led1=6;
int led2=7;
#define RST_PIN 9
#define SS_PIN 10
byte readCard[4];
String MasterTag = "2717277B";
String MasterTag2 = "6772397B";// REPLACE this Tag ID with your Tag ID!!!
String tagID = "";
// Create instances
MFRC522 mfrc522(SS_PIN, RST_PIN);
void setup()
{
lcd.init();
lcd.backlight();
SPI.begin(); // SPI bus
mfrc522.PCD_Init(); // MFRC522
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
lcd.clear();
lcd.print(" Access Control ");
lcd.setCursor(0, 1);
lcd.print("Scan Your Card>>");
}
void loop()
{
MFRC522::MIFARE_Key key;
for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;
//some variables we need
byte block;
byte len;
MFRC522::StatusCode status;
Serial.begin(9600);
//Wait until new tag is available
while (getID())
{
lcd.clear();
lcd.setCursor(0, 0);
if (tagID == MasterTag)
{
lcd.print(" Access Granted!");
digitalWrite(led1,HIGH);
digitalWrite(led2,LOW);
// You can write any code here like opening doors, switching on a relay, lighting up an LED, or anything else you can think of.
}
else
{
digitalWrite(led2,HIGH);
digitalWrite(led1,LOW);
lcd.print(" Access Denied!");
}
lcd.setCursor(0, 1);
lcd.print(" ID : ");
lcd.print(tagID);
delay(2000);
digitalWrite(led2,LOW);
digitalWrite(led1,LOW);
lcd.clear();
lcd.print(" Access Control ");
lcd.setCursor(0, 1);
lcd.print("Scan Your Card>>");
}
}
//Read new tag if available
boolean getID()
{
MFRC522::MIFARE_Key key;
for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;
//some variables we need
byte block;
byte len;
MFRC522::StatusCode status;
// Getting ready for Reading PICCs
if ( ! mfrc522.PICC_IsNewCardPresent()) { //If a new PICC placed to RFID reader continue
return false;
}
if ( ! mfrc522.PICC_ReadCardSerial()) { //Since a PICC placed get Serial and continue
return false;
}
byte buffer1[18];
block = 4;
len = 18;
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 4, &key, &(mfrc522.uid)); //line 834 of MFRC522.cpp file
if (status != MFRC522::STATUS_OK) {
Serial.print(F("Authentication failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
status = mfrc522.MIFARE_Read(block, buffer1, &len);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("Reading failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
tagID = "";
for (uint8_t i = 0; i < 16; i++)
{
if (buffer1[i] != 32)
{
Serial.write(buffer1[i]);
tagID.concat(char(buffer1[i]));
}
}
Serial.print(" ");
tagID.toUpperCase();
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
return true;
}
OKAY!!! I figured something out, I managed to work out how to read the tag and display it!
Now I need to figure out how to write to it..