improve debug

This commit is contained in:
2025-08-29 11:46:17 +02:00
parent b2ee44dd60
commit d630a7ea5f
6 changed files with 55 additions and 19 deletions

30
include/debugutil.hpp Normal file
View File

@@ -0,0 +1,30 @@
#pragma once
#include <Arduino.h>
#ifndef ENABLE_SERIAL_DEBUG
#define ENABLE_SERIAL_DEBUG 0
#endif
struct Debug {
static constexpr bool enabled = static_cast<bool>(ENABLE_SERIAL_DEBUG);
static inline void begin(unsigned long baud) {
if constexpr (enabled) Serial.begin(baud);
}
template<typename... Args>
static inline void print(Args&&... args) {
if constexpr (enabled) { (Serial.print(std::forward<Args>(args)), ...); }
}
template<typename... Args>
static inline void println(Args&&... args) {
if constexpr (enabled) {
(Serial.print(std::forward<Args>(args)), ...);
Serial.println();
}
}
template<typename... Args>
static inline void printf(const char *fmt, Args&&... args) {
if constexpr (enabled) {
Serial.printf(fmt, std::forward<Args>(args)...);
}
}
};

View File

@@ -1,4 +1,5 @@
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include "debugutil.hpp"
#include "light.h" #include "light.h"
#include "mqtt.h" #include "mqtt.h"
@@ -166,11 +167,11 @@ void Light::subscribeToMqttTopics() {
} }
void Light::handleCommand(const std::string& command) { void Light::handleCommand(const std::string& command) {
Serial.println("Received command: " + String(command.c_str())); Debug::println("Received command: " + String(command.c_str()));
JsonDocument commandJson; JsonDocument commandJson;
deserializeJson(commandJson, command); deserializeJson(commandJson, command);
if (commandJson.isNull()) { if (commandJson.isNull()) {
Serial.println("Invalid command JSON"); Debug::println("Invalid command JSON");
return; return;
} }
if (commandJson["state"].is<String>()) { if (commandJson["state"].is<String>()) {
@@ -240,11 +241,11 @@ void Light::publishCurrentState() {
std::string stateJson; std::string stateJson;
serializeJson(stateInfo, stateJson); serializeJson(stateInfo, stateJson);
Serial.println("Publishing current state: " + String(stateJson.c_str())); Debug::println("Publishing current state: " + String(stateJson.c_str()));
Mqtt::publish(lightInfo.stateTopic, stateJson); Mqtt::publish(lightInfo.stateTopic, stateJson);
std::string attributeJson; std::string attributeJson;
serializeJson(attributeInfo, attributeJson); serializeJson(attributeInfo, attributeJson);
Serial.println("Publishing current attributes: " + String(attributeJson.c_str())); Debug::println("Publishing current attributes: " + String(attributeJson.c_str()));
Mqtt::publish(lightInfo.jsonAttributesTopic, attributeJson); Mqtt::publish(lightInfo.jsonAttributesTopic, attributeJson);
} }

View File

@@ -1,29 +1,30 @@
#include <ArduinoOTA.h> #include <ArduinoOTA.h>
#include "debugutil.hpp"
#include "ota.h" #include "ota.h"
OTAHandler::OTAHandler(std::string_view hostname) { OTAHandler::OTAHandler(std::string_view hostname) {
ArduinoOTA.setHostname(hostname.data()); ArduinoOTA.setHostname(hostname.data());
ArduinoOTA.onStart([]() { ArduinoOTA.onStart([]() {
Serial.println("OTA Start"); Debug::println("OTA Start");
}); });
ArduinoOTA.onEnd([]() { ArduinoOTA.onEnd([]() {
Serial.println("OTA End"); Debug::println("OTA End");
}); });
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("OTA Progress: %u%%\n", (progress / (total / 100))); Debug::printf("OTA Progress: %u%%\n", (progress / (total / 100)));
}); });
ArduinoOTA.onError([](ota_error_t error) { ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("OTA Error[%u]: ", error); Debug::printf("OTA Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) { if (error == OTA_AUTH_ERROR) {
Serial.println("Auth Failed"); Debug::println("Auth Failed");
} else if (error == OTA_BEGIN_ERROR) { } else if (error == OTA_BEGIN_ERROR) {
Serial.println("Begin Failed"); Debug::println("Begin Failed");
} else if (error == OTA_CONNECT_ERROR) { } else if (error == OTA_CONNECT_ERROR) {
Serial.println("Connect Failed"); Debug::println("Connect Failed");
} else if (error == OTA_RECEIVE_ERROR) { } else if (error == OTA_RECEIVE_ERROR) {
Serial.println("Receive Failed"); Debug::println("Receive Failed");
} else if (error == OTA_END_ERROR) { } else if (error == OTA_END_ERROR) {
Serial.println("End Failed"); Debug::println("End Failed");
} }
}); });
ArduinoOTA.begin(); ArduinoOTA.begin();

