Skip to content

Commit b15d349

Browse files
authored
Merge pull request #471 from dbambus/main
Bugfixes for MQTT with documentation of code
2 parents 85b4b50 + 8131c28 commit b15d349

File tree

7 files changed

+221
-119
lines changed

7 files changed

+221
-119
lines changed

include/led.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class Led {
5050
//------------------------------------------------------------------------------
5151
// Pixel set Functions
5252
//------------------------------------------------------------------------------
53-
void setState(bool newState);
53+
void setState(const bool newState);
5454
void setPixel(uint16_t ledIndex, HsbColor color);
5555
void setPixel(uint8_t row, uint8_t col, HsbColor color);
5656
void setbyFrontMatrix(ColorPosition position = Foreground,

include/led.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ void Led::shiftColumnToRight() {
203203
// Pixel set Functions
204204
//------------------------------------------------------------------------------
205205

206-
void Led::setState(bool newState) {
206+
void Led::setState(const bool newState) {
207207
static bool firstRun = true;
208208
static uint8_t oldBrightness[3];
209209

include/mqtt.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ class Mqtt {
66
private:
77
void reInit();
88
static void callback(char *topic, byte *payload, unsigned int length);
9+
static void processState(const JsonDocument &doc);
10+
static void processEffect(const JsonDocument &doc);
11+
static void processScrollingText(const JsonDocument &doc);
12+
static void processColor(const JsonDocument &doc);
13+
static void processBrightness(const JsonDocument &doc);
914

1015
public:
1116
Mqtt() = default;

include/mqtt.hpp

Lines changed: 209 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,165 @@ extern WiFiClient client;
1616

1717
PubSubClient mqttClient(client);
1818

19+
// ToDo : MQTT Notify https: // www.home-assistant.io/integrations/notify.mqtt/
20+
21+
//------------------------------------------------------------------------------
22+
// Helper Functions
23+
//------------------------------------------------------------------------------
24+
25+
/* Description:
26+
27+
This function processes the "state" key in the provided JSON document. If the
28+
"state" key is present, it checks its value and sets the LED state accordingly.
29+
The LED can be turned ON or OFF based on the value of the "state" key.
30+
31+
Input:
32+
33+
const JsonDocument &doc: The JSON document containing the "state" key.
34+
35+
Output:
36+
37+
None
38+
*/
39+
40+
void Mqtt::processState(const JsonDocument &doc) {
41+
if (doc.containsKey("state")) {
42+
const char *state = doc["state"];
43+
if (!strcmp(state, "ON")) {
44+
Serial.println("ON");
45+
led.setState(true);
46+
} else if (!strcmp(state, "OFF")) {
47+
Serial.println("OFF");
48+
led.setState(false);
49+
}
50+
}
51+
}
52+
53+
//------------------------------------------------------------------------------
54+
55+
/* Description:
56+
57+
This function processes the "effect" key in the provided JSON document. If the
58+
"effect" key is present, it checks its value and sets the program mode
59+
accordingly. The program mode can be one of several predefined modes such as
60+
Wordclock, Seconds, Digitalclock, etc.
61+
62+
Input:
63+
64+
const JsonDocument &doc: The JSON document containing the "effect" key.
65+
66+
Output:
67+
68+
None
69+
*/
70+
71+
void Mqtt::processEffect(const JsonDocument &doc) {
72+
if (doc.containsKey("effect")) {
73+
const char *effect = doc["effect"];
74+
if (!strcmp("Wordclock", effect)) {
75+
G.prog = COMMAND_MODE_WORD_CLOCK;
76+
parametersChanged = true;
77+
} else if (!strcmp("Seconds", effect)) {
78+
G.prog = COMMAND_MODE_SECONDS;
79+
} else if (!strcmp("Digitalclock", effect)) {
80+
G.prog = COMMAND_MODE_DIGITAL_CLOCK;
81+
parametersChanged = true;
82+
} else if (!strcmp("Scrollingtext", effect)) {
83+
G.prog = COMMAND_MODE_SCROLLINGTEXT;
84+
} else if (!strcmp("Rainbowcycle", effect)) {
85+
G.prog = COMMAND_MODE_RAINBOWCYCLE;
86+
} else if (!strcmp("Rainbow", effect)) {
87+
G.prog = COMMAND_MODE_RAINBOW;
88+
} else if (!strcmp("Color", effect)) {
89+
G.prog = COMMAND_MODE_COLOR;
90+
parametersChanged = true;
91+
} else if (!strcmp("Symbol", effect)) {
92+
G.prog = COMMAND_MODE_SYMBOL;
93+
}
94+
G.progInit = true;
95+
}
96+
}
97+
98+
//------------------------------------------------------------------------------
99+
100+
/* Description:
101+
102+
This function processes the "scrolling_text" key in the provided JSON document.
103+
If the "scrolling_text" key is present, it copies its value to the global
104+
scrolling text buffer.
105+
106+
Input:
107+
108+
const JsonDocument &doc: The JSON document containing the "scrolling_text" key.
109+
110+
Output:
111+
112+
None
113+
*/
114+
115+
void Mqtt::processScrollingText(const JsonDocument &doc) {
116+
if (doc.containsKey("scrolling_text")) {
117+
strcpy(G.scrollingText, doc["scrolling_text"]);
118+
}
119+
}
120+
121+
//------------------------------------------------------------------------------
122+
123+
/* Description:
124+
125+
This function processes the "color" key in the provided JSON document. If the
126+
"color" key is present, it updates the foreground color based on the hue (h) and
127+
saturation (s) values provided in the JSON document. The brightness component of
128+
the color remains unchanged.
129+
130+
Input:
131+
132+
const JsonDocument &doc: The JSON document containing the "color" key.
133+
134+
Output:
135+
136+
None
137+
*/
138+
139+
void Mqtt::processColor(const JsonDocument &doc) {
140+
JsonObjectConst color = doc["color"];
141+
if (!color.isNull()) {
142+
G.color[Foreground] =
143+
HsbColor(float(color["h"]) / 360.f, float(color["s"]) / 100.f,
144+
G.color[Foreground].B);
145+
parametersChanged = true;
146+
}
147+
}
148+
149+
//------------------------------------------------------------------------------
150+
151+
/* Description:
152+
153+
This function processes the "brightness" key in the provided JSON document. If
154+
the "brightness" key is present, it updates the brightness of the foreground
155+
color based on the value associated with the "brightness" key. The brightness
156+
value is expected to be in the range of 0 to 255 and is normalized to a float
157+
between 0 and 1. The function also sets a flag indicating that parameters have
158+
changed.
159+
160+
Input:
161+
162+
const JsonDocument &doc: The JSON document containing the "brightness" key.
163+
164+
Output:
165+
166+
None
167+
*/
168+
169+
void Mqtt::processBrightness(const JsonDocument &doc) {
170+
if (doc.containsKey("brightness")) {
171+
G.color[Foreground] =
172+
HsbColor(G.color[Foreground].H, G.color[Foreground].S,
173+
uint8_t(doc["brightness"]) / 255.f);
174+
parametersChanged = true;
175+
}
176+
}
177+
19178
//------------------------------------------------------------------------------
20179

21180
/* Description:
@@ -182,8 +341,8 @@ None
182341
void Mqtt::callback(char *topic, byte *payload, unsigned int length) {
183342
StaticJsonDocument<512> doc;
184343

185-
char msg[length + 1];
186344
// Convert payload to a null-terminated string
345+
char msg[length + 1];
187346
memcpy(msg, payload, length);
188347
msg[length] = '\0';
189348

@@ -193,67 +352,18 @@ void Mqtt::callback(char *topic, byte *payload, unsigned int length) {
193352

194353
// Deserialize JSON
195354
DeserializationError error = deserializeJson(doc, msg);
196-
197355
if (error) {
198356
Serial.print(F("deserializeJson() failed: "));
199357
Serial.println(error.c_str());
200358
return;
201359
}
202360

203361
// Process received JSON data
204-
if (doc.containsKey("state")) {
205-
const char *state = doc["state"];
206-
if (!strcmp(state, "ON")) {
207-
Serial.println("ON");
208-
led.setState(true);
209-
} else if (!strcmp(state, "OFF")) {
210-
Serial.println("OFF");
211-
led.setState(false);
212-
}
213-
}
214-
215-
const char *effect = doc["effect"];
216-
if (doc.containsKey("effect")) {
217-
if (!strcmp("Wordclock", effect)) {
218-
G.prog = COMMAND_MODE_WORD_CLOCK;
219-
} else if (!strcmp("Seconds", effect)) {
220-
G.prog = COMMAND_MODE_SECONDS;
221-
} else if (!strcmp("Digitalclock", effect)) {
222-
G.prog = COMMAND_MODE_DIGITAL_CLOCK;
223-
} else if (!strcmp("Scrollingtext", effect)) {
224-
G.prog = COMMAND_MODE_SCROLLINGTEXT;
225-
} else if (!strcmp("Rainbowcycle", effect)) {
226-
G.prog = COMMAND_MODE_RAINBOWCYCLE;
227-
} else if (!strcmp("Rainbow", effect)) {
228-
G.prog = COMMAND_MODE_RAINBOW;
229-
} else if (!strcmp("Color", effect)) {
230-
G.prog = COMMAND_MODE_COLOR;
231-
} else if (!strcmp("Symbol", effect)) {
232-
G.prog = COMMAND_MODE_SYMBOL;
233-
}
234-
}
235-
236-
// Copy marquee_text if present
237-
if (doc.containsKey("marquee_text")) {
238-
strcpy(G.scrollingText, doc["marquee_text"]);
239-
}
240-
241-
// Update color if present
242-
JsonObject color = doc["color"];
243-
if (!color.isNull()) {
244-
G.color[Foreground] =
245-
HsbColor(float(color["h"]) / 360.f, float(color["s"]) / 100.f,
246-
G.color[Foreground].B);
247-
parametersChanged = true;
248-
}
249-
250-
// Update brightness if present
251-
if (doc.containsKey("brightness")) {
252-
G.color[Foreground] =
253-
HsbColor(G.color[Foreground].H, G.color[Foreground].S,
254-
uint8_t(doc["brightness"]) / 255.f);
255-
parametersChanged = true;
256-
}
362+
processState(doc);
363+
processEffect(doc);
364+
processScrollingText(doc);
365+
processColor(doc);
366+
processBrightness(doc);
257367
}
258368

259369
//------------------------------------------------------------------------------
@@ -293,57 +403,57 @@ void Mqtt::sendState() {
293403

294404
//------------------------------------------------------------------------------
295405

296-
void Mqtt::sendDiscovery() {
297-
298-
/* Description:
406+
/* Description:
299407
300-
This function publishes MQTT discovery messages for Home Assistant,
301-
providing configuration details for a light entity. It constructs a JSON
302-
payload according to Home Assistant's MQTT discovery format and
303-
publishes it to the appropriate topic.
408+
This function publishes MQTT discovery messages for Home Assistant,
409+
providing configuration details for a light entity. It constructs a JSON
410+
payload according to Home Assistant's MQTT discovery format and
411+
publishes it to the appropriate topic.
304412
305-
Input:
413+
Input:
306414
307-
None
308-
Output:
415+
None
416+
Output:
309417
310-
None
311-
*/
418+
None
419+
*/
312420

313-
/* Example MQTT Message
314-
{
315-
"brightness": true,
316-
"color_mode": true,
317-
"supported_color_modes": [
318-
"hs"
421+
/* Example MQTT Message
422+
{
423+
"brightness": true,
424+
"color_mode": true,
425+
"supported_color_modes": [
426+
"hs"
427+
],
428+
"schema": "json",
429+
"name": "ESP",
430+
"device": {
431+
"identifiers": [
432+
"ESPBuro"
319433
],
320-
"schema": "json",
321434
"name": "ESP",
322-
"device": {
323-
"identifiers": [
324-
"ESPBuro"
325-
],
326-
"name": "ESP",
327-
"sw_version": "3.3",
328-
"configuration_url": "http://<IP-Adress>"
329-
},
330-
"state_topic": "ESPBuro/status",
331-
"command_topic": "ESPBuro/cmd",
332-
"unique_id": "<MAC-Adress>",
333-
"plattform": "mqtt",
334-
"effect": true,
335-
"effect_list": [
336-
"Wordclock",
337-
"Seconds",
338-
"Digitalclock",
339-
"Scrollingtext",
340-
"Rainbowcycle",
341-
"Rainbow",
342-
"Color",
343-
"Symbol"
344-
]
345-
}
346-
*/
435+
"sw_version": "3.3",
436+
"configuration_url": "http://<IP-Adress>"
437+
},
438+
"state_topic": "ESPBuro/status",
439+
"command_topic": "ESPBuro/cmd",
440+
"unique_id": "<MAC-Adress>",
441+
"plattform": "mqtt",
442+
"effect": true,
443+
"effect_list": [
444+
"Wordclock",
445+
"Seconds",
446+
"Digitalclock",
447+
"Scrollingtext",
448+
"Rainbowcycle",
449+
"Rainbow",
450+
"Color",
451+
"Symbol"
452+
]
453+
}
454+
*/
455+
456+
void Mqtt::sendDiscovery() {
347457

348458
StaticJsonDocument<700> root;
349459
mqttClient.setBufferSize(700);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "multilayout-esp-wordclock",
3-
"version": "4.0.1",
3+
"version": "4.0.2",
44
"description": "For building a german layouted wordclock with an esp8266 module and WS2812/SK2812.",
55
"license": "BSD-3-Clause",
66
"contributors": [

0 commit comments

Comments
 (0)