From b1f86b90d58360d7200a46f5f025bc53bf059756 Mon Sep 17 00:00:00 2001 From: Daniele Verducci Date: Thu, 11 Dec 2025 23:38:01 +0100 Subject: [PATCH] Selecting folder by tag works, but invalid tags makes the device crash --- esphome/fabula.yaml | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/esphome/fabula.yaml b/esphome/fabula.yaml index daab4ea..8c6dc53 100644 --- a/esphome/fabula.yaml +++ b/esphome/fabula.yaml @@ -42,16 +42,22 @@ pn532_i2c: loop: True # Crashes somewhere here in the lambda folder: !lambda |- - if (!tag.has_ndef_message()) { - return 0; // Plays "No tag found" audio - } - auto message = tag.get_ndef_message(); - auto records = message->get_records(); - for (auto &record : records) { - std::string payload = record->get_payload(); - ESP_LOGD("Fabula", "NFC NDEF record type %s - value %s - int value %d", record->get_type().c_str(), record->get_payload().c_str(), stoi(payload)); - return stoi(payload); + if (tag.has_ndef_message()) { + const auto &message = tag.get_ndef_message(); + const auto &records = message->get_records(); + for (const auto &record : records) { + if (record->get_type().c_str()[0] != 'T') { + ESP_LOGD("Fabula", " Ignoring non-text record %s", record->get_payload().c_str()); + continue; + } + ESP_LOGD("Fabula", " Found record %s in tag, try to convert to int", record->get_payload().c_str()); + // Note: exception handling is disabled in ESPHome: the following line may crash the microcontroller if a tag with a textual record if scanned + int folderId = std::stoi(record->get_payload()); + ESP_LOGD("Fabula", " Found valid record in tag: playing folder %d", folderId); + return folderId; + } } + ESP_LOGD("Fabula", " No valid record found in tag. Please add a text field containing the folder number, e.g. 001"); return 0; on_tag_removed: then: