Skip to content

Commit ba008e2

Browse files
committed
Better debug logging using debug
1 parent 4a37ad0 commit ba008e2

File tree

1 file changed

+14
-19
lines changed

1 file changed

+14
-19
lines changed

src/EasyMqtt.h

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,10 @@ class EasyMqtt : public MqttMap {
4242
timer++;
4343
}
4444
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());
5047
} else {
51-
#ifdef DEBUG
52-
Serial.println("WiFi Unable to connect");
53-
#endif
48+
debug("WiFi Unable to connect");
5449
delay(5000);
5550
}
5651

@@ -61,11 +56,8 @@ class EasyMqtt : public MqttMap {
6156
debug("Connected to MQTT");
6257
subscribe();
6358
} 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");
6961
delay(5000);
7062
}
7163
}
@@ -87,11 +79,17 @@ class EasyMqtt : public MqttMap {
8779
};
8880
}
8981

82+
void debug(String key, String value) {
83+
debug(key + " = " + value);
84+
}
85+
9086
void debug(String msg) {
9187
#ifdef DEBUG
9288
Serial.println(msg);
9389
#endif
94-
get("system")["debug"].publish(msg);
90+
if(mqttClient.connected()) {
91+
get("system")["debug"].publish(msg);
92+
}
9593
}
9694

9795
/**
@@ -135,11 +133,8 @@ class EasyMqtt : public MqttMap {
135133
mqtt_port = port;
136134
mqtt_username = username;
137135
mqtt_password = password;
138-
139-
#ifdef DEBUG
140-
Serial.print("Topic: ");
141-
Serial.println(getTopic());
142-
#endif
136+
137+
debug("Topic", getTopic());
143138
}
144139

145140
/**

0 commit comments

Comments
 (0)