Better handling of parameters and first connect to mqtt broker

This commit is contained in:
Thomas Basler 2019-11-14 22:25:56 +01:00
parent 3d5db22d6b
commit 248cddd008
2 changed files with 11 additions and 12 deletions

View File

@ -19,6 +19,7 @@ lib_deps =
ESPAsyncTCP
ESP Async WebServer
ESP-DASH
AsyncMqttClient
ArduinoJson
BH1750
Adafruit Unified Sensor

View File

@ -6,6 +6,7 @@
#include <ESPAsyncWebServer.h>
#include <ESPDash.h>
#include <IotWebConf.h>
#include <AsyncMqttClient.h>
#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;
}