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)