RFID Card Reader help

Hello all,

I'm looking for help setting up a RC522 RFID reader/writer and need some help pulling and storing card ID numbers. I'm using a MEGA2560. I'm trying to do the "simple" access granted (green LED) access denied (red LED).

I'm using the RC522 library from Miguelbalboa and the RFID_default_keys example sketch as a base, which works fine and i can read all three of my badges.

From what I've learned from reading is that I have to try and pull the CARD ID number from the dump_byte_array function and create a String. Then I compare that to an array of cards I have. Sounds simple but I cannot find a way to acquire the CARD ID and store it.

I've searched for the past three days and seem to find the same builds and sketches but not the one I'm looking for. I'm a "noob" so I may have over looked it in my search. I may be making this way more complicated then i have to but thats why I'm asking the experts.

Anywho. so from what I have figured out is that here is how they grab the data from the cards,

}

void dump_byte_array(byte *buffer, byte bufferSize) {
    for (byte i = 0; i < bufferSize; i++) {
        Serial.print(buffer[i] < 0x10 ? " 0" : " ");
        Serial.print(buffer[i], HEX);
    }
}

and here is how they write it to the serial port.

void loop() {
    // Look for new cards
    if ( ! mfrc522.PICC_IsNewCardPresent())
        return;

    // Select one of the cards
    if ( ! mfrc522.PICC_ReadCardSerial())
        return;
    // Show some details of the PICC (that is: the tag/card)
    Serial.print("Card ID:");
    dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
    Serial.println();
    Serial.print("PICC type: ");
    byte piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);

I just don't know how I can intercept the information and make it into a usable form for me.

and here is the whole sketch. I put in steve bill and bob and their CARD ID's so i don't lose them.

#include <SPI.h>
#include <MFRC522.h>


#define RST_PIN         5           // Configurable, see typical pin layout above
#define SS_PIN          53          // Configurable, see typical pin layout above
#define STEVE           C3 B0 8B F4
#define BILL            A3 3E 75 D5
#define BOB             D6 B7 E7 3D

MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.

// Number of known default keys (hard-coded)
// NOTE: Synchronize the NR_KNOWN_KEYS define with the defaultKeys[] array
#define NR_KNOWN_KEYS   8
// Known keys, see: https://code.google.com/p/mfcuk/wiki/MifareClassicDefaultKeys
byte knownKeys[NR_KNOWN_KEYS][MFRC522::MF_KEY_SIZE] =  {
    {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, // FF FF FF FF FF FF = factory default
    {0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5}, // A0 A1 A2 A3 A4 A5
    {0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5}, // B0 B1 B2 B3 B4 B5
    {0x4d, 0x3a, 0x99, 0xc3, 0x51, 0xdd}, // 4D 3A 99 C3 51 DD
    {0x1a, 0x98, 0x2c, 0x7e, 0x45, 0x9a}, // 1A 98 2C 7E 45 9A
    {0xd3, 0xf7, 0xd3, 0xf7, 0xd3, 0xf7}, // D3 F7 D3 F7 D3 F7
    {0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}, // AA BB CC DD EE FF
    {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}  // 00 00 00 00 00 00
};

/*
 * Initialize.
 */
void setup() {
      pinMode (LED1, OUTPUT);
    Serial.begin(9600);         // Initialize serial communications with the PC
    SPI.begin();                // Init SPI bus
    mfrc522.PCD_Init();         // Init MFRC522 card
    Serial.println("Try the most used default keys to print block 0 of a MIFARE PICC.");

}

/*
 * Helper routine to dump a byte array as hex values to Serial.
 */
void dump_byte_array(byte *buffer, byte bufferSize) {
    for (byte i = 0; i < bufferSize; i++) {
        Serial.print(buffer[i] < 0x10 ? " 0" : " ");
        Serial.print(buffer[i], HEX);
    }
}

/*
 * Try using the PICC (the tag/card) with the given key to access block 0.
 * On success, it will show the key details, and dump the block data on Serial.
 *
 * @return true when the given key worked, false otherwise.
 */
boolean try_key(MFRC522::MIFARE_Key *key)
{
    boolean result = false;
    byte buffer[18];
    byte block = 0;
    byte status;
    
    // Serial.println("Authenticating using key A...");
    status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, key, &(mfrc522.uid));
    if (status != MFRC522::STATUS_OK) {
        // Serial.print("PCD_Authenticate() failed: ");
        // Serial.println(mfrc522.GetStatusCodeName(status));
        return false;
    }

    // Read block
    byte byteCount = sizeof(buffer);
    status = mfrc522.MIFARE_Read(block, buffer, &byteCount);
    if (status != MFRC522::STATUS_OK) {
        // Serial.print("MIFARE_Read() failed: ");
        // Serial.println(mfrc522.GetStatusCodeName(status));
    }
    else {
        // Successful read
        result = true;
        Serial.print("Success with key:");
        dump_byte_array((*key).keyByte, MFRC522::MF_KEY_SIZE);
        Serial.println();
        // Dump block data
        Serial.print("Block "); Serial.print(block); Serial.print(":");
        dump_byte_array(buffer, 16);
        Serial.println();
        
      }
    Serial.println();

    mfrc522.PICC_HaltA();       // Halt PICC
    mfrc522.PCD_StopCrypto1();  // Stop encryption on PCD
    return result;
   
}

