Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit e1647cc
Author: Boris Brock <[email protected]>
Date:   Sat Oct 5 07:29:17 2024 +0200

    Correction

commit bab7861
Author: Boris Brock <[email protected]>
Date:   Sat Oct 5 07:26:30 2024 +0200

    Improving startup info

commit 817ccbb
Author: Boris Brock <[email protected]>
Date:   Sat Oct 5 07:13:40 2024 +0200

    Introducing custom logger

commit b1a43d0
Author: Boris Brock <[email protected]>
Date:   Sat Oct 5 06:26:32 2024 +0200

    Fixing main info block

commit 948ec6f
Author: Boris Brock <[email protected]>
Date:   Sat Oct 5 06:20:54 2024 +0200

    Update ModbusRTU.cpp

commit e5dfcea
Author: Boris Brock <[email protected]>
Date:   Sat Oct 5 06:16:32 2024 +0200

    Logging additions

commit 055fa24
Author: Boris Brock <[email protected]>
Date:   Sat Oct 5 05:47:10 2024 +0200

    Integrating DebugLog
  • Loading branch information
BorisBrock committed Oct 5, 2024
1 parent c33dc2d commit 7c0e007
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 89 deletions.
4 changes: 3 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ lib_deps =
[env:heidelberg]
build_flags =
-O2
-D LOGGING_LEVEL_ERROR

[env:dummy]
build_flags =
-O2
-D DUMMY_WALLBOX
-D DUMMY_WALLBOX
-D LOGGING_LEVEL_DEBUG
22 changes: 22 additions & 0 deletions src/Components/Logger/Logger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <Arduino.h>
#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
}
};
63 changes: 63 additions & 0 deletions src/Components/Logger/Logger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#pragma once

namespace Logger
{
template <typename... Args>
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 <typename... Args>
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 <typename... Args>
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 <typename... Args>
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 <typename... Args>
void Trace(const char *format, Args... args)
{
#if defined(LOGGING_LEVEL_TRACE)
Serial.print("[TRACE] ");
Serial.printf(format, args...);
Serial.print("\n");
#endif
}

template <typename... Args>
void Print(const char *format, Args... args)
{
Serial.printf(format, args...);
Serial.print("\n");
}

const char *GetLogLevel();
};
11 changes: 6 additions & 5 deletions src/Components/MQTT/MQTTManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ extern "C"
#include "freertos/timers.h"
}
#include <AsyncMqttClient.h>
#include "../Logger/Logger.h"
#include "../../Configuration/Version.h"
#include "../../Configuration/Constants.h"
#include "../../Configuration/Credentials.h"
Expand Down Expand Up @@ -41,7 +42,7 @@ namespace MQTTManager
{
if (!gMqttClient.connected())
{
Serial.println("Connecting to MQTT...");
Logger::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");
Logger::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");
Logger::Warning("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);
Logger::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");
Logger::Info("Initializing MQTT");

gWallbox = wallbox;

Expand Down
12 changes: 5 additions & 7 deletions src/Components/Modbus/ModbusRTU.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <Arduino.h>
#include <WiFi.h>
#include "../Logger/Logger.h"
#include "HardwareSerial.h"
#include "ModbusClientRTU.h"
#include "../../Configuration/Pins.h"
Expand All @@ -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,
Expand All @@ -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
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand Down
51 changes: 26 additions & 25 deletions src/Components/Modbus/ModbusTCP.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <Arduino.h>
#include <WiFi.h>
#include "../Logger/Logger.h"
#include "ModbusServerWiFi.h"
#include "../../Configuration/Constants.h"
#include "../Wallbox/IWallbox.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:");
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{};
}
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);
Logger::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");
Logger::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");
Logger::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");
Logger::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");
Logger::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");
Logger::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");
Logger::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");
Logger::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");
Logger::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");
Logger::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");
Logger::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:");
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
}
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);
Logger::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);
Logger::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");
Logger::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");
Logger::Info("Initializing Modbus TCP server");

gWallbox = wallbox;

Expand Down
Loading

0 comments on commit 7c0e007

Please sign in to comment.