From 3e4e76e63416f1e9e433ea26db1b0198a5c53aaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Fiedler?= Date: Tue, 18 Feb 2025 19:56:59 +0100 Subject: [PATCH] Use Log instead of Serial.print --- .gitignore | 2 ++ Software/src/Display.cpp | 3 ++- Software/src/NFC.cpp | 30 ++++++++++++++---------------- Software/src/OTAProxy.cpp | 18 +++++++++--------- Software/src/main.cpp | 34 ++++++++++++++++++---------------- 5 files changed, 45 insertions(+), 42 deletions(-) diff --git a/.gitignore b/.gitignore index da43a0c..5230bcf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ Software/.DS_Store + +.DS_Store diff --git a/Software/src/Display.cpp b/Software/src/Display.cpp index 390cd34..9a0d7ef 100644 --- a/Software/src/Display.cpp +++ b/Software/src/Display.cpp @@ -2,6 +2,7 @@ #include #include #include +#include // Constructor: initializes I2C, creates the display object, // initializes the display, clears it, and sets an initial rotation. @@ -16,7 +17,7 @@ Display::Display(int sda, int scl, int readerid) display = new Adafruit_SSD1306(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); if (!display->begin(SSD1306_SWITCHCAPVCC, 0x3C)) { - Serial.println("SSD1306 allocation failed"); + Log.error(F("SSD1306 allocation failed")); while (1) { yield(); // Infinite loop if display initialization fails diff --git a/Software/src/NFC.cpp b/Software/src/NFC.cpp index 6628763..9651c63 100644 --- a/Software/src/NFC.cpp +++ b/Software/src/NFC.cpp @@ -2,10 +2,11 @@ #include #include #include "helpers.h" +#include NFC::NFC(int pin_sda, int pin_scl) { - Serial.println("NFC: Start"); + Log.trace(F("NFC: Start")); // Create a new PN532 instance with the defined IRQ and RESET pins. rfid = new Adafruit_PN532(PN532_IRQ, PN532_RESET); @@ -16,7 +17,7 @@ NFC::NFC(int pin_sda, int pin_scl) if (!versiondata) { // If no version data is returned, the PN532 was not detected. - Serial.println("Didn't find PN53x board"); + Log.error(F("Didn't find PN53x board")); while (true) { yield(); // Halt execution indefinitely if the board is missing. @@ -24,8 +25,8 @@ NFC::NFC(int pin_sda, int pin_scl) } // Print chip and firmware details using formatted output. - Serial.printf("NFC: Found chip PN5%02X\n", (versiondata >> 24) & 0xFF); - Serial.printf("NFC: Firmware ver. %d.%d\n", (versiondata >> 16) & 0xFF, (versiondata >> 8) & 0xFF); + Log.trace(F("NFC: Found chip PN5%02X"), (versiondata >> 24) & 0xFF); + Log.trace(F("NFC: Firmware ver. %d.%d"), (versiondata >> 16) & 0xFF, (versiondata >> 8) & 0xFF); } // Convert the card's UID (stored in a byte array) into a hexadecimal String. @@ -66,30 +67,29 @@ bool NFC::checkforCard() // Attempt to read the card's UID (ISO14443A standard) within 1000 ms. if (!rfid->readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength, 1000)) { - Serial.println("No card found within the timeout period"); + Log.warning("No card found within the timeout period"); return false; } // Validate the card's UID length: 7 for DESFire, 4 for MIFARE Classic/ISO14443A. if (uidLength != 7 && uidLength != 4) { - Serial.println("No card detected"); + Log.warning("No card detected"); return false; } // Report the type of card detected. if (uidLength == 7) { - Serial.println("DESFire card detected"); + Log.info("DESFire card detected"); } else { - Serial.println("MIFARE Classic or another ISO14443A card detected"); + Log.info("MIFARE Classic or another ISO14443A card detected"); } // Print the card's UID in hexadecimal format. - Serial.print("UID: "); - printbytes(uid, uidLength); + Log.trace(F("UID: %s"), getUID().c_str()); return true; } @@ -110,7 +110,7 @@ bool NFC::connecttoCard() // Send the RATS command and check for errors. if (rfid->inDataExchange(ratsCmd, sizeof(ratsCmd), atsResponse, &atsResponseLength) < 0) { - Serial.println(F("Failed ATS")); + Log.error(F("Failed ATS")); // PN532 does not offer a dedicated halt command, // so you may simply return false to abort communication. return false; @@ -129,7 +129,7 @@ bool NFC::connecttoCard() // Send the PPS command and check for errors. if (rfid->inDataExchange(ppsCmd, sizeof(ppsCmd), ppsResponse, &ppsResponseLength) < 0) { - Serial.println(F("Failed PPS")); + Log.error(F("Failed PPS")); return false; } @@ -146,7 +146,6 @@ bool NFC::connecttoCard() // Disconnect from the card by issuing an ISO14443A HALT command. bool NFC::disconnectCard() { - // HALT command for ISO14443A cards in auto-CRC mode: [0x50, 0x00] // You can either compute the CRC or, if your setup automatically appends it, // simply send the command without CRC bytes. @@ -164,8 +163,7 @@ bool NFC::disconnectCard() int status = rfid->inDataExchange(haltCmd, sizeof(haltCmd), response, &responseLength); if (status < 0) { - Serial.println(F("Failed to send HALT command")); - Serial.println(status); + Log.error(F("Failed to send HALT command: %d"), status); // Combined Serial.println calls return false; } @@ -186,4 +184,4 @@ bool NFC::disconnectCard() bool NFC::testCard() { return true; -} +} \ No newline at end of file diff --git a/Software/src/OTAProxy.cpp b/Software/src/OTAProxy.cpp index d0573d4..4d651f8 100644 --- a/Software/src/OTAProxy.cpp +++ b/Software/src/OTAProxy.cpp @@ -1,6 +1,7 @@ #include "OTAProxy.h" #include "NFC.h" #include +#include OTAProxy::OTAProxy(PubSubClient *mqttClient, NFC *nfc, int id) { @@ -28,7 +29,7 @@ void OTAProxy::startOTA() String uid = nfc->getUID(); mqtt->publish(topic, uid.c_str(), uid.length()); - Serial.println("Start OTA"); + Log.info(F("Start OTA")); } void OTAProxy::continueOTA(char *topic, byte *payload, unsigned int length) @@ -48,7 +49,7 @@ void OTAProxy::continueOTA(char *topic, byte *payload, unsigned int length) if (!strcmp(topic, topic_requestOTA)) { - Serial.println("Request OTA"); + Log.info("Request OTA"); byte response[APDU_BUFFER_SIZE] = {0}; @@ -58,9 +59,9 @@ void OTAProxy::continueOTA(char *topic, byte *payload, unsigned int length) // Cast length (unsigned int) to uint8_t, assuming it fits within 255 bytes. uint8_t sendLength = (uint8_t)length; - Serial.println("Run Transceive"); + Log.trace(F("Run Transceive")); int status = nfc->rfid->inDataExchange(payload, sendLength, response, &response_len); - Serial.printf("PICC_Transceive: 0x%02x\n", status); + Log.trace(F("PICC_Transceive: 0x%02x"), status); if (status < 0) { @@ -69,19 +70,18 @@ void OTAProxy::continueOTA(char *topic, byte *payload, unsigned int length) } mqtt->publish(topic_responseOTA, response, response_len); - Serial.println("Response OTA"); + Log.info("Response OTA"); } else if (!strcmp(topic, topic_stopOTA)) { - Serial.println("Stop OTA"); + Log.info("Stop OTA"); nfc->disconnectCard(); activeOTA = false; } // else if (!strcmp(topic, topic_restartOTA)) // { - // Serial.println("Restart OTA"); + // Log.info("Restart OTA"); // Updated if uncommented // // Add your PN532-specific card deselection logic here if needed. - // // For example, you might send a halt command or wait until the card is removed. // } } @@ -99,5 +99,5 @@ void OTAProxy::cancelOTA() } activeOTA = false; - Serial.println("Cancel OTA"); + Log.info("Cancel OTA"); } \ No newline at end of file diff --git a/Software/src/main.cpp b/Software/src/main.cpp index 8f77a39..aa6b4d8 100644 --- a/Software/src/main.cpp +++ b/Software/src/main.cpp @@ -1,6 +1,5 @@ #include #include - #include "config.h" #include #include @@ -27,7 +26,7 @@ unsigned long lastotatime; void setup_wifi() { delay(10); - Serial.println("Connecting Wifi ..."); + Log.trace(F("Connecting Wifi ...")); WiFi.mode(WIFI_STA); WiFi.begin(WLAN_SSID, WLAN_PASS); @@ -38,7 +37,7 @@ void setup_wifi() } randomSeed(micros()); - Serial.println("WiFi connected"); + Log.trace(F("WiFi connected")); } void reconnect() @@ -48,7 +47,7 @@ void reconnect() String clientId = "FabReader_"; clientId += String(FABREADERID); - Serial.println("Connecting MQTT ..."); + Log.trace(F("Connecting MQTT ...")); bool connected = false; if (strcmp(MQTT_USERNAME, "") == 0) { @@ -61,7 +60,7 @@ void reconnect() if (connected) { - Serial.println("MQTT connected"); + Log.trace(F("MQTT connected")); char id[6] = "00000"; sprintf(id, "%05d", FABREADERID); mqtt->publish("fabreader", id); @@ -86,9 +85,7 @@ void reconnect() display->writeTitle("Reconnect"); display->writeInfo("MQTT"); - Serial.print("failed, rc="); - Serial.print(mqtt->state()); - Serial.println(" try again in 5 seconds"); + Log.error(F("MQTT connection failed, rc=%d, try again in 5 seconds"), mqtt->state()); delay(5000); } } @@ -96,8 +93,7 @@ void reconnect() void callback(char *topic, byte *payload, unsigned int length) { - Serial.println("Receive Message"); - Serial.println(topic); + Log.trace(F("Receive Message: %s"), topic); if (ota->hasActiveOTA()) { ota->continueOTA(topic, payload, length); @@ -108,8 +104,14 @@ void callback(char *topic, byte *payload, unsigned int length) void setup() { Serial.begin(115200); - Serial.print("\n\n\n"); - Serial.println("Booting ..."); + + Log.begin(LOG_LEVEL_VERBOSE, &Serial); // Initialize ArduinoLog + + // Optional: add a timestamp to each log entry. + // Log.begin(LOG_LEVEL_VERBOSE, &Serial, true); + + Log.trace(F("\n\n\n")); + Log.trace(F("Booting ...")); pinMode(PIN_BUZZER, OUTPUT); @@ -117,11 +119,11 @@ void setup() display->clearReaderInfo(); display->writeInfo("Start NFC ..."); - Serial.println("Connecting NFC ..."); + Log.trace(F("Connecting NFC ...")); nfc = new NFC(PIN_SDA, PIN_SCL); - Serial.println("NFC connected"); + Log.trace(F("NFC connected")); display->writeInfo("Start WIFI ..."); setup_wifi(); @@ -147,10 +149,10 @@ void loop() { if (nfc->checkforCard()) { - Serial.println("Card detected"); + Log.trace(F("Card detected")); if (nfc->connecttoCard()) { - Serial.println("Card connected"); + Log.trace(F("Card connected")); lastotatime = millis(); display->createReaderInfo("Run OTA");