replace Serial.print with logger

This commit is contained in:
cjkas 2026-03-24 20:28:06 +01:00
parent f38dcea1f1
commit a220b9ecc2
13 changed files with 397 additions and 562 deletions

View file

@ -2,12 +2,15 @@
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include <esp_task_wdt.h>
#include "esp_log.h"
#include "ConfigSettings.h"
#include "MQTT.h"
#include "Somfy.h"
#include "Network.h"
#include "Utils.h"
static const char *TAG = "MQTT";
WiFiClient tcpClient;
PubSubClient mqttClient(tcpClient);
@ -45,12 +48,7 @@ bool MQTTClass::loop() {
}
void MQTTClass::receive(const char *topic, byte*payload, uint32_t length) {
esp_task_wdt_reset(); // Make sure we do not reboot here.
Serial.print("MQTT Topic:");
Serial.print(topic);
Serial.print(" payload:");
for(uint32_t i=0; i<length; i++)
Serial.print((char)payload[i]);
Serial.println();
ESP_LOGD(TAG, "MQTT Topic:%s payload:%.*s", topic, (int)length, (const char *)payload);
// We need to start at the last slash in the data
uint8_t len = strlen(topic);
@ -97,14 +95,7 @@ void MQTTClass::receive(const char *topic, byte*payload, uint32_t length) {
for(uint8_t j = 0; j < length && j < sizeof(value); j++)
value[j] = payload[j];
Serial.print("MQTT type:[");
Serial.print(entityType);
Serial.print("] command:[");
Serial.print(command);
Serial.print("] entityId:");
Serial.print(entityId);
Serial.print(" value:");
Serial.println(value);
ESP_LOGD(TAG, "MQTT type:[%s] command:[%s] entityId:%s value:%s", entityType, command, entityId, value);
if(strncmp(entityType, "shades", sizeof(entityType)) == 0) {
SomfyShade* shade = somfy.getShadeById(atoi(entityId));
if (shade) {
@ -204,8 +195,7 @@ bool MQTTClass::connect() {
snprintf(lwtTopic, sizeof(lwtTopic), "%s/status", settings.MQTT.rootTopic);
esp_task_wdt_reset();
if(mqttClient.connect(this->clientId, settings.MQTT.username, settings.MQTT.password, lwtTopic, 0, true, "offline")) {
Serial.print("Successfully connected MQTT client ");
Serial.println(this->clientId);
ESP_LOGI(TAG, "Successfully connected MQTT client %s", this->clientId);
this->publish("status", "online", true);
this->publish("ipAddress", settings.IP.ip.toString().c_str(), true);
this->publish("host", settings.hostname, true);
@ -228,14 +218,13 @@ bool MQTTClass::connect() {
this->subscribe("groups/+/sunny/set");
this->subscribe("groups/+/windy/set");
mqttClient.setCallback(MQTTClass::receive);
Serial.println("MQTT Startup Completed");
ESP_LOGI(TAG, "MQTT Startup Completed");
esp_task_wdt_reset();
this->lastConnect = millis();
return true;
}
else {
Serial.print("MQTT Connection failed for: ");
Serial.println(mqttClient.state());
ESP_LOGE(TAG, "MQTT Connection failed for: %d", mqttClient.state());
this->lastConnect = millis();
return false;
}
@ -273,8 +262,7 @@ bool MQTTClass::unsubscribe(const char *topic) {
snprintf(top, sizeof(top), "%s/%s", settings.MQTT.rootTopic, topic);
else
strlcpy(top, topic, sizeof(top));
Serial.print("MQTT Unsubscribed from:");
Serial.println(top);
ESP_LOGD(TAG, "MQTT Unsubscribed from:%s", top);
return mqttClient.unsubscribe(top);
}
return true;
@ -287,8 +275,7 @@ bool MQTTClass::subscribe(const char *topic) {
snprintf(top, sizeof(top), "%s/%s", settings.MQTT.rootTopic, topic);
else
strlcpy(top, topic, sizeof(top));
Serial.print("MQTT Subscribed to:");
Serial.println(top);
ESP_LOGD(TAG, "MQTT Subscribed to:%s", top);
return mqttClient.subscribe(top);
}
return true;