/*
 * Main loop.
 */
void loop() {
    // Look for new cards
    if ( ! mfrc522.PICC_IsNewCardPresent())
        return;

    // Select one of the cards
    if ( ! mfrc522.PICC_ReadCardSerial())
        return;
digitalWrite(LED1, HIGH);
    // Show some details of the PICC (that is: the tag/card)
    Serial.print("Card ID:");
    dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
    Serial.println();
    Serial.print("PICC type: ");
    byte piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
    
    
    
    
    // Try the known default keys
    MFRC522::MIFARE_Key key;
    for (byte k = 0; k < NR_KNOWN_KEYS; k++) {
        // Copy the known key into the MIFARE_Key structure
        for (byte i = 0; i < MFRC522::MF_KEY_SIZE; i++) {
            key.keyByte[i] = knownKeys[k][i];
        }
        // Try the key
        if (try_key(&key)) {
            // Found and reported on the key and block,
            // no need to try other keys for this PICC
            break;
        }

  }

}

From what I've learned from reading is that I have to try and pull the CARD ID number from the dump_byte_array function and create a String.

No, You have the card data as an array of bytes. You can compare that array to another array of bytes. Don't piss away resources on Strings.

I just don't know how I can intercept the information and make it into a usable form for me.

You don't need to "intercept" anything. The data is right there, in mfrc522.uid.uidByte. The number of elements in the uidByte array is in mfrc522.uid.size.

Hey Paul,

Thanks for your reply.

How would I use the data from the mfrc522.uid.uidByte and compare it to "approved" cards?

How would I use the data from the mfrc522.uid.uidByte and compare it to "approved" cards?

byte goodCard[xxx] = { /* xxx bytes here */ };


if(memcmp(goodCard, mfrc522.uid.uidByte, mfrc522.uid.size) == 0)
{
    // Yippee, skippy. The card is good
}

Where xxx is the number of bytes in the card data.

thank you Paul!!

got everything working so far. I still have to clean up the code and add LED's but it works! and i've learned something new. Took a little while to figure out how to write the bytes but it was right at the top.

the code

#include <SPI.h>
#include <MFRC522.h>


#define RST_PIN         5           // Configurable, see typical pin layout above
#define SS_PIN          53          // Configurable, see typical pin layout above
#define BILL            A3 3E 75 D5
#define BOB             D6 B7 E7 3D

MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.

