- RFID-Reader-Task optimized

This commit is contained in:
2025-03-18 11:54:56 +01:00
parent 9a7ba2845d
commit f2bf5e96f0
2 changed files with 131 additions and 96 deletions
+52 -21
View File
@@ -135,24 +135,49 @@ void loopNfc() {
} }
void processTag(uint8_t *uid, uint8_t uidLength, uint8_t readerNumber) { void processTag(uint8_t *uid, uint8_t uidLength, uint8_t readerNumber) {
// Create a unique identifier for this tag
String tagId = "";
for (uint8_t i = 0; i < uidLength; i++) {
if (uid[i] < 0x10) {
tagId += "0";
}
tagId += String(uid[i], HEX);
tagId += " ";
}
tagId.trim();
// Select the appropriate PN532 based on reader number
Adafruit_PN532 &pn532 = (readerNumber == 1) ? nfc1 : nfc2; Adafruit_PN532 &pn532 = (readerNumber == 1) ? nfc1 : nfc2;
// Read the tag data Serial.print("Reader "); Serial.print(readerNumber); Serial.println(" found tag:");
if (pn532.mifareclassic_AuthenticateBlock(uid, uidLength, 4, 1, keyA)) { Serial.print("UID Length: "); Serial.println(uidLength);
if (pn532.mifareclassic_ReadDataBlock(4, data)) { Serial.print("UID Value: ");
processNfcData(data, tagId); for (uint8_t i = 0; i < uidLength; i++) {
Serial.print(" 0x"); Serial.print(uid[i], HEX);
}
Serial.println("");
if (uidLength == 7) {
uint16_t tagSize = readTagSize(pn532);
if(tagSize > 0) {
Serial.print("Tag size: "); Serial.println(tagSize);
uint8_t* data = (uint8_t*)malloc(tagSize);
memset(data, 0, tagSize);
// Mehrere Leseversuche
bool readSuccess = false;
for(int attempt = 0; attempt < 3 && !readSuccess; attempt++) {
readSuccess = true;
uint8_t numPages = tagSize / 4;
for (uint8_t i = 4; i < 4 + numPages; i++) {
if (!pn532.ntag2xx_ReadPage(i, data + (i - 4) * 4)) {
Serial.print("Failed to read page "); Serial.println(i);
readSuccess = false;
break;
}
delay(5); // Kleine Pause zwischen den Seiten
}
if (!readSuccess) {
delay(50); // Pause vor erneutem Versuch
}
}
if (readSuccess) {
Serial.println("Successfully read tag data.");
processNfcData(data, createTagId(uid, uidLength));
} else {
Serial.println("Failed to read tag data after 3 attempts");
oledShowMessage("Read Error");
}
free(data);
} }
} }
} }
@@ -502,13 +527,14 @@ void scanRfidTask(void * parameter) {
uint8_t uidLength; uint8_t uidLength;
Adafruit_PN532* activeReader = nullptr; Adafruit_PN532* activeReader = nullptr;
// Try first reader // Try first reader with increased timeout
success = nfc1.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength, 500); success = nfc1.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength, 150);
if (success) { if (success) {
activeReader = &nfc1; activeReader = &nfc1;
} else { } else {
// Try second reader delay(50); // Small delay between readers
success = nfc2.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength, 500); // Try second reader with increased timeout
success = nfc2.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength, 150);
if (success) { if (success) {
activeReader = &nfc2; activeReader = &nfc2;
} }
@@ -542,7 +568,7 @@ void scanRfidTask(void * parameter) {
yield(); yield();
esp_task_wdt_reset(); esp_task_wdt_reset();
vTaskDelay(pdMS_TO_TICKS(1)); vTaskDelay(pdMS_TO_TICKS(5)); // Increased delay between page reads
} }
if (!decodeNdefAndReturnJson(data)) { if (!decodeNdefAndReturnJson(data)) {
@@ -571,6 +597,7 @@ void scanRfidTask(void * parameter) {
} }
sendNfcData(nullptr); sendNfcData(nullptr);
delay(100); // Add small delay at end of loop
} }
yield(); yield();
} }
@@ -593,3 +620,7 @@ void startNfc() {
Serial.println("RFID Task erfolgreich erstellt"); Serial.println("RFID Task erfolgreich erstellt");
} }
} }
String createTagId(uint8_t *uid, uint8_t uidLength) {
// Implementierung der Funktion
}
+4
View File
@@ -16,3 +16,7 @@ extern String nfcJsonData;
extern String spoolId; extern String spoolId;
extern volatile uint8_t hasReadRfidTag; extern volatile uint8_t hasReadRfidTag;
extern volatile bool pauseBambuMqttTask; extern volatile bool pauseBambuMqttTask;
// Function declarations
uint16_t readTagSize(Adafruit_PN532 &pn532);
String createTagId(uint8_t *uid, uint8_t uidLength);