1
1
#include " MQTTWidget.h"
2
2
#include " MQTTTranslations.h"
3
+ #include < ArduinoLog.h>
3
4
4
5
// Initialize the static instance pointer
5
6
MQTTWidget *MQTTWidget::instance = nullptr ;
@@ -108,23 +109,23 @@ uint16_t MQTTWidget::getColorFromString(const String &colorStr) {
108
109
109
110
// Setup method
110
111
void MQTTWidget::setup () {
111
- // Serial.println ("Inside setup method");
112
+ // Log.traceln ("Inside setup method");
112
113
/*
113
114
// Initialize MQTT connection
114
115
reconnect();
115
116
116
117
// Subscribe to the setup topic
117
118
if (mqttClient.connected()) {
118
119
mqttClient.subscribe(MQTT_SETUP_TOPIC);
119
- Serial.println ("Subscribed to setup topic1: " + String( MQTT_SETUP_TOPIC));
120
+ Log.traceln ("Subscribed to setup topic1: %s", MQTT_SETUP_TOPIC.c_str( ));
120
121
}
121
122
// Additional setup (e.g., initializing display elements) can be added here
122
123
*/
123
124
}
124
125
125
126
// Update method
126
127
void MQTTWidget::update (bool force) {
127
- // Serial.println ("Inside update method - " + String( mqttClient.connected()));
128
+ // Log.traceln ("Inside update method - %s " + mqttClient.connected().c_str( ));
128
129
129
130
if (!mqttClient.connected ()) {
130
131
reconnect ();
@@ -158,10 +159,7 @@ void MQTTWidget::callback(char *topic, byte *payload, unsigned int length) {
158
159
}
159
160
160
161
String receivedTopic = String (topic);
161
- Serial.print (" Message arrived [" );
162
- Serial.print (receivedTopic);
163
- Serial.print (" ]: " );
164
- Serial.println (message);
162
+ Log.traceln (" Message arrived [%s]: %s" , receivedTopic.c_str (), message.c_str ());
165
163
166
164
if (receivedTopic.equals (mqttSetupTopic.c_str ())) {
167
165
handleSetupMessage (message);
@@ -184,8 +182,7 @@ void MQTTWidget::callback(char *topic, byte *payload, unsigned int length) {
184
182
JsonDocument dataDoc;
185
183
DeserializationError dataError = deserializeJson (dataDoc, message);
186
184
if (dataError) {
187
- Serial.print (" Failed to parse data JSON: " );
188
- Serial.println (dataError.c_str ());
185
+ Log.errorln (" Failed to parse data JSON: %s" , dataError.c_str ());
189
186
return ;
190
187
}
191
188
@@ -208,7 +205,7 @@ void MQTTWidget::callback(char *topic, byte *payload, unsigned int length) {
208
205
if (fieldValue.is <JsonArray>()) {
209
206
fieldValue = fieldValue[index]; // Access the array element by index
210
207
} else {
211
- Serial. println (" Error: Expected an array for " + String ( token) );
208
+ Log. errorln (" Error: Expected an array for %s " , token);
212
209
return ;
213
210
}
214
211
} else {
@@ -238,47 +235,46 @@ void MQTTWidget::callback(char *topic, byte *payload, unsigned int length) {
238
235
239
236
// Update the display only if the value has actually changed
240
237
it->second = extractedValue;
241
- Serial. println (" Parsed " + orb->jsonField + " : " + extractedValue);
238
+ Log. traceln (" Parsed %s : %s " , orb->jsonField . c_str (), extractedValue. c_str () );
242
239
243
240
// Redraw the orb with updated data
244
241
drawOrb (orb->orbid );
245
242
} else {
246
- Serial. println (" No change detected for field: " + orb->jsonField );
243
+ Log. traceln (" No change detected for field: %s " , orb->jsonField . c_str () );
247
244
}
248
245
} else {
249
- Serial. println (" JSON field '" + orb-> jsonField + " ' not found in payload." );
246
+ Log. warningln (" JSON field '%s ' not found in payload." , orb-> jsonField . c_str () );
250
247
return ;
251
248
}
252
249
} else {
253
250
// The orb does not expect a JSON field; use the entire payload
254
251
if (it->second != message) {
255
252
it->second = message;
256
- Serial. println (" Updated data for " + receivedTopic + " : " + message);
253
+ Log. traceln (" Updated data for %s : %s " , receivedTopic. c_str (), message. c_str () );
257
254
drawOrb (orb->orbid );
258
255
} else {
259
- Serial. println (" No change detected for topic: " + receivedTopic);
256
+ Log. traceln (" No change detected for topic: %s " , receivedTopic. c_str () );
260
257
}
261
258
}
262
259
} else {
263
- Serial. println (" No orb configuration found for topic: " + receivedTopic);
260
+ Log. warningln (" No orb configuration found for topic: %s " , receivedTopic. c_str () );
264
261
}
265
262
} else {
266
- Serial. println (" Received message for unknown topic: " + receivedTopic);
263
+ Log. traceln (" Received message for unknown topic: %s " , receivedTopic. c_str () );
267
264
}
268
265
}
269
266
}
270
267
271
268
// Handle setup message to configure orbs
272
269
void MQTTWidget::handleSetupMessage (const String &message) {
273
- // Serial.println ("Handling setup message...");
270
+ // Log.traceln ("Handling setup message...");
274
271
275
272
// Parse JSON configuration
276
273
JsonDocument doc;
277
274
DeserializationError error = deserializeJson (doc, message);
278
275
279
276
if (error) {
280
- Serial.print (" Failed to parse setup JSON: " );
281
- Serial.println (error.c_str ());
277
+ Log.errorln (" Failed to parse setup JSON: %s" , error.c_str ());
282
278
return ;
283
279
}
284
280
@@ -314,7 +310,7 @@ void MQTTWidget::handleSetupMessage(const String &message) {
314
310
config.orbTextColor = getColorFromString (textColorStr);
315
311
316
312
orbConfigs.push_back (config);
317
- Serial. println (" Configured Orb: " + String (config. orbid ) + " -> " + config.orbdesc );
313
+ Log. infoln (" Configured Orb: %d -> %s " , config. orbid , config.orbdesc . c_str () );
318
314
319
315
// Initialize data map with empty strings
320
316
orbDataMap[config.topicSrc ] = " " ;
@@ -331,22 +327,22 @@ void MQTTWidget::handleSetupMessage(const String &message) {
331
327
332
328
// Subscribe to all orb topics
333
329
void MQTTWidget::subscribeToOrbs () {
334
- // Serial.println ("Inside subscribeToOrbs method");
330
+ // Log.traceln ("Inside subscribeToOrbs method");
335
331
336
332
for (const auto &orb : orbConfigs) {
337
333
bool success = mqttClient.subscribe (orb.topicSrc .c_str ());
338
334
if (success) {
339
- Serial. println (" Subscribed to topic: " + orb.topicSrc );
335
+ Log. traceln (" Subscribed to topic: %s " , orb.topicSrc . c_str () );
340
336
} else {
341
- Serial. println (" Failed to subscribe to topic: " + orb.topicSrc );
337
+ Log. warningln (" Failed to subscribe to topic: %s " , orb.topicSrc . c_str () );
342
338
}
343
339
}
344
340
}
345
341
346
342
#define RECONNECT_INTERVAL 5000
347
343
// Handle MQTT reconnection
348
344
void MQTTWidget::reconnect () {
349
- // Serial.println ("Inside reconnect method");
345
+ // Log.traceln ("Inside reconnect method");
350
346
351
347
// Loop until reconnected
352
348
if (!mqttClient.connected ()) {
@@ -356,7 +352,7 @@ void MQTTWidget::reconnect() {
356
352
return ;
357
353
}
358
354
359
- Serial. println (" Attempting MQTT connection..." );
355
+ Log. traceln (" Attempting MQTT connection..." );
360
356
lastReconnectAttempt = now;
361
357
362
358
// Generate a random client ID
@@ -369,33 +365,32 @@ void MQTTWidget::reconnect() {
369
365
if (!mqttUser.empty () && !mqttPass.empty ()) {
370
366
// Attempt to connect with username and password
371
367
connected = mqttClient.connect (clientId.c_str (), mqttUser.c_str (), mqttPass.c_str ());
372
- Serial. println (" Attempting MQTT connection with authentication..." );
368
+ Log. traceln (" Attempting MQTT connection with authentication..." );
373
369
} else {
374
370
// Attempt to connect without authentication
375
371
connected = mqttClient.connect (clientId.c_str ());
376
- Serial. println (" Attempting MQTT connection without authentication..." );
372
+ Log. traceln (" Attempting MQTT connection without authentication..." );
377
373
}
378
374
379
375
// Check the result of the connection attempt
380
376
if (connected) {
381
- Serial. println (" MQTT connected" );
377
+ Log. traceln (" MQTT connected" );
382
378
// Once connected, subscribe to the setup topic
383
379
if (mqttClient.subscribe (mqttSetupTopic.c_str ())) {
384
- Serial. println (" Subscribed to setup topic2: " + String ( mqttSetupTopic.c_str () ));
380
+ Log. traceln (" Subscribed to setup topic2: %s " , mqttSetupTopic.c_str ());
385
381
} else {
386
- Serial. println (" Failed to subscribe to setup topic: " + String ( mqttSetupTopic.c_str () ));
382
+ Log. warningln (" Failed to subscribe to setup topic: %s " , mqttSetupTopic.c_str ());
387
383
}
388
384
} else {
389
- Serial.print (" failed, rc=" );
390
- Serial.print (mqttClient.state ());
391
- Serial.println (" try again in 5 seconds" );
385
+ Log.warningln (" failed, rc=%d" , mqttClient.state ());
386
+ Log.warningln (" try again in 5 seconds" );
392
387
}
393
388
}
394
389
}
395
390
396
391
// New method to draw a single orb based on orbid
397
392
void MQTTWidget::drawOrb (int orbid) {
398
- // Serial.println ("Inside drawOrb method");
393
+ // Log.traceln ("Inside drawOrb method");
399
394
400
395
// Select the screen corresponding to the orbid
401
396
m_manager.selectScreen (orbid);
@@ -410,7 +405,7 @@ void MQTTWidget::drawOrb(int orbid) {
410
405
}
411
406
412
407
if (orb == nullptr ) {
413
- Serial. println (" Orb not found for orbid: " + String ( orbid) );
408
+ Log. warningln (" Orb not found for orbid: %d " , orbid);
414
409
return ;
415
410
}
416
411
0 commit comments