Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**************************************************************************/
- /*!
- This example will attempt to connect to an ISO14443A
- card or tag and retrieve some basic information about it
- that can be used to determine what type of card it is.
- Note that you need the baud rate to be 115200 because we need to print
- out the data and read from the card at the same time!
- To enable debug message, define DEBUG in PN532/PN532_debug.h
- */
- /**************************************************************************/
- #include <SPI.h>
- #include <PN532_SPI.h>
- #include "PN532.h"
- const int TE_PIN = PD2;
- const int BUZZER_PIN = PD3;
- // Var to store the last read uid in so we dont spam MQTT
- int lastCardUid[] = { 0, 0, 0, 0, 0, 0, 0 };
- unsigned long lastRead = 0;
- unsigned long lastReadSuccess = 0;
- // Track durations
- unsigned long buzzerAt = 0;
- int buzzerDuration = 500;
- PN532_SPI pn532spi(SPI, SS);
- PN532 nfc(pn532spi);
- void buzzerSet(int duration = 600) {
- digitalWrite(BUZZER_PIN, HIGH);
- buzzerAt = millis();
- buzzerDuration = duration;
- }
- void buzzerTimeout() {
- if ((millis() - buzzerAt) > buzzerDuration) {
- digitalWrite(BUZZER_PIN, LOW);
- }
- }
- void setup(void) {
- Serial.begin(9600);
- 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 getUidChars(uint8_t uid[], uint8_t uidLength, char* result, const unsigned result_length) {
- char part[] = {};
- for (int i = 0; i < uidLength; i++) {
- sprintf(part, "%02x", uid[i]);
- strcat(result, part);
- }
- }
- void readRFID() {
- uint8_t 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, &uidLength);
- if (success) {
- /*
- Is it the same card that was read then we skip over the rest so we dont
- to verify it below
- */
- int isSame = memcmp(lastCardUid, uid, uidLength);
- if (isSame == 0) {
- Serial.println("Same card detected, skipping");
- return;
- };
- memcpy(lastCardUid, uid, uidLength);
- lastReadSuccess = millis();
- // Display some basic information about the card
- Serial.println("Found an ISO14443A card");
- Serial.print(" UID Length: ");
- Serial.print(uidLength, DEC);
- Serial.println(" bytes");
- Serial.print(" UID Value: ");
- nfc.PrintHex(uid, uidLength);
- if (uidLength == 4) {
- // We probably have a Mifare Classic card ...
- char id[100] = {};
- int idLen = 0;
- getUidChars(uid, uidLength, id, idLen);
- //String credId = String(id);
- }
- Serial.println("");
- return;
- // In case of there wasn't a successfull attempt then we null the lastCardUid
- if ((millis() - lastReadSuccess) > 2000) {
- lastCardUid[0] = 0;
- }
- }
- }
- void loop() {
- readRFID();
- buzzerTimeout();
- }
Advertisement
Add Comment
Please, Sign In to add comment