From 40b0782b53b3397a2fa17b2710b22528fbd50dc3 Mon Sep 17 00:00:00 2001
From: Eric Duminil <eric.duminil@gmail.com>
Date: Sun, 27 Dec 2020 21:28:26 +0100
Subject: [PATCH] Adding mqtt::connected? to web-server

---
 mqtt.cpp       | 6 ++++--
 mqtt.h         | 1 +
 web_server.cpp | 4 ++--
 3 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/mqtt.cpp b/mqtt.cpp
index 98eefc8..08fe67c 100644
--- a/mqtt.cpp
+++ b/mqtt.cpp
@@ -22,6 +22,7 @@ PubSubClient mqttClient(espClient);
 namespace mqtt {
   unsigned long last_sent_at = 0;
   unsigned long last_failed_at = 0;
+  bool connected = false;
 
   String publish_topic;
   const char *json_sensor_format;
@@ -191,8 +192,9 @@ namespace mqtt {
     mqttClient.connect(publish_topic.c_str(), config::mqtt_user, config::mqtt_password);
     LedEffects::onBoardLEDOff();
 
-    if (mqttClient.connected()) {
-      //TODO: Send local IP?
+    connected = mqttClient.connected();
+
+    if (connected) {
       if (config::allow_mqtt_commands) {
         char control_topic[60]; // Should be enough for "CO2sensors/ESPd03cc5/control"
         snprintf(control_topic, sizeof(control_topic), "%s/control", publish_topic.c_str());
diff --git a/mqtt.h b/mqtt.h
index 8b520cc..2899a99 100644
--- a/mqtt.h
+++ b/mqtt.h
@@ -15,6 +15,7 @@ namespace config {
 }
 namespace mqtt {
   extern String last_successful_publish;
+  extern bool connected;
   void initialize(String &topic);
   void keepConnection();
   void publishIfTimeHasCome(const String &timeStamp, const int16_t &co2, const float &temp, const float &hum);
diff --git a/web_server.cpp b/web_server.cpp
index 2fc7730..7501263 100644
--- a/web_server.cpp
+++ b/web_server.cpp
@@ -95,7 +95,7 @@ namespace web_server {
 #endif
 #ifdef AMPEL_MQTT
             "<tr><th colspan='2'>MQTT</th></tr>\n"
-            //TODO: Add connected?
+            "<tr><td>Connected?</td><td>%s</td></tr>\n"
             "<tr><td>Last publish</td><td>%s</td></tr>\n"
             "<tr><td>Timestep</td><td>%5d s</td></tr>\n"
 #endif
@@ -225,7 +225,7 @@ namespace web_server {
         csv_writer::last_successful_write.c_str(), config::csv_interval, csv_writer::getAvailableSpace() / 1024,
 #endif
 #ifdef AMPEL_MQTT
-        mqtt::last_successful_publish.c_str(), config::sending_interval,
+        mqtt::connected ? "Yes" : "No", mqtt::last_successful_publish.c_str(), config::sending_interval,
 #endif
 #if defined(AMPEL_LORAWAN) && defined(ESP32)
         lorawan::connected ? "Yes" : "No", LMIC_FREQUENCY_PLAN, lorawan::last_transmission.c_str(),
-- 
GitLab