diff --git a/ampel-firmware/mqtt.cpp b/ampel-firmware/mqtt.cpp index cf55823621605941285b265213c308945c20a3ce..e847fc396a4cdcf25fe01d81bfbbeb9ff252ab2f 100644 --- a/ampel-firmware/mqtt.cpp +++ b/ampel-firmware/mqtt.cpp @@ -59,7 +59,7 @@ namespace mqtt { } 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(); Serial.print(F("MQTT - Publishing message ... ")); @@ -105,7 +105,7 @@ namespace mqtt { // It failed less than wait_after_fail ago. Not even trying. 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 return; } @@ -141,10 +141,23 @@ namespace mqtt { Serial.println(F(" Connected.")); last_failed_at = 0; } 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(); - 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(F(". Will try again in ")); + Serial.print(F("). Will try again in ")); Serial.print(config::wait_after_fail); Serial.println("s."); } @@ -178,9 +191,9 @@ namespace mqtt { 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. - // 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". +// 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. +// The sensor will send the info to "CO2sensors/ESP123456/info". void sendInfoAboutLocalNetwork() { char info_topic[60]; // Should be enough for "CO2sensors/ESP123456/info" snprintf(info_topic, sizeof(info_topic), "%s/info", publish_topic);