// Number of known default keys (hard-coded)
// NOTE: Synchronize the NR_KNOWN_KEYS define with the defaultKeys[] array
#define NR_KNOWN_KEYS   8
// Known keys, see: https://code.google.com/p/mfcuk/wiki/MifareClassicDefaultKeys
byte knownKeys[NR_KNOWN_KEYS][MFRC522::MF_KEY_SIZE] =  {
    {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, // FF FF FF FF FF FF = factory default
    {0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5}, // A0 A1 A2 A3 A4 A5
    {0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5}, // B0 B1 B2 B3 B4 B5
    {0x4d, 0x3a, 0x99, 0xc3, 0x51, 0xdd}, // 4D 3A 99 C3 51 DD
    {0x1a, 0x98, 0x2c, 0x7e, 0x45, 0x9a}, // 1A 98 2C 7E 45 9A
    {0xd3, 0xf7, 0xd3, 0xf7, 0xd3, 0xf7}, // D3 F7 D3 F7 D3 F7
    {0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}, // AA BB CC DD EE FF
    {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}  // 00 00 00 00 00 00
};
byte goodCard[10]= {0xc3, 0xb0, 0x8b, 0xf4};
byte goodCard2[10]= {0xd6, 0xb7, 0xe7, 0x3d};
/*
 * Initialize.
 */
void setup() {

    Serial.begin(9600);         // Initialize serial communications with the PC
    SPI.begin();                // Init SPI bus
    mfrc522.PCD_Init();         // Init MFRC522 card
    Serial.println("Try the most used default keys to print block 0 of a MIFARE PICC.");
    pinMode(LED1, OUTPUT);
    
}

/*
 * Helper routine to dump a byte array as hex values to Serial.
 */
void dump_byte_array(byte *buffer, byte bufferSize) {
    for (byte i = 0; i < bufferSize; i++) {
        Serial.print(buffer[i] < 0x10 ? " 0" : " ");
        Serial.print(buffer[i], HEX);
    }
}

/*
 * Try using the PICC (the tag/card) with the given key to access block 0.
 * On success, it will show the key details, and dump the block data on Serial.
 *
 * @return true when the given key worked, false otherwise.
 */
boolean try_key(MFRC522::MIFARE_Key *key)
{
    boolean result = false;
    byte buffer[18];
    byte block = 0;
    byte status;
    
    // Serial.println("Authenticating using key A...");
    status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, key, &(mfrc522.uid));
    if (status != MFRC522::STATUS_OK) {
        // Serial.print("PCD_Authenticate() failed: ");
        // Serial.println(mfrc522.GetStatusCodeName(status));
        return false;
    }

    // Read block
    byte byteCount = sizeof(buffer);
    status = mfrc522.MIFARE_Read(block, buffer, &byteCount);
    if (status != MFRC522::STATUS_OK) {
        // Serial.print("MIFARE_Read() failed: ");
        // Serial.println(mfrc522.GetStatusCodeName(status));
    }
    else {
        // Successful read
        result = true;
        Serial.print("Success with key:");
        dump_byte_array((*key).keyByte, MFRC522::MF_KEY_SIZE);
        Serial.println();
        // Dump block data
        Serial.print("Block "); Serial.print(block); Serial.print(":");
        dump_byte_array(buffer, 16);
        Serial.println();
        
      }
    Serial.println();

    mfrc522.PICC_HaltA();       // Halt PICC
    mfrc522.PCD_StopCrypto1();  // Stop encryption on PCD
    return result;
   
}

/*
 * Main loop.
 */
void loop() {
    // Look for new cards
    if ( ! mfrc522.PICC_IsNewCardPresent())
        return;

    // Select one of the cards
    if ( ! mfrc522.PICC_ReadCardSerial())
        return;

    // Show some details of the PICC (that is: the tag/card)
    Serial.print("Card ID:");
    dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
    Serial.println();
    Serial.print("PICC type: ");
    byte piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);

    // Try the known default keys
    MFRC522::MIFARE_Key key;
    for (byte k = 0; k < NR_KNOWN_KEYS; k++) {
        // Copy the known key into the MIFARE_Key structure
        for (byte i = 0; i < MFRC522::MF_KEY_SIZE; i++) {
            key.keyByte[i] = knownKeys[k][i];
        }
        // Try the key
        if (try_key(&key)) {
            // Found and reported on the key and block,
            // no need to try other keys for this PICC
            break;
        }

  }
if(memcmp(goodCard, mfrc522.uid.uidByte, mfrc522.uid.size) == 0)
{
   Serial.print("GOOD");
    Serial.println();
}
    else if(memcmp(goodCard2, mfrc522.uid.uidByte, mfrc522.uid.size) == 0)
    {
    Serial.print("GOOD");
    Serial.println();
    }
    else
    {
      Serial.print("BAD");
      Serial.println();
    }
}

and the serial read out

Try the most used default keys to print block 0 of a MIFARE PICC.
Card ID: C3 B0 8B F4
PICC type: Success with key: FF FF FF FF FF FF
Block 0: C3 B0 8B F4 0C 08 04 00 69 73 73 69 35 36 35 30

GOOD
Card ID: A3 3E 75 D5
PICC type: Success with key: FF FF FF FF FF FF
Block 0: A3 3E 75 D5 3D 88 04 00 85 00 B4 2E F0 BB 6A A8

BAD
Card ID: D6 B7 E7 3D
PICC type: Success with key: FF FF FF FF FF FF
Block 0: D6 B7 E7 3D BB 98 02 00 64 8F 76 14 41 10 51 11

GOOD
1 Like

How can i get rfid uid as a veriable

dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);

want to set in a url..

How can i get rfid uid as a veriable

Just like the dump_byte_array() function does it.