Callback not working while using library for an NFC-reader

Hello,

I'm currently doing an IoT project, which includes reading data from an NFC-tag (with a reader called PN532 from Elechouse). I would like to apply a callback function which could receive messages through mqtt, however after adding a line in the void loop() which reads NFC-tags, no callback messages get through.

void loop(void) {

  delay(500);
  // Maintain the MQTT connection
  if (!client.connected()) {
    reconnect1();
  }
  // Allow client to process incoming messages and maintain connection to server.
  client.loop();
  if(!client2.connected()) {
    reconnect2();
  }
  client2.loop();

    // Maintain the PN532 connection
  while (!connected) {  
    connected = connect();
  }
  
  // Register an NFC-card
  while (!firstCardRegistered){
    acc_str_card += register_card();
    firstCardRegistered = true;
  }


  // Buffer to store the UID
  uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };
  // UID size (4 or 7 bytes depending on card type)
  uint8_t uidLength;
  // The following lines wait for information from an NFC-tag.
  cardRecognized = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);

  if (cardRecognized) {
    
    std::string uidstr = ""; // The id of the NFC-tag in base 10. 
    for (uint8_t i = 0; i < uidLength; i++)
    {
      uidstr += std::to_string(uid[i]) + " ";
    }
    const char* uidcc = uidstr.c_str(); // MQTT accepts only string of the form "const char*"

    // Turn security on or off
    if (acc_str_card == uidcc){
      Serial.println("Proper tag used!");
      if (onOff == 0 ){
        publishMessage(mqtt_topic, "Authenticated! System turned on!");
        Serial.println("Authenticated! System turned on!");
        setColor(0,255,0);
        delay(3000);
        onOff = 1;
      } else {
        publishMessage(mqtt_topic, "Authenticated! System turned off!");
        Serial.println("Authenticated! System turned off!");
        delay(3000);
        onOff = 0;
      }

    } else {
      Serial.println("False NFC-chip");
      publishMessage(mqtt_topic, "False Nfc-Chip!");
      delay(3000);
    }

    Serial.println("Waiting for card (ISO14443A Mifare)...");
    Serial.println("");
  }

}

, where the problematic line is

cardRecognized = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);

This line alone makes it impossible to receive callback messages. When it is not being used everything works normally. What could be the reason?

Library for NFC-tag reader:

elechouse/PN532: NFC library for Arduino using PN532 (github.com)

readPassiveTargetID() can take a 4th parameter, a timeout. From PN532.h:

bool readPassiveTargetID(uint8_t cardbaudrate, uint8_t *uid, uint8_t *uidLength, uint16_t timeout = 1000);

The default is 1000 (probably milliseconds); in your sketch, pass it a lower value and see if that solves the problem.

Note:
Not familiar with MQTT and not familiar with NFC stuff.

1 Like

Seems it was the needed change!

With uint16_t timeout = 100 , messages are received with 5 second intervals,
with uint16_t timeout = 50, messages take around 3 seconds to be viewed.

with >500 I need to do empirical testing, but tried that value first and definetly took over 20 seconds (quitted before any messages were received).

Thanks !

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.