Commit 1c1e5c9c authored by Eric Duminil's avatar Eric Duminil
Browse files

More mqtt info

parent b87383a2
...@@ -59,7 +59,7 @@ namespace mqtt { ...@@ -59,7 +59,7 @@ namespace mqtt {
} }
void publish(const char *timestamp, int16_t co2, float temperature, float humidity) { void publish(const char *timestamp, int16_t co2, float temperature, float humidity) {
if (WiFi.status() == WL_CONNECTED && mqttClient.connected()) { if (wifi::connected() && mqttClient.connected()) {
led_effects::onBoardLEDOn(); led_effects::onBoardLEDOn();
Serial.print(F("MQTT - Publishing message ... ")); Serial.print(F("MQTT - Publishing message ... "));
...@@ -105,7 +105,7 @@ namespace mqtt { ...@@ -105,7 +105,7 @@ namespace mqtt {
// It failed less than wait_after_fail ago. Not even trying. // It failed less than wait_after_fail ago. Not even trying.
return; return;
} }
if (WiFi.status() != WL_CONNECTED) { //NOTE: Sadly, WiFi.status is sometimes WL_CONNECTED even though it's really not if (!wifi::connected()) { //NOTE: Sadly, WiFi.status is sometimes WL_CONNECTED even though it's really not
// No WIFI // No WIFI
return; return;
} }
...@@ -141,10 +141,23 @@ namespace mqtt { ...@@ -141,10 +141,23 @@ namespace mqtt {
Serial.println(F(" Connected.")); Serial.println(F(" Connected."));
last_failed_at = 0; last_failed_at = 0;
} else { } else {
char const *mqtt_statuses[] = { // As defined in PubSubClient
"Connection timeout", // -4
"Connection lost",
"Connection failed",
"Disconnected",
"Connected!", // 0
"Bad protocol",
"Bad client ID",
"Unavailable",
"Bad credentials",
"Unauthorized" }; // 5
last_failed_at = seconds(); last_failed_at = seconds();
Serial.print(F(" Failed! Error code=")); Serial.print(F(" Failed! "));
Serial.print(mqtt_statuses[mqttClient.state() + 4]);
Serial.print(" (Code=");
Serial.print(mqttClient.state()); Serial.print(mqttClient.state());
Serial.print(F(". Will try again in ")); Serial.print(F("). Will try again in "));
Serial.print(config::wait_after_fail); Serial.print(config::wait_after_fail);
Serial.println("s."); Serial.println("s.");
} }
...@@ -178,9 +191,9 @@ namespace mqtt { ...@@ -178,9 +191,9 @@ namespace mqtt {
led_effects::showKITTWheel(color::green, 1); led_effects::showKITTWheel(color::green, 1);
} }
// It can be hard to find the local IP of a sensor if it isn't connected to Serial port, and if mDNS is disabled. // It can be hard to find the local IP of a sensor if it isn't connected to Serial port, and if mDNS is disabled.
// If the sensor can be reach by MQTT, it can answer with info about local_ip and ssid. // If the sensor can be reach by MQTT, it can answer with info about local_ip and ssid.
// The sensor will send the info to "CO2sensors/ESP123456/info". // The sensor will send the info to "CO2sensors/ESP123456/info".
void sendInfoAboutLocalNetwork() { void sendInfoAboutLocalNetwork() {
char info_topic[60]; // Should be enough for "CO2sensors/ESP123456/info" char info_topic[60]; // Should be enough for "CO2sensors/ESP123456/info"
snprintf(info_topic, sizeof(info_topic), "%s/info", publish_topic); snprintf(info_topic, sizeof(info_topic), "%s/info", publish_topic);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment