Created
October 1, 2019 09:58
-
-
Save IsmailSan/7f343c46224c678db44928e0a1ec1bbe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if 0 | |
#include <SPI.h> | |
#include <PN532_SPI.h> | |
#include "PN532.h" | |
PN532_SPI pn532spi(SPI, 10); | |
PN532 nfc(pn532spi); | |
/* When the number after #elif set as 1, it will be switch to HSU Mode*/ | |
#elif 0 | |
#include <PN532_HSU.h> | |
#include <PN532.h> | |
PN532_HSU pn532hsu(Serial1); | |
PN532 nfc(pn532hsu); | |
/* When the number after #if & #elif set as 0, it will be switch to I2C Mode*/ | |
#else | |
#include <Wire.h> | |
#include <PN532_I2C.h> | |
#include <PN532.h> | |
#include <NfcAdapter.h> | |
PN532_I2C pn532i2c(Wire); | |
PN532 nfc(pn532i2c); | |
#endif | |
void setup(void) { | |
Serial.begin(115200); | |
Serial.println("Hello!"); | |
nfc.begin(); | |
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"); | |
} | |
void loop(void) { | |
uint8_t TagUid[]={0xCE,0xA0,0x53,0x73}; | |
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) | |
// 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 Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes"); | |
Serial.print("UID Value: "); | |
for (uint8_t i=0; i < uidLength; i++) | |
{ | |
Serial.print(" 0x");Serial.print(uid[i], HEX); | |
} | |
for (int count=0; count < uidLength; count++) | |
{ | |
if (uid[count]==TagUid[count]) | |
{ | |
if (count == 3){ Serial.println(""); | |
Serial.println("you have a matching card"); | |
delay(1000); | |
} | |
}else { | |
if (count == 3){ | |
Serial.println(""); | |
Serial.println("You have a non-matching card"); | |
delay(1000);} | |
} | |
if (count==4) return; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment