From bd2f0a83aaf4aa521a9760615b6c2df67e9b05ba Mon Sep 17 00:00:00 2001 From: David Panusch Date: Sun, 27 Oct 2024 09:30:12 +0100 Subject: [PATCH 1/4] added const to led::newState() --- include/led.h | 2 +- include/led.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/led.h b/include/led.h index e8b89e77..287f144c 100644 --- a/include/led.h +++ b/include/led.h @@ -50,7 +50,7 @@ class Led { //------------------------------------------------------------------------------ // Pixel set Functions //------------------------------------------------------------------------------ - void setState(bool newState); + void setState(const bool newState); void setPixel(uint16_t ledIndex, HsbColor color); void setPixel(uint8_t row, uint8_t col, HsbColor color); void setbyFrontMatrix(ColorPosition position = Foreground, diff --git a/include/led.hpp b/include/led.hpp index cc598485..906dab96 100644 --- a/include/led.hpp +++ b/include/led.hpp @@ -203,7 +203,7 @@ void Led::shiftColumnToRight() { // Pixel set Functions //------------------------------------------------------------------------------ -void Led::setState(bool newState) { +void Led::setState(const bool newState) { static bool firstRun = true; static uint8_t oldBrightness[3]; From 30249aba1f46e87e03373f9a17a270bf83aa3130 Mon Sep 17 00:00:00 2001 From: David Panusch Date: Sun, 27 Oct 2024 10:38:21 +0100 Subject: [PATCH 2/4] Bugfixed function change on MQTT --- include/mqtt.h | 5 + include/mqtt.hpp | 308 ++++++++++++++++++++++++++++++++--------------- package.json | 2 +- 3 files changed, 215 insertions(+), 100 deletions(-) diff --git a/include/mqtt.h b/include/mqtt.h index de49ec3a..aad26b68 100644 --- a/include/mqtt.h +++ b/include/mqtt.h @@ -6,6 +6,11 @@ class Mqtt { private: void reInit(); static void callback(char *topic, byte *payload, unsigned int length); + static void processState(const JsonDocument& doc); + static void processEffect(const JsonDocument& doc); + static void processScrollingText(const JsonDocument& doc); + static void processColor(const JsonDocument& doc); + static void processBrightness(const JsonDocument& doc); public: Mqtt() = default; diff --git a/include/mqtt.hpp b/include/mqtt.hpp index b2a80ab6..b38ac676 100644 --- a/include/mqtt.hpp +++ b/include/mqtt.hpp @@ -16,6 +16,165 @@ extern WiFiClient client; PubSubClient mqttClient(client); +// ToDo : MQTT Notify https: // www.home-assistant.io/integrations/notify.mqtt/ + +//------------------------------------------------------------------------------ +// Helper Functions +//------------------------------------------------------------------------------ + +/* Description: + +This function processes the "state" key in the provided JSON document. If the +"state" key is present, it checks its value and sets the LED state accordingly. +The LED can be turned ON or OFF based on the value of the "state" key. + +Input: + +const JsonDocument &doc: The JSON document containing the "state" key. + +Output: + +None +*/ + +void Mqtt::processState(const JsonDocument &doc) { + if (doc.containsKey("state")) { + const char *state = doc["state"]; + if (!strcmp(state, "ON")) { + Serial.println("ON"); + led.setState(true); + } else if (!strcmp(state, "OFF")) { + Serial.println("OFF"); + led.setState(false); + } + } +} + +//------------------------------------------------------------------------------ + +/* Description: + +This function processes the "effect" key in the provided JSON document. If the +"effect" key is present, it checks its value and sets the program mode +accordingly. The program mode can be one of several predefined modes such as +Wordclock, Seconds, Digitalclock, etc. + +Input: + +const JsonDocument &doc: The JSON document containing the "effect" key. + +Output: + +None +*/ + +void Mqtt::processEffect(const JsonDocument &doc) { + if (doc.containsKey("effect")) { + const char *effect = doc["effect"]; + if (!strcmp("Wordclock", effect)) { + G.prog = COMMAND_MODE_WORD_CLOCK; + parametersChanged = true; + } else if (!strcmp("Seconds", effect)) { + G.prog = COMMAND_MODE_SECONDS; + } else if (!strcmp("Digitalclock", effect)) { + G.prog = COMMAND_MODE_DIGITAL_CLOCK; + parametersChanged = true; + } else if (!strcmp("Scrollingtext", effect)) { + G.prog = COMMAND_MODE_SCROLLINGTEXT; + } else if (!strcmp("Rainbowcycle", effect)) { + G.prog = COMMAND_MODE_RAINBOWCYCLE; + } else if (!strcmp("Rainbow", effect)) { + G.prog = COMMAND_MODE_RAINBOW; + } else if (!strcmp("Color", effect)) { + G.prog = COMMAND_MODE_COLOR; + parametersChanged = true; + } else if (!strcmp("Symbol", effect)) { + G.prog = COMMAND_MODE_SYMBOL; + } + G.progInit = true; + } +} + +//------------------------------------------------------------------------------ + +/* Description: + +This function processes the "scrolling_text" key in the provided JSON document. +If the "scrolling_text" key is present, it copies its value to the global +scrolling text buffer. + +Input: + +const JsonDocument &doc: The JSON document containing the "scrolling_text" key. + +Output: + +None +*/ + +void Mqtt::processScrollingText(const JsonDocument &doc) { + if (doc.containsKey("scrolling_text")) { + strcpy(G.scrollingText, doc["scrolling_text"]); + } +} + +//------------------------------------------------------------------------------ + +/* Description: + +This function processes the "color" key in the provided JSON document. If the +"color" key is present, it updates the foreground color based on the hue (h) and +saturation (s) values provided in the JSON document. The brightness component of +the color remains unchanged. + +Input: + +const JsonDocument &doc: The JSON document containing the "color" key. + +Output: + +None +*/ + +void Mqtt::processColor(const JsonDocument &doc) { + JsonObjectConst color = doc["color"]; + if (!color.isNull()) { + G.color[Foreground] = + HsbColor(float(color["h"]) / 360.f, float(color["s"]) / 100.f, + G.color[Foreground].B); + parametersChanged = true; + } +} + +//------------------------------------------------------------------------------ + +/* Description: + +This function processes the "brightness" key in the provided JSON document. If +the "brightness" key is present, it updates the brightness of the foreground +color based on the value associated with the "brightness" key. The brightness +value is expected to be in the range of 0 to 255 and is normalized to a float +between 0 and 1. The function also sets a flag indicating that parameters have +changed. + +Input: + +const JsonDocument &doc: The JSON document containing the "brightness" key. + +Output: + +None +*/ + +void Mqtt::processBrightness(const JsonDocument &doc) { + if (doc.containsKey("brightness")) { + G.color[Foreground] = + HsbColor(G.color[Foreground].H, G.color[Foreground].S, + uint8_t(doc["brightness"]) / 255.f); + parametersChanged = true; + } +} + //------------------------------------------------------------------------------ /* Description: @@ -182,8 +341,8 @@ None void Mqtt::callback(char *topic, byte *payload, unsigned int length) { StaticJsonDocument<512> doc; - char msg[length + 1]; // Convert payload to a null-terminated string + char msg[length + 1]; memcpy(msg, payload, length); msg[length] = '\0'; @@ -193,7 +352,6 @@ void Mqtt::callback(char *topic, byte *payload, unsigned int length) { // Deserialize JSON DeserializationError error = deserializeJson(doc, msg); - if (error) { Serial.print(F("deserializeJson() failed: ")); Serial.println(error.c_str()); @@ -201,59 +359,11 @@ void Mqtt::callback(char *topic, byte *payload, unsigned int length) { } // Process received JSON data - if (doc.containsKey("state")) { - const char *state = doc["state"]; - if (!strcmp(state, "ON")) { - Serial.println("ON"); - led.setState(true); - } else if (!strcmp(state, "OFF")) { - Serial.println("OFF"); - led.setState(false); - } - } - - const char *effect = doc["effect"]; - if (doc.containsKey("effect")) { - if (!strcmp("Wordclock", effect)) { - G.prog = COMMAND_MODE_WORD_CLOCK; - } else if (!strcmp("Seconds", effect)) { - G.prog = COMMAND_MODE_SECONDS; - } else if (!strcmp("Digitalclock", effect)) { - G.prog = COMMAND_MODE_DIGITAL_CLOCK; - } else if (!strcmp("Scrollingtext", effect)) { - G.prog = COMMAND_MODE_SCROLLINGTEXT; - } else if (!strcmp("Rainbowcycle", effect)) { - G.prog = COMMAND_MODE_RAINBOWCYCLE; - } else if (!strcmp("Rainbow", effect)) { - G.prog = COMMAND_MODE_RAINBOW; - } else if (!strcmp("Color", effect)) { - G.prog = COMMAND_MODE_COLOR; - } else if (!strcmp("Symbol", effect)) { - G.prog = COMMAND_MODE_SYMBOL; - } - } - - // Copy marquee_text if present - if (doc.containsKey("marquee_text")) { - strcpy(G.scrollingText, doc["marquee_text"]); - } - - // Update color if present - JsonObject color = doc["color"]; - if (!color.isNull()) { - G.color[Foreground] = - HsbColor(float(color["h"]) / 360.f, float(color["s"]) / 100.f, - G.color[Foreground].B); - parametersChanged = true; - } - - // Update brightness if present - if (doc.containsKey("brightness")) { - G.color[Foreground] = - HsbColor(G.color[Foreground].H, G.color[Foreground].S, - uint8_t(doc["brightness"]) / 255.f); - parametersChanged = true; - } + processState(doc); + processEffect(doc); + processScrollingText(doc); + processColor(doc); + processBrightness(doc); } //------------------------------------------------------------------------------ @@ -293,57 +403,57 @@ void Mqtt::sendState() { //------------------------------------------------------------------------------ -void Mqtt::sendDiscovery() { - - /* Description: +/* Description: - This function publishes MQTT discovery messages for Home Assistant, - providing configuration details for a light entity. It constructs a JSON - payload according to Home Assistant's MQTT discovery format and - publishes it to the appropriate topic. +This function publishes MQTT discovery messages for Home Assistant, +providing configuration details for a light entity. It constructs a JSON +payload according to Home Assistant's MQTT discovery format and +publishes it to the appropriate topic. - Input: +Input: - None - Output: +None +Output: - None - */ +None + */ - /* Example MQTT Message - { - "brightness": true, - "color_mode": true, - "supported_color_modes": [ - "hs" +/* Example MQTT Message +{ + "brightness": true, + "color_mode": true, + "supported_color_modes": [ + "hs" + ], + "schema": "json", + "name": "ESP", + "device": { + "identifiers": [ + "ESPBuro" ], - "schema": "json", "name": "ESP", - "device": { - "identifiers": [ - "ESPBuro" - ], - "name": "ESP", - "sw_version": "3.3", - "configuration_url": "http://" - }, - "state_topic": "ESPBuro/status", - "command_topic": "ESPBuro/cmd", - "unique_id": "", - "plattform": "mqtt", - "effect": true, - "effect_list": [ - "Wordclock", - "Seconds", - "Digitalclock", - "Scrollingtext", - "Rainbowcycle", - "Rainbow", - "Color", - "Symbol" - ] - } - */ + "sw_version": "3.3", + "configuration_url": "http://" + }, + "state_topic": "ESPBuro/status", + "command_topic": "ESPBuro/cmd", + "unique_id": "", + "plattform": "mqtt", + "effect": true, + "effect_list": [ + "Wordclock", + "Seconds", + "Digitalclock", + "Scrollingtext", + "Rainbowcycle", + "Rainbow", + "Color", + "Symbol" + ] +} +*/ + +void Mqtt::sendDiscovery() { StaticJsonDocument<700> root; mqttClient.setBufferSize(700); diff --git a/package.json b/package.json index 2b75e15c..6abf0033 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "multilayout-esp-wordclock", - "version": "4.0.1", + "version": "4.0.2", "description": "For building a german layouted wordclock with an esp8266 module and WS2812/SK2812.", "license": "BSD-3-Clause", "contributors": [ From 3efe532db60c058732ab855581b3a2a32d926ea2 Mon Sep 17 00:00:00 2001 From: David Panusch Date: Sun, 27 Oct 2024 10:40:33 +0100 Subject: [PATCH 3/4] Clang formated --- include/mqtt.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/include/mqtt.h b/include/mqtt.h index aad26b68..7850a0e1 100644 --- a/include/mqtt.h +++ b/include/mqtt.h @@ -6,11 +6,11 @@ class Mqtt { private: void reInit(); static void callback(char *topic, byte *payload, unsigned int length); - static void processState(const JsonDocument& doc); - static void processEffect(const JsonDocument& doc); - static void processScrollingText(const JsonDocument& doc); - static void processColor(const JsonDocument& doc); - static void processBrightness(const JsonDocument& doc); + static void processState(const JsonDocument &doc); + static void processEffect(const JsonDocument &doc); + static void processScrollingText(const JsonDocument &doc); + static void processColor(const JsonDocument &doc); + static void processBrightness(const JsonDocument &doc); public: Mqtt() = default; From 8131c2882f7fa325454d889bc77458ddb195f28f Mon Sep 17 00:00:00 2001 From: David Panusch Date: Sun, 27 Oct 2024 13:58:23 +0100 Subject: [PATCH 4/4] Some improvements for ESP32 --- platformio.ini | 4 ++-- src/Wortuhr.cpp | 17 ++--------------- 2 files changed, 4 insertions(+), 17 deletions(-) diff --git a/platformio.ini b/platformio.ini index 01c9c04a..57fcc593 100644 --- a/platformio.ini +++ b/platformio.ini @@ -30,7 +30,7 @@ lib_deps = extra_scripts = pre:extra_scripts.py [env:esp32dev] -platform = espressif32 +platform = espressif32 @ 6.9.0 board = esp32dev board_build.partitions = partitions_singleapp_large.csv framework = arduino @@ -44,7 +44,7 @@ build_flags = lib_deps = makuna/NeoPixelBus@^2.7.6 bblanchon/ArduinoJson@^6.17.2 - links2004/WebSockets@2.4.1 + links2004/WebSockets@^2.4.1 adafruit/RTClib@^1.11.2 knolleary/PubSubClient@^2.8.0 https://github.com/tzapu/WiFiManager#v2.0.17 diff --git a/src/Wortuhr.cpp b/src/Wortuhr.cpp index c2776fd6..4b6f1ef6 100644 --- a/src/Wortuhr.cpp +++ b/src/Wortuhr.cpp @@ -6,19 +6,20 @@ #include #include #include +#include #elif defined(ESP32) #include #include #include #include #include +#include #endif #include #include #include #include #include -#include #include "Uhr.h" @@ -89,20 +90,6 @@ uint16_t powerCycleCount = 0; // Variable to store power cycle count //------------------------------------------------------------------------------ -uint32_t sntp_startup_delay_MS_rfc_not_less_than_60000() { - if (externalRTC) { - Serial.println("SNTP startup delay 1min"); - return 60000; - } else { - // yes this is against the RFC, but we don't have an RTC and want the - // time now. - Serial.println("no RTC clock - disable SNTP startup delay"); - return 500; - } -} - -//------------------------------------------------------------------------------ - void time_is_set() { time_t utc = time(nullptr); if (externalRTC) {