From 0dd9901d19c9427d32a1c679340ae4c17d68fe57 Mon Sep 17 00:00:00 2001 From: Mathieu Maret Date: Fri, 1 Apr 2016 00:14:12 +0200 Subject: [PATCH] Avoid mqtt communication if mqtt is not configured --- WifiControlSensor/MQTT.ino | 7 +++++++ WifiControlSensor/WifiControlSensor.ino | 7 +++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/WifiControlSensor/MQTT.ino b/WifiControlSensor/MQTT.ino index 691a3cd..8d11395 100644 --- a/WifiControlSensor/MQTT.ino +++ b/WifiControlSensor/MQTT.ino @@ -25,6 +25,8 @@ char GPIO_SET_FEED[MAXSUBSCRIPTIONS][FEED_MAX_SIZE] = {}; Adafruit_MQTT_Publish * mqttGpio[MAXSUBSCRIPTIONS] = {}; +bool isMqttConfigured = false; + int MqttSetup(char *server, char *user, char *passwd, int port, char * hostname) { mqttId = hostname; snprintf(temperatureFeed, FEED_MAX_SIZE, TEMPERATURE_FEED_FORMAT, mqttId); @@ -34,6 +36,8 @@ int MqttSetup(char *server, char *user, char *passwd, int port, char * hostname) mqtt_temp = new Adafruit_MQTT_Publish(mqtt, temperatureFeed); mqtt_pressure = new Adafruit_MQTT_Publish(mqtt, pressureFeed); + isMqttConfigured = server[0] != '\0'; + for (int i = 0 ; i < NB_ELEMENTS(gpioWatched) && i < MAXSUBSCRIPTIONS; i++) { snprintf(GPIO_FEED[i], FEED_MAX_SIZE, GPIO_FEED_FORMAT, mqttId, gpioWatched[i]); snprintf(GPIO_SET_FEED[i], FEED_MAX_SIZE, GPIO_SET_FEED_FORMAT, mqttId, gpioWatched[i]); @@ -47,6 +51,9 @@ int MqttSetup(char *server, char *user, char *passwd, int port, char * hostname) return 0; } +bool MqttIsConfigured(){ + return isMqttConfigured; +} int MqttIsConnected() { return mqtt->connected(); } diff --git a/WifiControlSensor/WifiControlSensor.ino b/WifiControlSensor/WifiControlSensor.ino index a44ead8..e56e167 100644 --- a/WifiControlSensor/WifiControlSensor.ino +++ b/WifiControlSensor/WifiControlSensor.ino @@ -80,6 +80,7 @@ int MqttSetup(char *server, char *user, char *passwd, int port, char * hostname) int MqttPublish(double temp, double pressure); void MqttCheckSubscription(); void MqttChangeGpioValue(int gpio, int value); +bool MqttIsConfigured(); void WifiSetup(int bootmode, int forceSetup, char *confSsid, char *confPassword, char *confHost) { IPAddress myIP; @@ -215,7 +216,8 @@ void loop() { ArduinoOTA.handle(); } else { server.handleClient(); - MqttCheckSubscription(); + if (MqttIsConfigured()) + MqttCheckSubscription(); delay(CONFIG_WEB_DELAY_MS); nbCycle++; @@ -225,7 +227,8 @@ void loop() { SKETCH_DEBUG_PRINT(temp); SKETCH_DEBUG_PRINT( " Pressure mB "); SKETCH_DEBUG_PRINTLN(pressure); - MqttPublish(temp, pressure); + if (MqttIsConfigured()) + MqttPublish(temp, pressure); } else { SKETCH_DEBUG_PRINTLN("Cannot get T°C"); }