@@ -16,6 +16,165 @@ extern WiFiClient client;
16
16
17
17
PubSubClient mqttClient (client);
18
18
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
+
19
178
// ------------------------------------------------------------------------------
20
179
21
180
/* Description:
182
341
void Mqtt::callback (char *topic, byte *payload, unsigned int length) {
183
342
StaticJsonDocument<512 > doc;
184
343
185
- char msg[length + 1 ];
186
344
// Convert payload to a null-terminated string
345
+ char msg[length + 1 ];
187
346
memcpy (msg, payload, length);
188
347
msg[length] = ' \0 ' ;
189
348
@@ -193,67 +352,18 @@ void Mqtt::callback(char *topic, byte *payload, unsigned int length) {
193
352
194
353
// Deserialize JSON
195
354
DeserializationError error = deserializeJson (doc, msg);
196
-
197
355
if (error) {
198
356
Serial.print (F (" deserializeJson() failed: " ));
199
357
Serial.println (error.c_str ());
200
358
return ;
201
359
}
202
360
203
361
// 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);
257
367
}
258
368
259
369
// ------------------------------------------------------------------------------
@@ -293,57 +403,57 @@ void Mqtt::sendState() {
293
403
294
404
// ------------------------------------------------------------------------------
295
405
296
- void Mqtt::sendDiscovery () {
297
-
298
- /* Description:
406
+ /* Description:
299
407
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.
304
412
305
- Input:
413
+ Input:
306
414
307
- None
308
- Output:
415
+ None
416
+ Output:
309
417
310
- None
311
- */
418
+ None
419
+ */
312
420
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"
319
433
],
320
- "schema": "json",
321
434
"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 () {
347
457
348
458
StaticJsonDocument<700 > root;
349
459
mqttClient.setBufferSize (700 );
0 commit comments