Skip to content

Commit

Permalink
Logging additions
Browse files Browse the repository at this point in the history
  • Loading branch information
BorisBrock committed Oct 5, 2024
1 parent 055fa24 commit e5dfcea
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 87 deletions.
4 changes: 3 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ lib_deps =
[env:heidelberg]
build_flags =
-O2
-D DEBUGLOG_DEFAULT_LOG_LEVEL_ERROR

[env:dummy]
build_flags =
-O2
-D DUMMY_WALLBOX
-D DUMMY_WALLBOX
-D DEBUGLOG_DEFAULT_LOG_LEVEL_DEBUG
11 changes: 6 additions & 5 deletions src/Components/MQTT/MQTTManager.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <Arduino.h>
#include <DebugLog.h>
#include <WiFi.h>
#include <WiFi.h>
extern "C"
Expand Down Expand Up @@ -41,7 +42,7 @@ namespace MQTTManager
{
if (!gMqttClient.connected())
{
Serial.println("Connecting to MQTT...");
LOG_INFO("Connecting to MQTT broker...");
gMqttClient.connect();
}
}
Expand Down Expand Up @@ -112,7 +113,7 @@ namespace MQTTManager

void OnMqttConnect(bool sessionPresent)
{
Serial.println("Connected to MQTT");
LOG_INFO("Connected to MQTT");

// Subscribe to control topics
gMqttClient.subscribe(ChargingCurrentControl.c_str(), 2);
Expand All @@ -125,15 +126,15 @@ namespace MQTTManager

void OnMqttDisconnect(AsyncMqttClientDisconnectReason reason)
{
Serial.println("Disconnected from MQTT");
LOG_WARN("Disconnected from MQTT");
}

void OnMqttMessage(char *topic, char *payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total)
{
if (ChargingCurrentControl == topic)
{
float current = String(payload, len).toFloat();
Serial.printf("Received MQTT control command: charging current limit = %f\n", current);
LOG_TRACE("Received MQTT control command: charging current limit = %f\n", current);
gWallbox->SetChargingCurrentLimit(current);
}
}
Expand All @@ -145,7 +146,7 @@ namespace MQTTManager

void Init(IWallbox *wallbox)
{
Serial.println("Initializing MQTT");
LOG_INFO("Initializing MQTT");

gWallbox = wallbox;

Expand Down
51 changes: 26 additions & 25 deletions src/Components/Modbus/ModbusTCP.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <Arduino.h>
#include <DebugLog.h>
#include <WiFi.h>
#include "ModbusServerWiFi.h"
#include "../../Configuration/Constants.h"
Expand All @@ -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:");
LOG_TRACE("Modbus TCP request received: READ_INPUT_REGISTER:");
LOG_TRACE(" > Size: %d", msg.size());
LOG_TRACE(" > FC: %d", msg.getFunctionCode());
LOG_TRACE(" > Data:");
for (uint8_t i = 0; i < msg.size(); ++i)
{
Serial.printf(" > %d\n", msg[i]);
LOG_TRACE(" > %d", msg[i]);
}
return ModbusMessage{};
}
Expand All @@ -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);
LOG_TRACE("ModbusTCP request received: READ_HOLD_REGISTER %d words at reg. %d", numWords, startAddress);

