From 248cddd0082de076a2c0933efbf715be92c1ad8e Mon Sep 17 00:00:00 2001 From: Thomas Basler Date: Thu, 14 Nov 2019 22:25:56 +0100 Subject: [PATCH] Better handling of parameters and first connect to mqtt broker --- platformio.ini | 1 + src/main.cpp | 22 ++++++++++------------ 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/platformio.ini b/platformio.ini index 8c0c2de..9ea6793 100644 --- a/platformio.ini +++ b/platformio.ini @@ -19,6 +19,7 @@ lib_deps = ESPAsyncTCP ESP Async WebServer ESP-DASH + AsyncMqttClient ArduinoJson BH1750 Adafruit Unified Sensor diff --git a/src/main.cpp b/src/main.cpp index bb207a9..0ccdd99 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #define I2C_SDA 25 #define I2C_SCL 26 @@ -21,7 +22,6 @@ BH1750 lightMeter; DHT_Unified dht(DHT12_PIN, DHT11); #define STRING_LEN 128 -#define BOOL_LEN 1 // Configuration specific key. The value should be modified if config structure was changed. #define CONFIG_VERSION "hgr1" @@ -32,13 +32,11 @@ const char thingName[] = "hiGrow"; // Initial password to connect to the Thing, when it creates an own Access Point. const char wifiInitialApPassword[] = "test1234"; -char mqttEnabledValue[BOOL_LEN]; char mqttServerValue[STRING_LEN]; char mqttUserNameValue[STRING_LEN]; char mqttUserPasswordValue[STRING_LEN]; char mqttTopicValue[STRING_LEN]; IotWebConfSeparator separator1 = IotWebConfSeparator(); -IotWebConfParameter mqttEnabledParam = IotWebConfParameter("MQTT enabled", "mqttEnabled", mqttEnabledValue, BOOL_LEN, "checkbox"); IotWebConfParameter mqttServerParam = IotWebConfParameter("MQTT server", "mqttServer", mqttServerValue, STRING_LEN); IotWebConfParameter mqttUserNameParam = IotWebConfParameter("MQTT user", "mqttUser", mqttUserNameValue, STRING_LEN); IotWebConfParameter mqttUserPasswordParam = IotWebConfParameter("MQTT password", "mqttPass", mqttUserPasswordValue, STRING_LEN, "password"); @@ -48,6 +46,7 @@ DNSServer dnsServer; AsyncWebServer server(80); AsyncCallbackWebHandler* rootHandler; IotWebConf iotWebConf(thingName, &dnsServer, &server, wifiInitialApPassword, CONFIG_VERSION); +AsyncMqttClient mqttClient; uint16_t soil = 0; uint32_t salt = 0; @@ -56,7 +55,7 @@ float temperature = 0; float humidity = 0; float lux = 0; -bool needMqttReconnect = false; +bool needMqttReconnect = true; /** * Handle web requests to "/" path. @@ -145,16 +144,12 @@ boolean formValidator(AsyncWebServerRequest* request) { Serial.println("Validating form."); boolean valid = true; -/* - if (request->arg(mqttServerParam.getId()).length() < 3) { - mqttServerParam.errorMessage = "Please provide at least 3 characters!"; - valid = false; - } - if (request->arg(mqttTopicParam.getId()).length() < 1) { + + if (request->arg(mqttServerParam.getId()).length() > 0 + && request->arg(mqttTopicParam.getId()).length() == 0) { mqttTopicParam.errorMessage = "Please provide at least 1 character!"; valid = false; } -*/ return valid; } @@ -167,7 +162,6 @@ void setup() // Init IotWebConf iotWebConf.setWifiConnectionCallback(&wifiConnected); iotWebConf.addParameter(&separator1); - iotWebConf.addParameter(&mqttEnabledParam); iotWebConf.addParameter(&mqttServerParam); iotWebConf.addParameter(&mqttUserNameParam); iotWebConf.addParameter(&mqttUserPasswordParam); @@ -227,6 +221,10 @@ void loop() if (needMqttReconnect) { Serial.println("Reconfigure & reconnect MqTT"); + if (String(mqttServerValue) != "") { + mqttClient.setServer(mqttServerValue, 1883); + mqttClient.connect(); + } needMqttReconnect = false; }