@@ -42,15 +42,10 @@ class EasyMqtt : public MqttMap {
42
42
timer++;
43
43
}
44
44
if (WiFi.status () == WL_CONNECTED) {
45
- #ifdef DEBUG
46
- Serial.println (" WiFi connected" );
47
- Serial.print (" * IP address: " );
48
- Serial.println (WiFi.localIP ());
49
- #endif
45
+ debug (" WiFi connected" );
46
+ debug (" * IP address" , WiFi.localIP ().toString ());
50
47
} else {
51
- #ifdef DEBUG
52
- Serial.println (" WiFi Unable to connect" );
53
- #endif
48
+ debug (" WiFi Unable to connect" );
54
49
delay (5000 );
55
50
}
56
51
@@ -61,11 +56,8 @@ class EasyMqtt : public MqttMap {
61
56
debug (" Connected to MQTT" );
62
57
subscribe ();
63
58
} else {
64
- #ifdef DEBUG
65
- Serial.print (" failed, rc=" );
66
- Serial.print (mqttClient.state ());
67
- Serial.println (" try again in 5 seconds" );
68
- #endif
59
+ debug (" Failed" , String (mqttClient.state ()));
60
+ debug (" Try again in 5 seconds" );
69
61
delay (5000 );
70
62
}
71
63
}
@@ -87,11 +79,17 @@ class EasyMqtt : public MqttMap {
87
79
};
88
80
}
89
81
82
+ void debug (String key, String value) {
83
+ debug (key + " = " + value);
84
+ }
85
+
90
86
void debug (String msg) {
91
87
#ifdef DEBUG
92
88
Serial.println (msg);
93
89
#endif
94
- get (" system" )[" debug" ].publish (msg);
90
+ if (mqttClient.connected ()) {
91
+ get (" system" )[" debug" ].publish (msg);
92
+ }
95
93
}
96
94
97
95
/* *
@@ -135,11 +133,8 @@ class EasyMqtt : public MqttMap {
135
133
mqtt_port = port;
136
134
mqtt_username = username;
137
135
mqtt_password = password;
138
-
139
- #ifdef DEBUG
140
- Serial.print (" Topic: " );
141
- Serial.println (getTopic ());
142
- #endif
136
+
137
+ debug (" Topic" , getTopic ());
143
138
}
144
139
145
140
/* *
0 commit comments