// Respond properly
ModbusMessage response;
Expand All @@ -40,7 +41,7 @@ namespace ModbusTCP
{
case (Constants::DaheimladenRegisters::Status):
{
Serial.println(" -> Responding with wallbox status");
LOG_TRACE(" -> Responding with wallbox status");
responseLengthBytes = 2;
const uint16_t rawState = static_cast<uint16_t>(gWallbox->GetState());
response.add(request.getServerID(), fc, responseLengthBytes);
Expand All @@ -49,7 +50,7 @@ namespace ModbusTCP
}
case (Constants::DaheimladenRegisters::LimitChargingCurrent):
{
Serial.println(" -> Responding with charging current limit");
LOG_TRACE(" -> Responding with charging current limit");
responseLengthBytes = 2;
uint16_t rawCurrent = static_cast<uint16_t>(gWallbox->GetChargingCurrentLimit() * Constants::DaheimladenWallbox::CurrentFactor);
response.add(request.getServerID(), fc, responseLengthBytes);
Expand All @@ -58,7 +59,7 @@ namespace ModbusTCP
}
case (Constants::DaheimladenRegisters::ConnectionTimeoutTime):
{
Serial.println(" -> Responding with connection timeout time");
LOG_TRACE(" -> Responding with connection timeout time");
responseLengthBytes = 2;
const uint16_t dummyValue = 60;
response.add(request.getServerID(), fc, responseLengthBytes);
Expand All @@ -67,7 +68,7 @@ namespace ModbusTCP
}
case (Constants::DaheimladenRegisters::EnergyMeter):
{
Serial.println(" -> Responding with energy meter value");
LOG_TRACE(" -> Responding with energy meter value");
responseLengthBytes = 4;
const uint32_t rawEnergy01kWh = static_cast<uint32_t>(gWallbox->GetEnergyMeterValue() * Constants::DaheimladenWallbox::EnergyFactor);
response.add(request.getServerID(), fc, responseLengthBytes);
Expand All @@ -76,7 +77,7 @@ namespace ModbusTCP
}
case (Constants::DaheimladenRegisters::MaxChargingCurrentAfterConnectionLoss):
{
Serial.println(" -> Responding with connection loss current");
LOG_TRACE(" -> Responding with connection loss current");
responseLengthBytes = 2;
uint16_t rawCurrent = static_cast<uint16_t>(gWallbox->GetFailsafeCurrent() * Constants::DaheimladenWallbox::CurrentFactor);
response.add(request.getServerID(), fc, responseLengthBytes);
Expand All @@ -85,7 +86,7 @@ namespace ModbusTCP
}
case (Constants::DaheimladenRegisters::TotalChargingPower):
{
Serial.println(" -> Responding with total charging power");
LOG_TRACE(" -> Responding with total charging power");
responseLengthBytes = 4;
const uint32_t powerW = static_cast<uint32_t>(gWallbox->GetChargingPower());
response.add(request.getServerID(), fc, responseLengthBytes);
Expand All @@ -94,7 +95,7 @@ namespace ModbusTCP
}
case (Constants::DaheimladenRegisters::ChargeCurrents):
{
Serial.println(" -> Responding with charging currents");
LOG_TRACE(" -> Responding with charging currents");
responseLengthBytes = 12;

float c1, c2, c3;
Expand All @@ -111,7 +112,7 @@ namespace ModbusTCP
}
case (Constants::DaheimladenRegisters::ChargeVoltages):
{
Serial.println(" -> Responding with charging voltages");
LOG_TRACE(" -> Responding with charging voltages");
responseLengthBytes = 12;

float v1, v2, v3;
Expand All @@ -129,7 +130,7 @@ namespace ModbusTCP
case (Constants::DaheimladenRegisters::RfidStationId):
case (Constants::DaheimladenRegisters::RfidCardId):
{
Serial.println(" -> Responding with ID");
LOG_TRACE(" -> Responding with ID");
responseLengthBytes = 32;
const uint32_t dummyValue = 0;
response.add(request.getServerID(), fc, responseLengthBytes);
Expand All @@ -139,7 +140,7 @@ namespace ModbusTCP
}
default:
{
Serial.println(" -> Responding with error ILLEGAL_DATA_ADDRESS");
LOG_ERROR(" -> Responding with error ILLEGAL_DATA_ADDRESS");
response.setError(request.getServerID(), fc, Modbus::Error::ILLEGAL_DATA_ADDRESS);
break;
}
Expand All @@ -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:");
LOG_TRACE("Modbus TCP request received: WRITE_HOLD_REGISTER");
LOG_TRACE(" > Size: %d", msg.size());
LOG_TRACE(" > FC: %d", msg.getFunctionCode());
LOG_TRACE(" > Data:");
for (uint8_t i = 0; i < msg.size(); ++i)
{
Serial.printf(" > %d\n", msg[i]);
LOG_TRACE(" > %d", msg[i]);
}
return msg; // Echo back request
}
Expand All @@ -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);
LOG_TRACE("ModbusTCP request received: WRITE_MULT_REGISTERS %d words to reg. %d", numWords, startAddress);

// Respond properly
switch (startAddress)
Expand All @@ -178,13 +179,13 @@ namespace ModbusTCP
{
uint16_t currentLimit = 0;
request.get(7, currentLimit);
Serial.printf(" -> Writing charging current limit: %d\n", currentLimit);
LOG_TRACE(" -> Writing charging current limit: %d", currentLimit);
gWallbox->SetChargingCurrentLimit(static_cast<float>(currentLimit * 0.1f));
break;
}
default:
{
Serial.println(" -> Responding with error ILLEGAL_DATA_ADDRESS");
LOG_ERROR(" -> Responding with error ILLEGAL_DATA_ADDRESS");
ModbusMessage response;
response.setError(request.getServerID(), fc, Modbus::Error::ILLEGAL_DATA_ADDRESS);
return response;
Expand All @@ -200,7 +201,7 @@ namespace ModbusTCP

void Init(IWallbox *wallbox)
{
Serial.println("Initializing Modbus TCP server");
LOG_INFO("Initializing Modbus TCP server");

gWallbox = wallbox;

Expand Down
23 changes: 12 additions & 11 deletions src/Components/Wallbox/DummyWallbox.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <Arduino.h>
#include <DebugLog.h>
#include "../../Configuration/Constants.h"
#include "DummyWallbox.h"

Expand All @@ -10,57 +11,57 @@ DummyWallbox *DummyWallbox::Instance()

void DummyWallbox::Init()
{
Serial.println("Dummy wallbox: initializing");
LOG_DEBUG("Dummy wallbox: initializing");
}

VehicleState DummyWallbox::GetState()
{
if (mChargingCurrentLimitA > 0.0f)
{
Serial.println("Dummy wallbox: returning state CHARGING");
LOG_DEBUG("Dummy wallbox: returning state CHARGING");
return VehicleState::Charging;
}
else
{
Serial.println("Dummy wallbox: returning state CONNECTED");
LOG_DEBUG("Dummy wallbox: returning state CONNECTED");
return VehicleState::Connected;
}
}

bool DummyWallbox::SetChargingCurrentLimit(float currentLimitA)
{
mChargingCurrentLimitA = currentLimitA;
Serial.printf("Dummy wallbox: setting charging current limit to %f A\n", mChargingCurrentLimitA);
LOG_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);
LOG_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);
LOG_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);
LOG_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);
LOG_DEBUG("Dummy wallbox: returning charging power %f W", chargingPowerW);
return chargingPowerW;
}

Expand All @@ -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);
LOG_DEBUG("Dummy wallbox: returning charging currents %f, %f and %f A", c1A, c2A, c3A);
return true;
}

Expand All @@ -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);
LOG_DEBUG("Dummy wallbox: returning charging voltages %f, %f and %f A", v1V, v2V, v3V);
return true;
}

Expand Down
Loading

0 comments on commit e5dfcea

Please sign in to comment.