Hi,
I have the following test sketch running on a Micromod Single Board + ESP32 + Qwiic.
No other components are connected, power is over USB.
The purpose of the setup is to just detect the presence of a device, like:
- reading “00000” = no device is present (myRfid.getTag() returns 000000 when no tag was detected)
- reading “number wrong” = device (tag) is present but the wrong one (in my example, 5809122368250)
- reading “number correct” = device (tag) is present and the right one (in my example, 580912131888)
After exactly 20 readings (with the 21st) this happens (this is repeatable):
- the readings gets wrong (it reads 59102000232 instead of 580912131888)
- myRfid.clearTags() seems to not be executed any more as myRfid.getTag() delivers the same result again and again, even when there is no tag present any more
- with every additional reading, the read number changes (increases)
- if I continue reading tags, eventually a crash happens (i2c.master: I2C hardware NACK detected))
- this happens no matter if I change NFC tags or use the same over and over again
I found that in the SparkFun_Qwiic_Rfid.h file there is #define MAX_TAG_STORAGE 20 I assume this means that 20 read tags are stored and are cleared with clearTags(), so that I do not get the last read tag when calling myRfid.getTag() but get the 000000 mentioned above.
What I have tried:
- Changing MAX_TAG_STORAGE to e.g. 1 does not solve the issue.
- removing calling myRfid.getTag() when a tag was detected does not solve the issue
The issue persists even when I reboot the board once it starts to read wrong. The only thing that helps resetting the Reader is with power cycle.
I wonder if I am using the library functions wrong (I based my program on the example), do something unallowed (const char* String compare..) or if I hit a bug in the library?
//repeat reading tag 21 times
//after 20 reads, tag starts to show 59102000232 (instead of 580912131888)
//it does not reset/clear the read then but goes up to 60102000232
//then with every subsequent read the number changes a bit: 62102000232
//next read: 63102000232
//next read: 64102000232
//eventually this becomes: 92102000232
//after that, there is a crash (E (199983) i2c.master: I2C hardware NACK detected)
#define LOGENABLED
#ifdef LOGENABLED
#define LOG_PRINT(x) Log(x)
#else
#define LOG_PRINT(x)
#endif
#include <Wire.h>
#include "SparkFun_Qwiic_Rfid.h"
#define RFID_ADDR 0x13 // Default I2C address
Qwiic_Rfid myRfid(RFID_ADDR);
String tagRead = "0000000000000000"; //length 16
const char* tagEmpty = "000000";
const char* tagRequired = "580912131888";
const char* textNoTag = "TAG_0";
const char* textTrueTag = "TAG_1";
const char* textFalseTag = "TAG_2";
bool nfcDetected;
unsigned long nfcUpdateLast = 0;
unsigned long nfcUpdateInterval = 500;
int reading = 0;
void setup()
{
Serial.begin(115200);
while(!Serial) delay(10);
Wire.begin();
if(!myRfid.begin())
{
while(1);
}
myRfid.clearTags();
}
void loop()
{
if (millis() - nfcUpdateLast >= nfcUpdateInterval)
{
nfcUpdateLast = millis();
LoopNFC();
}
}
void LoopNFC()
{
//Reset values before the next reading
if (nfcDetected == 1)
{
myRfid.clearTags();
nfcDetected = 0;
tagRead = tagEmpty;
}
//Read tag
tagRead = myRfid.getTag();
LOG_PRINT(tagRead);
if (tagRead.equals(tagEmpty))
{
LOG_PRINT(textNoTag);
}
else
{
reading++;
nfcDetected = 1;
}
if (nfcDetected == 1)
{
if (tagRead.equals(tagRequired))
{
LOG_PRINT(textTrueTag);
}
else
{
LOG_PRINT(textFalseTag);
}
//clear all discovered tags after everything is done this loop, otherwise myRfid delivers wrong results
//myRfid.clearTags();
Serial.println(reading);
}
}
void Log(String pText)
{
#ifdef LOGENABLED
Serial.println(pText);
#endif
}