Avoid mqtt communication if mqtt is not configured

This commit is contained in:
Mathieu Maret 2016-04-01 00:14:12 +02:00
parent b843eeb184
commit 0dd9901d19
2 changed files with 12 additions and 2 deletions

View File

@ -25,6 +25,8 @@ char GPIO_SET_FEED[MAXSUBSCRIPTIONS][FEED_MAX_SIZE] = {};
Adafruit_MQTT_Publish * mqttGpio[MAXSUBSCRIPTIONS] = {}; Adafruit_MQTT_Publish * mqttGpio[MAXSUBSCRIPTIONS] = {};
bool isMqttConfigured = false;
int MqttSetup(char *server, char *user, char *passwd, int port, char * hostname) { int MqttSetup(char *server, char *user, char *passwd, int port, char * hostname) {
mqttId = hostname; mqttId = hostname;
snprintf(temperatureFeed, FEED_MAX_SIZE, TEMPERATURE_FEED_FORMAT, mqttId); 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_temp = new Adafruit_MQTT_Publish(mqtt, temperatureFeed);
mqtt_pressure = new Adafruit_MQTT_Publish(mqtt, pressureFeed); mqtt_pressure = new Adafruit_MQTT_Publish(mqtt, pressureFeed);
isMqttConfigured = server[0] != '\0';
for (int i = 0 ; i < NB_ELEMENTS(gpioWatched) && i < MAXSUBSCRIPTIONS; i++) { 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_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]); 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; return 0;
} }
bool MqttIsConfigured(){
return isMqttConfigured;
}
int MqttIsConnected() { int MqttIsConnected() {
return mqtt->connected(); return mqtt->connected();
} }

View File

@ -80,6 +80,7 @@ int MqttSetup(char *server, char *user, char *passwd, int port, char * hostname)
int MqttPublish(double temp, double pressure); int MqttPublish(double temp, double pressure);
void MqttCheckSubscription(); void MqttCheckSubscription();
void MqttChangeGpioValue(int gpio, int value); void MqttChangeGpioValue(int gpio, int value);
bool MqttIsConfigured();
void WifiSetup(int bootmode, int forceSetup, char *confSsid, char *confPassword, char *confHost) { void WifiSetup(int bootmode, int forceSetup, char *confSsid, char *confPassword, char *confHost) {
IPAddress myIP; IPAddress myIP;
@ -215,6 +216,7 @@ void loop() {
ArduinoOTA.handle(); ArduinoOTA.handle();
} else { } else {
server.handleClient(); server.handleClient();
if (MqttIsConfigured())
MqttCheckSubscription(); MqttCheckSubscription();
delay(CONFIG_WEB_DELAY_MS); delay(CONFIG_WEB_DELAY_MS);
@ -225,6 +227,7 @@ void loop() {
SKETCH_DEBUG_PRINT(temp); SKETCH_DEBUG_PRINT(temp);
SKETCH_DEBUG_PRINT( " Pressure mB "); SKETCH_DEBUG_PRINT( " Pressure mB ");
SKETCH_DEBUG_PRINTLN(pressure); SKETCH_DEBUG_PRINTLN(pressure);
if (MqttIsConfigured())
MqttPublish(temp, pressure); MqttPublish(temp, pressure);
} else { } else {
SKETCH_DEBUG_PRINTLN("Cannot get T°C"); SKETCH_DEBUG_PRINTLN("Cannot get T°C");