Hello all
I am playing around with RFID for the first time... struggling a little bit. Got an RC522 set up and reading correctly.
Using the library MFRC522.h, and here is the read personal data code
My problem is this.
This routine basically sits in the main loop, waiting for a card to be presented.
I however, want to move this out to a sub-routine, so that a flag is set when a valid card is read, then it returns to the main loop. I would use the IRQ pin on the RC522.... but apparently it doesn't work (useful!)
But, it really doesn't seem to like being moved about. I get timeout errors and all sorts.
The line if (mfrc522.PICC_IsNewCardPresent() or mfrc522.PICC_ReadCardSerial()) {Read_RFID();} seems to work to branch out from the main loop upon a card being presented, but then you need to remove those commands from being repeated in the sub-routine. Otherwise it hangs.
But, then you get card timeout errors.
I have read the README file with the library and got my head around the blocks etc, but I don't understand what each of the library components do E.G. mfrc522.PICC_IsNewCardPresent() or mfrc522.GetStatusCodeName(status) etc
I wish they commented them better!
Earlier, after extensive Googling, I found another segment of code for reading cards.. but it had errors and after 2 hours, I have returned to this library.
Any suggestions or other library recommendations?
I am playing around with RFID for the first time... struggling a little bit. Got an RC522 set up and reading correctly.
Using the library MFRC522.h, and here is the read personal data code
Code:
#include <SPI.h>
#include <MFRC522.h>
constexpr uint8_t RST_PIN = 9; // Configurable, see typical pin layout above
constexpr uint8_t SS_PIN = 10; // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
//*****************************************************************************************//
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
Serial.println(F("Read personal data on a MIFARE PICC:")); //shows in serial that it is ready to read
}
//*****************************************************************************************//
void loop() {
// Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
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;
//-------------------------------------------
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
Serial.println(F("**Card Detected:**"));
//-------------------------------------------
mfrc522.PICC_DumpDetailsToSerial(&(mfrc522.uid)); //dump some details about the card
//mfrc522.PICC_DumpToSerial(&(mfrc522.uid)); //uncomment this to see all blocks in hex
//-------------------------------------------
Serial.print(F("Name: "));
byte buffer1[18];
block = 4;
len = 18;
//------------------------------------------- GET FIRST NAME
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;
}
//PRINT FIRST NAME
for (uint8_t i = 0; i < 16; i++)
{
if (buffer1[i] != 32)
{
Serial.write(buffer1[i]);
}
}
Serial.print(" ");
//---------------------------------------- GET LAST NAME
byte buffer2[18];
block = 1;
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 1, &key, &(mfrc522.uid)); //line 834
if (status != MFRC522::STATUS_OK) {
Serial.print(F("Authentication failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
status = mfrc522.MIFARE_Read(block, buffer2, &len);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("Reading failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}
//PRINT LAST NAME
for (uint8_t i = 0; i < 16; i++) {
Serial.write(buffer2[i] );
}
//----------------------------------------
Serial.println(F("\n**End Reading**\n"));
delay(1000); //change value if you want to read cards faster
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
}
//*****************************************************************************************//
My problem is this.
This routine basically sits in the main loop, waiting for a card to be presented.
I however, want to move this out to a sub-routine, so that a flag is set when a valid card is read, then it returns to the main loop. I would use the IRQ pin on the RC522.... but apparently it doesn't work (useful!)
But, it really doesn't seem to like being moved about. I get timeout errors and all sorts.
The line if (mfrc522.PICC_IsNewCardPresent() or mfrc522.PICC_ReadCardSerial()) {Read_RFID();} seems to work to branch out from the main loop upon a card being presented, but then you need to remove those commands from being repeated in the sub-routine. Otherwise it hangs.
But, then you get card timeout errors.
I have read the README file with the library and got my head around the blocks etc, but I don't understand what each of the library components do E.G. mfrc522.PICC_IsNewCardPresent() or mfrc522.GetStatusCodeName(status) etc
I wish they commented them better!
Earlier, after extensive Googling, I found another segment of code for reading cards.. but it had errors and after 2 hours, I have returned to this library.
Any suggestions or other library recommendations?