11#include " MQTTWidget.h"
22#include " MQTTTranslations.h"
3+ #include < ArduinoLog.h>
34
45// Initialize the static instance pointer
56MQTTWidget *MQTTWidget::instance = nullptr ;
@@ -108,23 +109,23 @@ uint16_t MQTTWidget::getColorFromString(const String &colorStr) {
108109
109110// Setup method
110111void MQTTWidget::setup () {
111- // Serial.println ("Inside setup method");
112+ // Log.traceln ("Inside setup method");
112113 /*
113114 // Initialize MQTT connection
114115 reconnect();
115116
116117 // Subscribe to the setup topic
117118 if (mqttClient.connected()) {
118119 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( ));
120121 }
121122 // Additional setup (e.g., initializing display elements) can be added here
122123 */
123124}
124125
125126// Update method
126127void MQTTWidget::update (bool force) {
127- // Serial.println ("Inside update method - " + String( mqttClient.connected()));
128+ // Log.traceln ("Inside update method - %s " + mqttClient.connected().c_str( ));
128129
129130 if (!mqttClient.connected ()) {
130131 reconnect ();
@@ -158,10 +159,7 @@ void MQTTWidget::callback(char *topic, byte *payload, unsigned int length) {
158159 }
159160
160161 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 ());
165163
166164 if (receivedTopic.equals (mqttSetupTopic.c_str ())) {
167165 handleSetupMessage (message);
@@ -184,8 +182,7 @@ void MQTTWidget::callback(char *topic, byte *payload, unsigned int length) {
184182 JsonDocument dataDoc;
185183 DeserializationError dataError = deserializeJson (dataDoc, message);
186184 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 ());
189186 return ;
190187 }
191188
@@ -208,7 +205,7 @@ void MQTTWidget::callback(char *topic, byte *payload, unsigned int length) {
208205 if (fieldValue.is <JsonArray>()) {
209206 fieldValue = fieldValue[index]; // Access the array element by index
210207 } else {
211- Serial. println (" Error: Expected an array for " + String ( token) );
208+ Log. errorln (" Error: Expected an array for %s " , token);
212209 return ;
213210 }
214211 } else {
@@ -238,47 +235,46 @@ void MQTTWidget::callback(char *topic, byte *payload, unsigned int length) {
238235
239236 // Update the display only if the value has actually changed
240237 it->second = extractedValue;
241- Serial. println (" Parsed " + orb->jsonField + " : " + extractedValue);
238+ Log. traceln (" Parsed %s : %s " , orb->jsonField . c_str (), extractedValue. c_str () );
242239
243240 // Redraw the orb with updated data
244241 drawOrb (orb->orbid );
245242 } else {
246- Serial. println (" No change detected for field: " + orb->jsonField );
243+ Log. traceln (" No change detected for field: %s " , orb->jsonField . c_str () );
247244 }
248245 } 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 () );
250247 return ;
251248 }
252249 } else {
253250 // The orb does not expect a JSON field; use the entire payload
254251 if (it->second != message) {
255252 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 () );
257254 drawOrb (orb->orbid );
258255 } else {
259- Serial. println (" No change detected for topic: " + receivedTopic);
256+ Log. traceln (" No change detected for topic: %s " , receivedTopic. c_str () );
260257 }
261258 }
262259 } else {
263- Serial. println (" No orb configuration found for topic: " + receivedTopic);
260+ Log. warningln (" No orb configuration found for topic: %s " , receivedTopic. c_str () );
264261 }
265262 } else {
266- Serial. println (" Received message for unknown topic: " + receivedTopic);
263+ Log. traceln (" Received message for unknown topic: %s " , receivedTopic. c_str () );
267264 }
268265 }
269266}
270267
271268// Handle setup message to configure orbs
272269void MQTTWidget::handleSetupMessage (const String &message) {
273- // Serial.println ("Handling setup message...");
270+ // Log.traceln ("Handling setup message...");
274271
275272 // Parse JSON configuration
276273 JsonDocument doc;
277274 DeserializationError error = deserializeJson (doc, message);
278275
279276 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 ());
282278 return ;
283279 }
284280
@@ -314,7 +310,7 @@ void MQTTWidget::handleSetupMessage(const String &message) {
314310 config.orbTextColor = getColorFromString (textColorStr);
315311
316312 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 () );
318314
319315 // Initialize data map with empty strings
320316 orbDataMap[config.topicSrc ] = " " ;
@@ -331,22 +327,22 @@ void MQTTWidget::handleSetupMessage(const String &message) {
331327
332328// Subscribe to all orb topics
333329void MQTTWidget::subscribeToOrbs () {
334- // Serial.println ("Inside subscribeToOrbs method");
330+ // Log.traceln ("Inside subscribeToOrbs method");
335331
336332 for (const auto &orb : orbConfigs) {
337333 bool success = mqttClient.subscribe (orb.topicSrc .c_str ());
338334 if (success) {
339- Serial. println (" Subscribed to topic: " + orb.topicSrc );
335+ Log. traceln (" Subscribed to topic: %s " , orb.topicSrc . c_str () );
340336 } else {
341- Serial. println (" Failed to subscribe to topic: " + orb.topicSrc );
337+ Log. warningln (" Failed to subscribe to topic: %s " , orb.topicSrc . c_str () );
342338 }
343339 }
344340}
345341
346342#define RECONNECT_INTERVAL 5000
347343// Handle MQTT reconnection
348344void MQTTWidget::reconnect () {
349- // Serial.println ("Inside reconnect method");
345+ // Log.traceln ("Inside reconnect method");
350346
351347 // Loop until reconnected
352348 if (!mqttClient.connected ()) {
@@ -356,7 +352,7 @@ void MQTTWidget::reconnect() {
356352 return ;
357353 }
358354
359- Serial. println (" Attempting MQTT connection..." );
355+ Log. traceln (" Attempting MQTT connection..." );
360356 lastReconnectAttempt = now;
361357
362358 // Generate a random client ID
@@ -369,33 +365,32 @@ void MQTTWidget::reconnect() {
369365 if (!mqttUser.empty () && !mqttPass.empty ()) {
370366 // Attempt to connect with username and password
371367 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..." );
373369 } else {
374370 // Attempt to connect without authentication
375371 connected = mqttClient.connect (clientId.c_str ());
376- Serial. println (" Attempting MQTT connection without authentication..." );
372+ Log. traceln (" Attempting MQTT connection without authentication..." );
377373 }
378374
379375 // Check the result of the connection attempt
380376 if (connected) {
381- Serial. println (" MQTT connected" );
377+ Log. traceln (" MQTT connected" );
382378 // Once connected, subscribe to the setup topic
383379 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 ());
385381 } 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 ());
387383 }
388384 } 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" );
392387 }
393388 }
394389}
395390
396391// New method to draw a single orb based on orbid
397392void MQTTWidget::drawOrb (int orbid) {
398- // Serial.println ("Inside drawOrb method");
393+ // Log.traceln ("Inside drawOrb method");
399394
400395 // Select the screen corresponding to the orbid
401396 m_manager.selectScreen (orbid);
@@ -410,7 +405,7 @@ void MQTTWidget::drawOrb(int orbid) {
410405 }
411406
412407 if (orb == nullptr ) {
413- Serial. println (" Orb not found for orbid: " + String ( orbid) );
408+ Log. warningln (" Orb not found for orbid: %d " , orbid);
414409 return ;
415410 }
416411
0 commit comments