From 7c0e00717762e7164fc594c9cbd21183e0eb8bef Mon Sep 17 00:00:00 2001 From: Boris Brock Date: Sat, 5 Oct 2024 07:29:56 +0200 Subject: [PATCH] Squashed commit of the following: commit e1647cc5138ff29adc9b34cea17035b245cf49b7 Author: Boris Brock Date: Sat Oct 5 07:29:17 2024 +0200 Correction commit bab7861163c8ce42f2df4e30ac585ce3533ee2d6 Author: Boris Brock Date: Sat Oct 5 07:26:30 2024 +0200 Improving startup info commit 817ccbb99b9553a0685ff364b3c40552af176d10 Author: Boris Brock Date: Sat Oct 5 07:13:40 2024 +0200 Introducing custom logger commit b1a43d025679a5174a657472a426bf802b006385 Author: Boris Brock Date: Sat Oct 5 06:26:32 2024 +0200 Fixing main info block commit 948ec6f14f0fc7c8c6e2e8d257ae39a2b2e96288 Author: Boris Brock Date: Sat Oct 5 06:20:54 2024 +0200 Update ModbusRTU.cpp commit e5dfceafcba003f7c4347c0b6dff3c67ca3bfbe9 Author: Boris Brock Date: Sat Oct 5 06:16:32 2024 +0200 Logging additions commit 055fa2479186ca450270437ae1a506cf712adeef Author: Boris Brock Date: Sat Oct 5 05:47:10 2024 +0200 Integrating DebugLog --- platformio.ini | 4 +- src/Components/Logger/Logger.cpp | 22 +++++++ src/Components/Logger/Logger.h | 63 ++++++++++++++++++++ src/Components/MQTT/MQTTManager.cpp | 11 ++-- src/Components/Modbus/ModbusRTU.cpp | 12 ++-- src/Components/Modbus/ModbusTCP.cpp | 51 ++++++++-------- src/Components/Wallbox/DummyWallbox.cpp | 23 +++---- src/Components/Wallbox/HeidelbergWallbox.cpp | 43 ++++++------- src/Components/WiFi/WifiConnection.cpp | 20 +++---- src/Main.cpp | 25 +++++--- 10 files changed, 185 insertions(+), 89 deletions(-) create mode 100644 src/Components/Logger/Logger.cpp create mode 100644 src/Components/Logger/Logger.h diff --git a/platformio.ini b/platformio.ini index 2b2415e..5307b8e 100644 --- a/platformio.ini +++ b/platformio.ini @@ -27,8 +27,10 @@ lib_deps = [env:heidelberg] build_flags = -O2 + -D LOGGING_LEVEL_ERROR [env:dummy] build_flags = -O2 - -D DUMMY_WALLBOX \ No newline at end of file + -D DUMMY_WALLBOX + -D LOGGING_LEVEL_DEBUG \ No newline at end of file diff --git a/src/Components/Logger/Logger.cpp b/src/Components/Logger/Logger.cpp new file mode 100644 index 0000000..04fc97a --- /dev/null +++ b/src/Components/Logger/Logger.cpp @@ -0,0 +1,22 @@ +#include +#include "Logger.h" + +namespace Logger +{ + const char *GetLogLevel() + { +#ifdef LOGGING_LEVEL_ERROR + return "Error"; +#elif defined(LOGGING_LEVEL_WARNING) + return "Warning"; +#elif defined(LOGGING_LEVEL_INFO) + return "Info"; +#elif defined(LOGGING_LEVEL_DEBUG) + return "Debug"; +#elif defined(LOGGING_LEVEL_TRACE) + return "Trace"; +#else + return "Undefined"; +#endif + } +}; \ No newline at end of file diff --git a/src/Components/Logger/Logger.h b/src/Components/Logger/Logger.h new file mode 100644 index 0000000..674a972 --- /dev/null +++ b/src/Components/Logger/Logger.h @@ -0,0 +1,63 @@ +#pragma once + +namespace Logger +{ + template + void Error(const char *format, Args... args) + { +#if defined(LOGGING_LEVEL_ERROR) || defined(LOGGING_LEVEL_WARNING) || defined(LOGGING_LEVEL_INFO) || defined(LOGGING_LEVEL_DEBUG) || defined(LOGGING_LEVEL_TRACE) + Serial.print("[ERROR] "); + Serial.printf(format, args...); + Serial.print("\n"); +#endif + } + + template + void Warning(const char *format, Args... args) + { +#if defined(LOGGING_LEVEL_WARNING) || defined(LOGGING_LEVEL_INFO) || defined(LOGGING_LEVEL_DEBUG) || defined(LOGGING_LEVEL_TRACE) + Serial.print("[WARNING] "); + Serial.printf(format, args...); + Serial.print("\n"); +#endif + } + + template + void Info(const char *format, Args... args) + { +#if defined(LOGGING_LEVEL_INFO) || defined(LOGGING_LEVEL_DEBUG) || defined(LOGGING_LEVEL_TRACE) + Serial.print("[INFO] "); + Serial.printf(format, args...); + Serial.print("\n"); +#endif + } + + template + void Debug(const char *format, Args... args) + { +#if defined(LOGGING_LEVEL_DEBUG) || defined(LOGGING_LEVEL_TRACE) + Serial.print("[DEBUG] "); + Serial.printf(format, args...); + Serial.print("\n"); +#endif + } + + template + void Trace(const char *format, Args... args) + { +#if defined(LOGGING_LEVEL_TRACE) + Serial.print("[TRACE] "); + Serial.printf(format, args...); + Serial.print("\n"); +#endif + } + + template + void Print(const char *format, Args... args) + { + Serial.printf(format, args...); + Serial.print("\n"); + } + + const char *GetLogLevel(); +}; \ No newline at end of file diff --git a/src/Components/MQTT/MQTTManager.cpp b/src/Components/MQTT/MQTTManager.cpp index ff25252..9acd635 100644 --- a/src/Components/MQTT/MQTTManager.cpp +++ b/src/Components/MQTT/MQTTManager.cpp @@ -7,6 +7,7 @@ extern "C" #include "freertos/timers.h" } #include +#include "../Logger/Logger.h" #include "../../Configuration/Version.h" #include "../../Configuration/Constants.h" #include "../../Configuration/Credentials.h" @@ -41,7 +42,7 @@ namespace MQTTManager { if (!gMqttClient.connected()) { - Serial.println("Connecting to MQTT..."); + Logger::Info("Connecting to MQTT broker..."); gMqttClient.connect(); } } @@ -112,7 +113,7 @@ namespace MQTTManager void OnMqttConnect(bool sessionPresent) { - Serial.println("Connected to MQTT"); + Logger::Info("Connected to MQTT"); // Subscribe to control topics gMqttClient.subscribe(ChargingCurrentControl.c_str(), 2); @@ -125,7 +126,7 @@ namespace MQTTManager void OnMqttDisconnect(AsyncMqttClientDisconnectReason reason) { - Serial.println("Disconnected from MQTT"); + Logger::Warning("Disconnected from MQTT"); } void OnMqttMessage(char *topic, char *payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) @@ -133,7 +134,7 @@ namespace MQTTManager if (ChargingCurrentControl == topic) { float current = String(payload, len).toFloat(); - Serial.printf("Received MQTT control command: charging current limit = %f\n", current); + Logger::Trace("Received MQTT control command: charging current limit = %f\n", current); gWallbox->SetChargingCurrentLimit(current); } } @@ -145,7 +146,7 @@ namespace MQTTManager void Init(IWallbox *wallbox) { - Serial.println("Initializing MQTT"); + Logger::Info("Initializing MQTT"); gWallbox = wallbox; diff --git a/src/Components/Modbus/ModbusRTU.cpp b/src/Components/Modbus/ModbusRTU.cpp index 1087815..a6f867e 100644 --- a/src/Components/Modbus/ModbusRTU.cpp +++ b/src/Components/Modbus/ModbusRTU.cpp @@ -1,5 +1,5 @@ #include -#include +#include "../Logger/Logger.h" #include "HardwareSerial.h" #include "ModbusClientRTU.h" #include "../../Configuration/Pins.h" @@ -22,7 +22,7 @@ void ModbusRTU::Init() gMutex = xSemaphoreCreateMutex(); // Init serial conneted to the RTU Modbus - Serial.println("Starting RS485 hardware serial"); + Logger::Info("Starting RS485 hardware serial"); RTUutils::prepareHardwareSerial(gRs485Serial); gRs485Serial.begin( Constants::HeidelbergWallbox::ModbusBaudrate, @@ -31,7 +31,7 @@ void ModbusRTU::Init() Pins::PinTX); // Start Modbus RTU - Serial.println("Creating Modbus RTU instance"); + Logger::Trace("Creating Modbus RTU instance"); gModbusRTU.setTimeout(Constants::HeidelbergWallbox::ModbusTimeoutMs); gModbusRTU.begin(gRs485Serial); // Start ModbusRTU background task } @@ -62,8 +62,7 @@ bool ModbusRTU::ReadRegisters(uint16_t startAddress, uint8_t numValues, uint8_t } else { - Serial.print("ModbusRTU read error: "); - Serial.println(response.getError()); + Logger::Error("ModbusRTU read error: %d", response.getError()); for (uint8_t wordIndex = 0; wordIndex < numValues; ++wordIndex) { values[wordIndex] = 0; @@ -96,8 +95,7 @@ bool ModbusRTU::WriteHoldRegister16(uint16_t address, uint16_t value) } else { - Serial.print("ModbusRTU write error: "); - Serial.println(response.getError()); + Logger::Error("ModbusRTU write error: %d", response.getError()); return false; } } diff --git a/src/Components/Modbus/ModbusTCP.cpp b/src/Components/Modbus/ModbusTCP.cpp index f13ffef..df152d6 100644 --- a/src/Components/Modbus/ModbusTCP.cpp +++ b/src/Components/Modbus/ModbusTCP.cpp @@ -1,5 +1,6 @@ #include #include +#include "../Logger/Logger.h" #include "ModbusServerWiFi.h" #include "../../Configuration/Constants.h" #include "../Wallbox/IWallbox.h" @@ -13,13 +14,13 @@ namespace ModbusTCP ModbusMessage ReadInputRegister(ModbusMessage msg) { // For debugging - Serial.println("\nMB TCP request received: READ_INPUT_REGISTER:"); - Serial.printf(" > Size: %d\n", msg.size()); - Serial.printf(" > FC: %d\n", msg.getFunctionCode()); - Serial.println(" > Data:"); + Logger::Trace("Modbus TCP request received: READ_INPUT_REGISTER:"); + Logger::Trace(" > Size: %d", msg.size()); + Logger::Trace(" > FC: %d", msg.getFunctionCode()); + Logger::Trace(" > Data:"); for (uint8_t i = 0; i < msg.size(); ++i) { - Serial.printf(" > %d\n", msg[i]); + Logger::Trace(" > %d", msg[i]); } return ModbusMessage{}; } @@ -31,7 +32,7 @@ namespace ModbusTCP uint16_t numWords = 0; request.get(2, startAddress); request.get(4, numWords); - Serial.printf("\nModbusTCP request received: READ_HOLD_REGISTER %d words at reg. %d\n", numWords, startAddress); + Logger::Trace("ModbusTCP request received: READ_HOLD_REGISTER %d words at reg. %d", numWords, startAddress); // Respond properly ModbusMessage response; @@ -40,7 +41,7 @@ namespace ModbusTCP { case (Constants::DaheimladenRegisters::Status): { - Serial.println(" -> Responding with wallbox status"); + Logger::Trace(" -> Responding with wallbox status"); responseLengthBytes = 2; const uint16_t rawState = static_cast(gWallbox->GetState()); response.add(request.getServerID(), fc, responseLengthBytes); @@ -49,7 +50,7 @@ namespace ModbusTCP } case (Constants::DaheimladenRegisters::LimitChargingCurrent): { - Serial.println(" -> Responding with charging current limit"); + Logger::Trace(" -> Responding with charging current limit"); responseLengthBytes = 2; uint16_t rawCurrent = static_cast(gWallbox->GetChargingCurrentLimit() * Constants::DaheimladenWallbox::CurrentFactor); response.add(request.getServerID(), fc, responseLengthBytes); @@ -58,7 +59,7 @@ namespace ModbusTCP } case (Constants::DaheimladenRegisters::ConnectionTimeoutTime): { - Serial.println(" -> Responding with connection timeout time"); + Logger::Trace(" -> Responding with connection timeout time"); responseLengthBytes = 2; const uint16_t dummyValue = 60; response.add(request.getServerID(), fc, responseLengthBytes); @@ -67,7 +68,7 @@ namespace ModbusTCP } case (Constants::DaheimladenRegisters::EnergyMeter): { - Serial.println(" -> Responding with energy meter value"); + Logger::Trace(" -> Responding with energy meter value"); responseLengthBytes = 4; const uint32_t rawEnergy01kWh = static_cast(gWallbox->GetEnergyMeterValue() * Constants::DaheimladenWallbox::EnergyFactor); response.add(request.getServerID(), fc, responseLengthBytes); @@ -76,7 +77,7 @@ namespace ModbusTCP } case (Constants::DaheimladenRegisters::MaxChargingCurrentAfterConnectionLoss): { - Serial.println(" -> Responding with connection loss current"); + Logger::Trace(" -> Responding with connection loss current"); responseLengthBytes = 2; uint16_t rawCurrent = static_cast(gWallbox->GetFailsafeCurrent() * Constants::DaheimladenWallbox::CurrentFactor); response.add(request.getServerID(), fc, responseLengthBytes); @@ -85,7 +86,7 @@ namespace ModbusTCP } case (Constants::DaheimladenRegisters::TotalChargingPower): { - Serial.println(" -> Responding with total charging power"); + Logger::Trace(" -> Responding with total charging power"); responseLengthBytes = 4; const uint32_t powerW = static_cast(gWallbox->GetChargingPower()); response.add(request.getServerID(), fc, responseLengthBytes); @@ -94,7 +95,7 @@ namespace ModbusTCP } case (Constants::DaheimladenRegisters::ChargeCurrents): { - Serial.println(" -> Responding with charging currents"); + Logger::Trace(" -> Responding with charging currents"); responseLengthBytes = 12; float c1, c2, c3; @@ -111,7 +112,7 @@ namespace ModbusTCP } case (Constants::DaheimladenRegisters::ChargeVoltages): { - Serial.println(" -> Responding with charging voltages"); + Logger::Trace(" -> Responding with charging voltages"); responseLengthBytes = 12; float v1, v2, v3; @@ -129,7 +130,7 @@ namespace ModbusTCP case (Constants::DaheimladenRegisters::RfidStationId): case (Constants::DaheimladenRegisters::RfidCardId): { - Serial.println(" -> Responding with ID"); + Logger::Trace(" -> Responding with ID"); responseLengthBytes = 32; const uint32_t dummyValue = 0; response.add(request.getServerID(), fc, responseLengthBytes); @@ -139,7 +140,7 @@ namespace ModbusTCP } default: { - Serial.println(" -> Responding with error ILLEGAL_DATA_ADDRESS"); + Logger::Error(" -> Responding with error ILLEGAL_DATA_ADDRESS"); response.setError(request.getServerID(), fc, Modbus::Error::ILLEGAL_DATA_ADDRESS); break; } @@ -151,13 +152,13 @@ namespace ModbusTCP ModbusMessage WriteHoldRegister(ModbusMessage msg) { // For debugging only - Serial.println("\nMB TCP request received: WRITE_HOLD_REGISTER"); - Serial.printf(" > Size: %d\n", msg.size()); - Serial.printf(" > FC: %d\n", msg.getFunctionCode()); - Serial.println(" > Data:"); + Logger::Trace("Modbus TCP request received: WRITE_HOLD_REGISTER"); + Logger::Trace(" > Size: %d", msg.size()); + Logger::Trace(" > FC: %d", msg.getFunctionCode()); + Logger::Trace(" > Data:"); for (uint8_t i = 0; i < msg.size(); ++i) { - Serial.printf(" > %d\n", msg[i]); + Logger::Trace(" > %d", msg[i]); } return msg; // Echo back request } @@ -169,7 +170,7 @@ namespace ModbusTCP uint16_t numWords = 0; request.get(2, startAddress); request.get(4, numWords); - Serial.printf("\nModbusTCP request received: WRITE_MULT_REGISTERS %d words to reg. %d\n", numWords, startAddress); + Logger::Trace("ModbusTCP request received: WRITE_MULT_REGISTERS %d words to reg. %d", numWords, startAddress); // Respond properly switch (startAddress) @@ -178,13 +179,13 @@ namespace ModbusTCP { uint16_t currentLimit = 0; request.get(7, currentLimit); - Serial.printf(" -> Writing charging current limit: %d\n", currentLimit); + Logger::Trace(" -> Writing charging current limit: %d", currentLimit); gWallbox->SetChargingCurrentLimit(static_cast(currentLimit * 0.1f)); break; } default: { - Serial.println(" -> Responding with error ILLEGAL_DATA_ADDRESS"); + Logger::Error(" -> Responding with error ILLEGAL_DATA_ADDRESS"); ModbusMessage response; response.setError(request.getServerID(), fc, Modbus::Error::ILLEGAL_DATA_ADDRESS); return response; @@ -200,7 +201,7 @@ namespace ModbusTCP void Init(IWallbox *wallbox) { - Serial.println("Initializing Modbus TCP server"); + Logger::Info("Initializing Modbus TCP server"); gWallbox = wallbox; diff --git a/src/Components/Wallbox/DummyWallbox.cpp b/src/Components/Wallbox/DummyWallbox.cpp index 10c7a12..336dd4f 100644 --- a/src/Components/Wallbox/DummyWallbox.cpp +++ b/src/Components/Wallbox/DummyWallbox.cpp @@ -1,4 +1,5 @@ #include +#include "../Logger/Logger.h" #include "../../Configuration/Constants.h" #include "DummyWallbox.h" @@ -10,19 +11,19 @@ DummyWallbox *DummyWallbox::Instance() void DummyWallbox::Init() { - Serial.println("Dummy wallbox: initializing"); + Logger::Debug("Dummy wallbox: initializing"); } VehicleState DummyWallbox::GetState() { if (mChargingCurrentLimitA > 0.0f) { - Serial.println("Dummy wallbox: returning state CHARGING"); + Logger::Debug("Dummy wallbox: returning state CHARGING"); return VehicleState::Charging; } else { - Serial.println("Dummy wallbox: returning state CONNECTED"); + Logger::Debug("Dummy wallbox: returning state CONNECTED"); return VehicleState::Connected; } } @@ -30,37 +31,37 @@ VehicleState DummyWallbox::GetState() bool DummyWallbox::SetChargingCurrentLimit(float currentLimitA) { mChargingCurrentLimitA = currentLimitA; - Serial.printf("Dummy wallbox: setting charging current limit to %f A\n", mChargingCurrentLimitA); + Logger::Debug("Dummy wallbox: setting charging current limit to %f A", mChargingCurrentLimitA); return true; } float DummyWallbox::GetChargingCurrentLimit() { - Serial.printf("Dummy wallbox: returning charging current limit %f A\n", mChargingCurrentLimitA); + Logger::Debug("Dummy wallbox: returning charging current limit %f A", mChargingCurrentLimitA); return mChargingCurrentLimitA; } float DummyWallbox::GetEnergyMeterValue() { - if(mChargingCurrentLimitA > 0.0f) + if (mChargingCurrentLimitA > 0.0f) { mEnergyMeterKWh += 0.1f; } - Serial.printf("Dummy wallbox: returning energy meter value %f kWh\n", mEnergyMeterKWh); + Logger::Debug("Dummy wallbox: returning energy meter value %f kWh", mEnergyMeterKWh); return mEnergyMeterKWh; } float DummyWallbox::GetFailsafeCurrent() { - Serial.printf("Dummy wallbox: returning failsafe current %f A\n", mFailsafeCurrentA); + Logger::Debug("Dummy wallbox: returning failsafe current %f A", mFailsafeCurrentA); return mFailsafeCurrentA; } float DummyWallbox::GetChargingPower() { float chargingPowerW = mChargingCurrentLimitA * Constants::DummyWallbox::ChargingVoltageV * Constants::DummyWallbox::NumPhases; - Serial.printf("Dummy wallbox: returning charging power %f W\n", chargingPowerW); + Logger::Debug("Dummy wallbox: returning charging power %f W", chargingPowerW); return chargingPowerW; } @@ -69,7 +70,7 @@ bool DummyWallbox::GetChargingCurrents(float &c1A, float &c2A, float &c3A) c1A = mChargingCurrentLimitA; c2A = mChargingCurrentLimitA; c3A = mChargingCurrentLimitA; - Serial.printf("Dummy wallbox: returning charging currents %f, %f and %f A\n", c1A, c2A, c3A); + Logger::Debug("Dummy wallbox: returning charging currents %f, %f and %f A", c1A, c2A, c3A); return true; } @@ -78,7 +79,7 @@ bool DummyWallbox::GetChargingVoltages(float &v1V, float &v2V, float &v3V) v1V = Constants::DummyWallbox::ChargingVoltageV; v2V = Constants::DummyWallbox::ChargingVoltageV; v3V = Constants::DummyWallbox::ChargingVoltageV; - Serial.printf("Dummy wallbox: returning charging voltages %f, %f and %f A\n", v1V, v2V, v3V); + Logger::Debug("Dummy wallbox: returning charging voltages %f, %f and %f A", v1V, v2V, v3V); return true; } diff --git a/src/Components/Wallbox/HeidelbergWallbox.cpp b/src/Components/Wallbox/HeidelbergWallbox.cpp index 62549fa..709a5f6 100644 --- a/src/Components/Wallbox/HeidelbergWallbox.cpp +++ b/src/Components/Wallbox/HeidelbergWallbox.cpp @@ -1,4 +1,5 @@ #include +#include "../Logger/Logger.h" #include "../Modbus/ModbusRTU.h" #include "../../Configuration/Constants.h" #include "HeidelbergWallbox.h" @@ -12,19 +13,19 @@ HeidelbergWallbox *HeidelbergWallbox::Instance() void HeidelbergWallbox::Init() { uint16_t rawCurrent = static_cast(Constants::HeidelbergWallbox::FailSafeCurrentA / Constants::HeidelbergWallbox::CurrentFactor); - Serial.printf("Heidelberg wallbox: Initializing fail safe current with %d (raw)\n", rawCurrent); + Logger::Debug("Heidelberg wallbox: Initializing fail safe current with %d (raw)", rawCurrent); if (!ModbusRTU::Instance()->WriteHoldRegister16(Constants::HeidelbergRegisters::FailsafeCurrent, rawCurrent)) { // Error writing modbus register - Serial.println("ERROR: Could not set fail safe current"); + Logger::Error("ERROR: Could not set fail safe current"); } uint16_t standbyDisabled = 4; - Serial.printf("Heidelberg wallbox: Initializing standby mode with %d (raw)\n", standbyDisabled); + Logger::Debug("Heidelberg wallbox: Initializing standby mode with %d (raw)", standbyDisabled); if (!ModbusRTU::Instance()->WriteHoldRegister16(Constants::HeidelbergRegisters::DisableStandby, standbyDisabled)) { // Error writing modbus register - Serial.println("ERROR: Could not disable standby"); + Logger::Error("ERROR: Could not disable standby"); } } @@ -33,7 +34,7 @@ VehicleState HeidelbergWallbox::GetState() uint16_t registerValue[0]; if (ModbusRTU::Instance()->ReadRegisters(Constants::HeidelbergRegisters::ChargingState, 1, 0x4, registerValue)) { - Serial.printf("Heidelberg wallbox: Read state: %d\n", registerValue[0]); + Logger::Debug("Heidelberg wallbox: Read state: %d", registerValue[0]); if (registerValue[0] <= 3) { mState = VehicleState::Disconnected; @@ -50,7 +51,7 @@ VehicleState HeidelbergWallbox::GetState() else { // Error reading modbus register - Serial.println("Heidelberg wallbox: ERROR: Could not read plugged state"); + Logger::Error("Heidelberg wallbox: ERROR: Could not read plugged state"); } return mState; @@ -59,13 +60,13 @@ VehicleState HeidelbergWallbox::GetState() bool HeidelbergWallbox::SetChargingCurrentLimit(float currentLimitA) { mChargingCurrentLimitA = currentLimitA; - Serial.printf("Heidelberg wallbox: setting charging current limit to %f A\n", mChargingCurrentLimitA); + Logger::Debug("Heidelberg wallbox: setting charging current limit to %f A", mChargingCurrentLimitA); uint16_t rawCurrent = static_cast(mChargingCurrentLimitA / Constants::HeidelbergWallbox::CurrentFactor); if (!ModbusRTU::Instance()->WriteHoldRegister16(Constants::HeidelbergRegisters::MaximalCurrent, rawCurrent)) { // Error writing modbus register - Serial.println("Heidelberg wallbox: ERROR: Could not set maximum charging current"); + Logger::Error("Heidelberg wallbox: ERROR: Could not set maximum charging current"); } return true; @@ -77,13 +78,13 @@ float HeidelbergWallbox::GetChargingCurrentLimit() if (ModbusRTU::Instance()->ReadRegisters(Constants::HeidelbergRegisters::MaximalCurrent, 1, 0x3, registerValue)) { mChargingCurrentLimitA = static_cast(registerValue[0] * Constants::HeidelbergWallbox::CurrentFactor); - Serial.printf("Heidelberg wallbox: Read max. charging current: %d\n", mChargingCurrentLimitA); + Logger::Debug("Heidelberg wallbox: Read max. charging current: %d", mChargingCurrentLimitA); return mChargingCurrentLimitA; } else { // Error reading modbus register - Serial.println("Heidelberg wallbox: ERROR: Could not read max. charging current"); + Logger::Error("Heidelberg wallbox: ERROR: Could not read max. charging current"); return mChargingCurrentLimitA; // Return last valid value } } @@ -101,11 +102,11 @@ float HeidelbergWallbox::GetEnergyMeterValue() uint32_t totalEnergyWh = static_cast(rawEnergy[0]) << 16 | static_cast(rawEnergy[1]); mLastEnergyMeterValueWh = static_cast(totalEnergyWh); - Serial.printf("Heidelberg wallbox: Read energy meter value: %f Wh\n", mLastEnergyMeterValueWh); + Logger::Debug("Heidelberg wallbox: Read energy meter value: %f Wh", mLastEnergyMeterValueWh); } else { - Serial.println("Heidelberg wallbox: ERROR: Could not read energy meter value"); + Logger::Error("Heidelberg wallbox: ERROR: Could not read energy meter value"); } return mLastEnergyMeterValueWh; @@ -117,12 +118,12 @@ float HeidelbergWallbox::GetFailsafeCurrent() if (ModbusRTU::Instance()->ReadRegisters(Constants::HeidelbergRegisters::FailsafeCurrent, 1, 0x3, registerValue)) { mFailsafeCurrentA = static_cast(registerValue[0] * Constants::HeidelbergWallbox::CurrentFactor); - Serial.printf("Read Heidelberg failsafe current: %f\n", mFailsafeCurrentA); + Logger::Debug("Read Heidelberg failsafe current: %f", mFailsafeCurrentA); } else { // Error reading modbus register - Serial.println("Heidelberg wallbox: ERROR: Could not read failsafe current"); + Logger::Error("Heidelberg wallbox: ERROR: Could not read failsafe current"); } return mFailsafeCurrentA; @@ -134,12 +135,12 @@ float HeidelbergWallbox::GetChargingPower() if (ModbusRTU::Instance()->ReadRegisters(Constants::HeidelbergRegisters::Power, 1, 0x4, registerValue)) { mLastPowerMeterValueW = static_cast(registerValue[0]); - Serial.printf("Reading power meter value: %f W\n", mLastPowerMeterValueW); + Logger::Debug("Reading power meter value: %f W", mLastPowerMeterValueW); } else { // Error reading modbus register - Serial.println("Heidelberg wallbox: ERROR: Could not read last power meter value"); + Logger::Error("Heidelberg wallbox: ERROR: Could not read last power meter value"); } return mLastPowerMeterValueW; @@ -158,12 +159,12 @@ bool HeidelbergWallbox::GetChargingCurrents(float &c1A, float &c2A, float &c3A) c1A = static_cast(rawCurrents[0] * Constants::HeidelbergWallbox::CurrentFactor); c2A = static_cast(rawCurrents[1] * Constants::HeidelbergWallbox::CurrentFactor); c3A = static_cast(rawCurrents[2] * Constants::HeidelbergWallbox::CurrentFactor); - Serial.printf("Reading currents: %f %f %f A\n", c1A, c2A, c3A); + Logger::Debug("Reading currents: %f %f %f A", c1A, c2A, c3A); return true; } else { - Serial.println("Heidelberg wallbox: ERROR: Could not read currents"); + Logger::Error("Heidelberg wallbox: ERROR: Could not read currents"); return false; } } @@ -181,12 +182,12 @@ bool HeidelbergWallbox::GetChargingVoltages(float &v1V, float &v2V, float &v3V) v1V = static_cast(rawVoltages[0] * Constants::HeidelbergWallbox::VoltageFactor); v2V = static_cast(rawVoltages[1] * Constants::HeidelbergWallbox::VoltageFactor); v3V = static_cast(rawVoltages[2] * Constants::HeidelbergWallbox::VoltageFactor); - Serial.printf("Reading voltages: %f %f %f V\n", v1V, v2V, v3V); + Logger::Debug("Reading voltages: %f %f %f V", v1V, v2V, v3V); return true; } else { - Serial.println("Heidelberg wallbox: ERROR: Could not read voltages"); + Logger::Error("Heidelberg wallbox: ERROR: Could not read voltages"); return false; } } @@ -201,7 +202,7 @@ float HeidelbergWallbox::GetTemperature() else { // Error reading modbus register - Serial.println("Heidelberg wallbox: ERROR: Could not read PCB temperature"); + Logger::Error("Heidelberg wallbox: ERROR: Could not read PCB temperature"); return 0.0f; } } \ No newline at end of file diff --git a/src/Components/WiFi/WifiConnection.cpp b/src/Components/WiFi/WifiConnection.cpp index 302a5aa..f0283b5 100644 --- a/src/Components/WiFi/WifiConnection.cpp +++ b/src/Components/WiFi/WifiConnection.cpp @@ -3,35 +3,34 @@ #include "../../Configuration/Constants.h" #include "WifiConnection.h" #include "../../Configuration/Credentials.h" +#include "../Logger/Logger.h" namespace WifiConnection { void WiFiStationConnected(WiFiEvent_t event, WiFiEventInfo_t info) { - Serial.println("Connected to AP successfully!"); + Logger::Info("Connected to AP successfully!"); } void WiFiGotIP(WiFiEvent_t event, WiFiEventInfo_t info) { - Serial.println("WiFi connected"); - Serial.println("IP address: "); - Serial.println(WiFi.localIP()); + Logger::Info("WiFi connected"); + Logger::Info("IP address: %s", WiFi.localIP().toString().c_str()); } void WiFiStationDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) { - Serial.println("Disconnected from WiFi access point"); - Serial.print("WiFi lost connection. Reason: "); - Serial.println(info.wifi_sta_disconnected.reason); + Logger::Warning("Disconnected from WiFi access point"); + Logger::Warning("WiFi lost connection. Reason: %d", info.wifi_sta_disconnected.reason); + Logger::Warning("Trying to reconnect"); - Serial.println("Trying to reconnect"); WiFi.begin(Credentials::WiFi::SSID, Credentials::WiFi::Password); } void Init() { // Delete old config - Serial.println("Preparing Wifi"); + Logger::Trace("Preparing Wifi"); WiFi.disconnect(true); delay(100); @@ -45,8 +44,7 @@ namespace WifiConnection WiFi.onEvent(WiFiStationDisconnected, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_DISCONNECTED); // Start Wifi connection - Serial.print("Connecting WiFi, SSID: "); - Serial.println(Credentials::WiFi::SSID); + Logger::Info("Connecting to WiFi SSID '%s'", Credentials::WiFi::SSID); WiFi.begin(Credentials::WiFi::SSID, Credentials::WiFi::Password); } }; \ No newline at end of file diff --git a/src/Main.cpp b/src/Main.cpp index 46e8ce8..e6c81ab 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -1,6 +1,7 @@ #include #include "Configuration/Constants.h" #include "Configuration/Version.h" +#include "Components/Logger/Logger.h" #include "Components/WiFi/WifiConnection.h" #include "Components/Modbus/ModbusRTU.h" #include "Components/Modbus/ModbusTCP.h" @@ -15,22 +16,30 @@ void setup() // Configure serial communication Serial.begin(115200); - // Print version info - Serial.println("Booting HeidelBridge"); - Serial.printf(" Version: %d.%d.%d\n", Version::Major, Version::Minor, Version::Patch); - Serial.printf(" Build date: %s\n", __DATE__); - Serial.println(""); + // Print startup information + Logger::Print("Booting HeidelBridge"); + Logger::Print(" Version: %d.%d.%d", Version::Major, Version::Minor, Version::Patch); + Logger::Print(" Build date: %s", __DATE__); + Logger::Print(""); + Logger::Print("Logging level: %s", Logger::GetLogLevel()); + Logger::Print(""); +#ifndef DUMMY_WALLBOX + Logger::Print("Wallbox mode: real hardware (Heidelberg)"); +#else + Logger::Print("Wallbox mode: simulated (dummy)"); +#endif + Logger::Print(""); // Make sure WiFi connection is up and running WifiConnection::Init(); // Initialize wallbox #ifndef DUMMY_WALLBOX - Serial.println("Starting with Heidelberg wallbox in real mode"); + Logger::Info("Starting with Heidelberg wallbox in real mode"); ModbusRTU::Instance()->Init(); gWallbox = HeidelbergWallbox::Instance(); #else - Serial.println("Starting with dummy wallbox in simulated mode"); + Logger::Info("Starting with dummy wallbox in simulated mode"); gWallbox = DummyWallbox::Instance(); #endif gWallbox->Init(); @@ -44,7 +53,7 @@ void setup() MQTTManager::Init(gWallbox); } - Serial.println("Setup complete"); + Logger::Info("Setup complete"); } void loop()