View File

@@ -15,8 +15,8 @@ framework = arduino
monitor_speed = 115200 monitor_speed = 115200
build_unflags = -std=gnu++11 build_unflags = -std=gnu++11
build_flags = build_flags =
-std=c++17
-std=gnu++17 -std=gnu++17
-I include
lib_deps = lib_deps =
martinverges/ESP32 Wifi Manager@^1.5.0 martinverges/ESP32 Wifi Manager@^1.5.0
esp32async/ESPAsyncWebServer@^3.7.10 esp32async/ESPAsyncWebServer@^3.7.10
@@ -25,6 +25,9 @@ lib_deps =
arkhipenko/TaskScheduler@^3.8.5 arkhipenko/TaskScheduler@^3.8.5
[env:esp32dev-serial] [env:esp32dev-serial]
build_flags =
${env.build_flags}
-D ENABLE_SERIAL_DEBUG=1
[env:esp32dev-ota] [env:esp32dev-ota]
upload_protocol = espota upload_protocol = espota

View File

@@ -1,6 +1,7 @@
#include <Arduino.h> #include <Arduino.h>
#include "appcontext.hpp" #include "appcontext.hpp"
#include "config.h" #include "config.h"
#include "debugutil.hpp"
#include "light.h" #include "light.h"
#include "mqtt.h" #include "mqtt.h"
#include "network.h" #include "network.h"
@@ -41,9 +42,8 @@ Scheduler *scheduler = nullptr;
void initializeScheduler(); void initializeScheduler();
void setup() { void setup() {
// put your setup code here, to run once: Debug::begin(115200);
Serial.begin(115200); Debug::println("Starting Smart RGB ESP32...");
Serial.println("Starting Smart RGB ESP32...");
stateMachine = new StateMachine<maxNumberOfStates>(); stateMachine = new StateMachine<maxNumberOfStates>();
initializeScheduler(); initializeScheduler();
appContext->pinR = pinR; appContext->pinR = pinR;

View File

@@ -1,6 +1,7 @@
#include <Arduino.h> #include <Arduino.h>
#include "appcontext.hpp" #include "appcontext.hpp"
#include "config.h" #include "config.h"
#include "debugutil.hpp"
#include "mqtt.h" #include "mqtt.h"
#include "pin.h" #include "pin.h"
#include "statemachine.hpp" #include "statemachine.hpp"
@@ -51,7 +52,7 @@ public:
NetworkInitializeState(AppContext *appContext) : State("NetworkInitializeState", StateId::NetworkInitializeState), appContext(appContext) {} NetworkInitializeState(AppContext *appContext) : State("NetworkInitializeState", StateId::NetworkInitializeState), appContext(appContext) {}
void onEnter(StateMachineBase &stateMachine) override { void onEnter(StateMachineBase &stateMachine) override {
Serial.println("Entering NetworkInitializeState"); Debug::println("Entering NetworkInitializeState");
if (appContext && !appContext->network) { if (appContext && !appContext->network) {
appContext->network = new Network(hostName, friendlyName); appContext->network = new Network(hostName, friendlyName);
} else if (appContext && appContext->network) { } else if (appContext && appContext->network) {
@@ -91,7 +92,7 @@ public:
} }
void onExit(StateMachineBase &stateMachine) override { void onExit(StateMachineBase &stateMachine) override {
Serial.println("Exiting RunningState"); Debug::println("Exiting RunningState");
} }
void onUpdate(StateMachineBase &stateMachine) override { void onUpdate(StateMachineBase &stateMachine) override {