Skip to content

Instantly share code, notes, and snippets.

@DirtyOptics
Created October 17, 2017 11:37
Show Gist options
  • Save DirtyOptics/473ce0ecf4b6e25eeee66d64d77345a2 to your computer and use it in GitHub Desktop.
Save DirtyOptics/473ce0ecf4b6e25eeee66d64d77345a2 to your computer and use it in GitHub Desktop.
Arduino NFC/RFID Door Entry Code
#include <SPI.h>
#include <Adafruit_PN532.h>
//Declare shield IO hardware
#define strikePlate 9
#define ledPin 8
// Using hardware serial so only Chip select pin needs to be defined.
#define PN532_SCK (2)
#define PN532_MOSI (3)
#define PN532_SS (10)
#define PN532_MISO (5)
// Configure NFC shield device by creating new instance of Adafruit_PN532 called nfc
Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);
void setup() {
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
pinMode(strikePlate, OUTPUT);
digitalWrite(strikePlate, LOW);
//Open serial port for debugging
Serial.begin(115200);
Serial.print("Start");
nfc.begin();
//Shamelessly borrowed from Adafruit lib ***********************
uint32_t versiondata = nfc.getFirmwareVersion();
if (! versiondata) {
Serial.print("Didn't find PN53x board");
while (1); // halt
}
// Got ok data, print it out!
Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);
Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);
Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
// Set the max number of retry attempts to read from a card
// This prevents us from waiting forever for a card, which is
// the default behaviour of the PN532.
nfc.setPassiveActivationRetries(0xFF);
// configure board to read RFID tags
nfc.SAMConfig();
Serial.println("Waiting for an ISO14443A card");
}
//*************************************************************
uint8_t card1[] = { 0x04, 0xDD, 0xD4, 0x12, 0xFF, 0x38, 0x80 }; //card 1 Dwaynes Hand
uint8_t card2[] = { 0x93, 0xD5, 0x15, 0xC1, 0, 0, 0 }; //card 2 White Card
uint8_t card3[] = { 0x94, 0xBB, 0x17, 0xC5, 0, 0, 0 }; //card 1 belongs to dwayne's hand.
void loop() {
// put your main code here, to run repeatedly:
boolean success;
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
unsigned long readID =0;
// Wait for an ISO14443A type cards (Mifare, etc.). When one is found
// 'uid' will be populated with the UID, and uidLength will indicate
// if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);
if (success) {
Serial.println("Found a card!");
Serial.print("UID Value: ");
for (uint8_t i=0; i < uidLength; i++)
{
Serial.print(" 0x");Serial.print(uid[i], HEX);
readID += uid[i];
if(i < (uidLength -1)){
readID = readID << 8;
}
}
int i=0;
int card1matches=0;
int card2matches=0;
int card3matches=0;
for(i=0;i<7;i++)
{
if(uid[i] == card1[i])
{
card1matches++;
}
if(uid[i] == card2[i])
{
card2matches++;
}
if(uid[i] == card3[i])
{
card3matches++;
}
}
if(card1matches == 7)
{
Unlock();
}
if(card2matches == 7)
{
Unlock();
}
if(card3matches == 7)
{
Unlock();
}
delay(1000);
}
else
{
// PN532 probably timed out waiting for a card
Serial.println("Timed out waiting for a card");
}
}
void Unlock()
{
Serial.println("");
Serial.println("Unlocking!!");
digitalWrite(strikePlate, HIGH);
digitalWrite(ledPin, HIGH);
delay(2000);
digitalWrite(strikePlate, LOW);
digitalWrite(ledPin, LOW);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment