From 7902518002c0b824038b63318307932f3ac8cafa Mon Sep 17 00:00:00 2001 From: Jean Jacques Avril Date: Sat, 4 Oct 2025 16:17:08 +0200 Subject: [PATCH] Enhance HardwareRfid class with detailed logging and hardware verification; update main loop to log detected RFID tags --- lib/hardware/hardware_rfid.cpp | 55 ++++++++++++++++++++++++++++++---- lib/hardware/hardware_rfid.hpp | 21 +++++++++---- platformio.ini | 2 +- src/main.cpp | 1 + 4 files changed, 67 insertions(+), 12 deletions(-) diff --git a/lib/hardware/hardware_rfid.cpp b/lib/hardware/hardware_rfid.cpp index bfa888b..01ed88e 100644 --- a/lib/hardware/hardware_rfid.cpp +++ b/lib/hardware/hardware_rfid.cpp @@ -1,19 +1,59 @@ #include "hardware_rfid.hpp" #include +#include + +//#define DEBUG_RFID +#ifdef DEBUG_RFID +#define LOG(msg) Serial.println(msg) +#else +#define LOG(msg) +#endif HardwareRfid::HardwareRfid(uint8_t ssPin, uint8_t rstPin) : _mfrc(ssPin, rstPin) {} void HardwareRfid::begin() { + LOG("RFID begin called"); + SPI.begin(); // Initialize SPI bus _mfrc.PCD_Init(); + + // Verify MFRC522 hardware + uint8_t version = _mfrc.PCD_ReadRegister(_mfrc.VersionReg); + if (version == 0x00 || version == 0xFF) { + LOG("ERROR: Failed to detect MFRC522 hardware. Check wiring or power."); + } else { + LOG("MFRC522 detected, version: 0x" + String(version, HEX)); + } + + // Optional: Set antenna gain for better range + _mfrc.PCD_SetAntennaGain(_mfrc.RxGain_max); + LOG("RFID initialized, antenna gain set to max"); } void HardwareRfid::end() { - // Nothing special to do + LOG("RFID end called"); + SPI.end(); // Stop SPI bus } void HardwareRfid::update() { - if (!_mfrc.PICC_IsNewCardPresent() || !_mfrc.PICC_ReadCardSerial()) { + static unsigned long lastLog = 0; + unsigned long now = millis(); + if (now - lastLog >= 1000) { + LOG("RFID update called"); + lastLog = now; + } + + // Check if a new card is present + if (!_mfrc.PICC_IsNewCardPresent()) { + if (now - lastLog >= 1000) { + LOG("No new card present"); + } + return; + } + + // Try to read the card's serial number + if (!_mfrc.PICC_ReadCardSerial()) { + LOG("Failed to read card serial"); return; } @@ -22,23 +62,28 @@ void HardwareRfid::update() { for (byte i = 0; i < _mfrc.uid.size; i++) { cardId = (cardId << 8) | _mfrc.uid.uidByte[i]; } + LOG("Card detected, UID: 0x" + String(cardId, HEX)); // Create the message - hardware_SensorToControlMessage msg = {0}; + hardware_SensorToControlMessage msg = hardware_SensorToControlMessage_init_zero; msg.sensor_id = 0; // Assuming single sensor - msg.which_payload = hardware_SensorToControlMessage_rfid_reading_tag; // Assuming this is the tag + msg.which_payload = hardware_SensorToControlMessage_rfid_reading_tag; msg.payload.rfid_reading.card_id = cardId; // Call the callback if set if (_callback) { + LOG("Callback is set, calling it with card ID: 0x" + String(cardId, HEX)); _callback(msg); + } else { + LOG("Callback not set"); } - // Halt the card + // Halt the card and stop crypto _mfrc.PICC_HaltA(); _mfrc.PCD_StopCrypto1(); } void HardwareRfid::setCallback(RfidCallback cb) { _callback = cb; + LOG("RFID callback set"); } \ No newline at end of file diff --git a/lib/hardware/hardware_rfid.hpp b/lib/hardware/hardware_rfid.hpp index 0bf4c8d..734fa6f 100644 --- a/lib/hardware/hardware_rfid.hpp +++ b/lib/hardware/hardware_rfid.hpp @@ -1,13 +1,22 @@ #pragma once -#include -#include #include "hardware.pb.h" +#include -typedef std::function RfidCallback; - +/** + * @class HardwareRfid + * @brief Manages an MFRC522 RFID reader on ESP8266. + * Reads card UIDs and sends them via a callback using hardware_SensorToControlMessage. + */ class HardwareRfid { public: + using RfidCallback = void (*)(const hardware_SensorToControlMessage&); + + /** + * @brief Constructor for HardwareRfid. + * @param ssPin SPI Slave Select (SS) pin for MFRC522. + * @param rstPin Reset pin for MFRC522. + */ HardwareRfid(uint8_t ssPin, uint8_t rstPin); void begin(); void end(); @@ -16,5 +25,5 @@ public: private: MFRC522 _mfrc; - RfidCallback _callback; -}; + RfidCallback _callback = nullptr; +}; \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index 9c3ac30..a675c18 100644 --- a/platformio.ini +++ b/platformio.ini @@ -26,4 +26,4 @@ custom_nanopb_protos = --error-on-unmatched [env:esp8285] platform = espressif8266 -board = esp8285 \ No newline at end of file +board = esp12e diff --git a/src/main.cpp b/src/main.cpp index a2c2d70..5796d08 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,6 +14,7 @@ unsigned long lastChange = 0; int currentConfig = 0; void onRfidTag(const hardware_SensorToControlMessage& msg) { + Serial.println("Callback onRfidTag triggered"); Serial.print("RFID Tag detected: 0x"); Serial.println(msg.payload.rfid_reading.card_id, HEX); }