Better handling of parameters and first connect to mqtt broker
This commit is contained in:
parent
3d5db22d6b
commit
248cddd008
@ -19,6 +19,7 @@ lib_deps =
|
|||||||
ESPAsyncTCP
|
ESPAsyncTCP
|
||||||
ESP Async WebServer
|
ESP Async WebServer
|
||||||
ESP-DASH
|
ESP-DASH
|
||||||
|
AsyncMqttClient
|
||||||
ArduinoJson
|
ArduinoJson
|
||||||
BH1750
|
BH1750
|
||||||
Adafruit Unified Sensor
|
Adafruit Unified Sensor
|
||||||
|
22
src/main.cpp
22
src/main.cpp
@ -6,6 +6,7 @@
|
|||||||
#include <ESPAsyncWebServer.h>
|
#include <ESPAsyncWebServer.h>
|
||||||
#include <ESPDash.h>
|
#include <ESPDash.h>
|
||||||
#include <IotWebConf.h>
|
#include <IotWebConf.h>
|
||||||
|
#include <AsyncMqttClient.h>
|
||||||
|
|
||||||
#define I2C_SDA 25
|
#define I2C_SDA 25
|
||||||
#define I2C_SCL 26
|
#define I2C_SCL 26
|
||||||
@ -21,7 +22,6 @@ BH1750 lightMeter;
|
|||||||
DHT_Unified dht(DHT12_PIN, DHT11);
|
DHT_Unified dht(DHT12_PIN, DHT11);
|
||||||
|
|
||||||
#define STRING_LEN 128
|
#define STRING_LEN 128
|
||||||
#define BOOL_LEN 1
|
|
||||||
|
|
||||||
// Configuration specific key. The value should be modified if config structure was changed.
|
// Configuration specific key. The value should be modified if config structure was changed.
|
||||||
#define CONFIG_VERSION "hgr1"
|
#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.
|
// Initial password to connect to the Thing, when it creates an own Access Point.
|
||||||
const char wifiInitialApPassword[] = "test1234";
|
const char wifiInitialApPassword[] = "test1234";
|
||||||
|
|
||||||
char mqttEnabledValue[BOOL_LEN];
|
|
||||||
char mqttServerValue[STRING_LEN];
|
char mqttServerValue[STRING_LEN];
|
||||||
char mqttUserNameValue[STRING_LEN];
|
char mqttUserNameValue[STRING_LEN];
|
||||||
char mqttUserPasswordValue[STRING_LEN];
|
char mqttUserPasswordValue[STRING_LEN];
|
||||||
char mqttTopicValue[STRING_LEN];
|
char mqttTopicValue[STRING_LEN];
|
||||||
IotWebConfSeparator separator1 = IotWebConfSeparator();
|
IotWebConfSeparator separator1 = IotWebConfSeparator();
|
||||||
IotWebConfParameter mqttEnabledParam = IotWebConfParameter("MQTT enabled", "mqttEnabled", mqttEnabledValue, BOOL_LEN, "checkbox");
|
|
||||||
IotWebConfParameter mqttServerParam = IotWebConfParameter("MQTT server", "mqttServer", mqttServerValue, STRING_LEN);
|
IotWebConfParameter mqttServerParam = IotWebConfParameter("MQTT server", "mqttServer", mqttServerValue, STRING_LEN);
|
||||||
IotWebConfParameter mqttUserNameParam = IotWebConfParameter("MQTT user", "mqttUser", mqttUserNameValue, STRING_LEN);
|
IotWebConfParameter mqttUserNameParam = IotWebConfParameter("MQTT user", "mqttUser", mqttUserNameValue, STRING_LEN);
|
||||||
IotWebConfParameter mqttUserPasswordParam = IotWebConfParameter("MQTT password", "mqttPass", mqttUserPasswordValue, STRING_LEN, "password");
|
IotWebConfParameter mqttUserPasswordParam = IotWebConfParameter("MQTT password", "mqttPass", mqttUserPasswordValue, STRING_LEN, "password");
|
||||||
@ -48,6 +46,7 @@ DNSServer dnsServer;
|
|||||||
AsyncWebServer server(80);
|
AsyncWebServer server(80);
|
||||||
AsyncCallbackWebHandler* rootHandler;
|
AsyncCallbackWebHandler* rootHandler;
|
||||||
IotWebConf iotWebConf(thingName, &dnsServer, &server, wifiInitialApPassword, CONFIG_VERSION);
|
IotWebConf iotWebConf(thingName, &dnsServer, &server, wifiInitialApPassword, CONFIG_VERSION);
|
||||||
|
AsyncMqttClient mqttClient;
|
||||||
|
|
||||||
uint16_t soil = 0;
|
uint16_t soil = 0;
|
||||||
uint32_t salt = 0;
|
uint32_t salt = 0;
|
||||||
@ -56,7 +55,7 @@ float temperature = 0;
|
|||||||
float humidity = 0;
|
float humidity = 0;
|
||||||
float lux = 0;
|
float lux = 0;
|
||||||
|
|
||||||
bool needMqttReconnect = false;
|
bool needMqttReconnect = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle web requests to "/" path.
|
* Handle web requests to "/" path.
|
||||||
@ -145,16 +144,12 @@ boolean formValidator(AsyncWebServerRequest* request)
|
|||||||
{
|
{
|
||||||
Serial.println("Validating form.");
|
Serial.println("Validating form.");
|
||||||
boolean valid = true;
|
boolean valid = true;
|
||||||
/*
|
|
||||||
if (request->arg(mqttServerParam.getId()).length() < 3) {
|
if (request->arg(mqttServerParam.getId()).length() > 0
|
||||||
mqttServerParam.errorMessage = "Please provide at least 3 characters!";
|
&& request->arg(mqttTopicParam.getId()).length() == 0) {
|
||||||
valid = false;
|
|
||||||
}
|
|
||||||
if (request->arg(mqttTopicParam.getId()).length() < 1) {
|
|
||||||
mqttTopicParam.errorMessage = "Please provide at least 1 character!";
|
mqttTopicParam.errorMessage = "Please provide at least 1 character!";
|
||||||
valid = false;
|
valid = false;
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
return valid;
|
return valid;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -167,7 +162,6 @@ void setup()
|
|||||||
// Init IotWebConf
|
// Init IotWebConf
|
||||||
iotWebConf.setWifiConnectionCallback(&wifiConnected);
|
iotWebConf.setWifiConnectionCallback(&wifiConnected);
|
||||||
iotWebConf.addParameter(&separator1);
|
iotWebConf.addParameter(&separator1);
|
||||||
iotWebConf.addParameter(&mqttEnabledParam);
|
|
||||||
iotWebConf.addParameter(&mqttServerParam);
|
iotWebConf.addParameter(&mqttServerParam);
|
||||||
iotWebConf.addParameter(&mqttUserNameParam);
|
iotWebConf.addParameter(&mqttUserNameParam);
|
||||||
iotWebConf.addParameter(&mqttUserPasswordParam);
|
iotWebConf.addParameter(&mqttUserPasswordParam);
|
||||||
@ -227,6 +221,10 @@ void loop()
|
|||||||
|
|
||||||
if (needMqttReconnect) {
|
if (needMqttReconnect) {
|
||||||
Serial.println("Reconfigure & reconnect MqTT");
|
Serial.println("Reconfigure & reconnect MqTT");
|
||||||
|
if (String(mqttServerValue) != "") {
|
||||||
|
mqttClient.setServer(mqttServerValue, 1883);
|
||||||
|
mqttClient.connect();
|
||||||
|
}
|
||||||
needMqttReconnect = false;
|
needMqttReconnect = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user