Hello everyone, I am using esp32 and pn532 nfc sensor and I am trying to read the payload inserted in the tag. I'm using the elechouse/PN532 library and this code worked for a while but now it compiles normally without any console errors, but nothing happens afterwards. Can anyone tell me what could be causing my code not to report an error but not working the way it should? Could it be a problem with the library? Is there another one I can use?
#include <Wire.h>
#include <PN532_I2C.h>
#include <PN532.h>
#include <NfcAdapter.h>
#include <NdefMessage.h>
#include <NdefRecord.h>
PN532_I2C pn532_i2c(Wire);
NfcAdapter nfc = NfcAdapter(pn532_i2c);
void setup() {
Serial.begin(115200);
Serial.println("LEITOR NFC");
nfc.begin();
}
void loop() {
Serial.println("\nApproach the Tag to perform the reading.\n");
Serial.print("outside if");
if (nfc.tagPresent())
{
Serial.print("inside if");
NfcTag tag = nfc.read();
NdefMessage msg = tag.getNdefMessage();
// Get the first record
NdefRecord record = msg.getRecord(0);
// Get the payload size
byte length = record.getPayloadLength();
// Create byte array big enough for payload
byte payload[length];
// Get payload to byte array
record.getPayload(payload);
// Convert byte Array to string
String string = String((char *)payload);
client.publish("unipam/JOHNNFC", string.c_str());
}
}
This is how my console looks when I run the code. Notice that he is not able to enter the "if":
I believe the connections are correct, here is how I am connecting everything. Also because this code worked once with things exactly as they are now, I don't know why everything suddenly stopped.
The tags I'm using are compatible because when I use the <PN532_HSU.h> library I can read the tag normally, the problem only happens when I try to use the i2c connection and library