From b631e572e9c91bc9921d47ec6dd796c32521029a Mon Sep 17 00:00:00 2001 From: Graham Date: Sun, 5 May 2024 07:00:51 -0400 Subject: [PATCH] Upgrade IOTWebConf to version 3+ Migrate to IotWebConf version 3.2 --- code/ESP32/include/IOT.h | 4 +++- code/ESP32/platformio.ini | 6 ++--- code/ESP32/src/IOT.cpp | 45 +++++++++++++++++++------------------- code/ESP32/src/Tracker.cpp | 2 +- 4 files changed, 30 insertions(+), 27 deletions(-) diff --git a/code/ESP32/include/IOT.h b/code/ESP32/include/IOT.h index 1df2682..d8c5b25 100644 --- a/code/ESP32/include/IOT.h +++ b/code/ESP32/include/IOT.h @@ -8,11 +8,13 @@ extern "C" #include "freertos/timers.h" } #include "AsyncMqttClient.h" -#include "IotWebConf.h" +#include +#include #include "Configuration.h" #define STR_LEN 64 // general string buffer size #define CONFIG_LEN 32 // configuration string buffer size +#define NUMBER_CONFIG_LEN 5 extern SkyeTracker::Configuration _config; diff --git a/code/ESP32/platformio.ini b/code/ESP32/platformio.ini index 282c522..417358e 100644 --- a/code/ESP32/platformio.ini +++ b/code/ESP32/platformio.ini @@ -19,10 +19,10 @@ lib_deps = ArduinoJson Thread AsyncMqttClient - IotWebConf@2.3.1 + IotWebConf build_flags = - -D 'CONFIG_VERSION="V1.0.6"' ; major.minor.build (major or minor will invalidate the configuration) + -D 'CONFIG_VERSION="V1.2.0"' ; major.minor.build (major or minor will invalidate the configuration) -D 'HOME_ASSISTANT_PREFIX="homeassistant"' ; Home Assistant Auto discovery root topic -D 'BluetoothDeviceName="HC-06"' ; name should be HC-06 to work with Play Store version of the Android App @@ -62,5 +62,5 @@ build_flags = -D FACTORY_RESET_PIN=4 ; logs are sent over BT. ; -D APP_LOG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG - -D APP_LOG_LEVEL=ARDUHAL_LOG_LEVEL_ERROR + -D APP_LOG_LEVEL=ARDUHAL_LOG_LEVEL_INFO diff --git a/code/ESP32/src/IOT.cpp b/code/ESP32/src/IOT.cpp index 819c9ad..498e19f 100644 --- a/code/ESP32/src/IOT.cpp +++ b/code/ESP32/src/IOT.cpp @@ -4,6 +4,7 @@ #include "time.h" #include "Log.h" #include "Tracker.h" +#include extern SkyeTracker::Tracker _tracker; @@ -13,6 +14,7 @@ AsyncMqttClient _mqttClient; TimerHandle_t mqttReconnectTimer; DNSServer _dnsServer; WebServer _webServer(80); +// Create Update Server HTTPUpdateServer _httpUpdater; IotWebConf _iotWebConf(TAG, &_dnsServer, &_webServer, TAG, CONFIG_VERSION); char _mqttRootTopic[STR_LEN]; @@ -22,18 +24,18 @@ char _mqttPort[5]; char _mqttUserName[IOTWEBCONF_WORD_LEN]; char _mqttUserPassword[IOTWEBCONF_WORD_LEN]; u_int32_t _uniqueId = ESP.getEfuseMac() & 0xFFFFFFFF; -IotWebConfSeparator seperatorParam = IotWebConfSeparator("MQTT"); -IotWebConfParameter mqttServerParam = IotWebConfParameter("MQTT server", "mqttServer", _mqttServer, IOTWEBCONF_WORD_LEN); -IotWebConfParameter mqttPortParam = IotWebConfParameter("MQTT port", "mqttSPort", _mqttPort, 5, "text", NULL, "1883"); -IotWebConfParameter mqttUserNameParam = IotWebConfParameter("MQTT user", "mqttUser", _mqttUserName, IOTWEBCONF_WORD_LEN); -IotWebConfParameter mqttUserPasswordParam = IotWebConfParameter("MQTT password", "mqttPass", _mqttUserPassword, IOTWEBCONF_WORD_LEN, "password"); -IotWebConfParameter mqttRootTopicParam = IotWebConfParameter("MQTT Root Topic", "mqttRootTopic", _mqttRootTopic, IOTWEBCONF_WORD_LEN); +iotwebconf::ParameterGroup MQTT_group = iotwebconf::ParameterGroup("MQTT", "MQTT"); +iotwebconf::TextParameter mqttServerParam = iotwebconf::TextParameter("MQTT server", "mqttServer", _mqttServer, IOTWEBCONF_WORD_LEN); +iotwebconf::NumberParameter mqttPortParam = iotwebconf::NumberParameter("MQTT port", "mqttSPort", _mqttPort, NUMBER_CONFIG_LEN, "text", NULL, "1883"); +iotwebconf::TextParameter mqttUserNameParam = iotwebconf::TextParameter("MQTT user", "mqttUser", _mqttUserName, IOTWEBCONF_WORD_LEN); +iotwebconf::PasswordParameter mqttUserPasswordParam = iotwebconf::PasswordParameter("MQTT password", "mqttPass", _mqttUserPassword, IOTWEBCONF_WORD_LEN, "password"); +iotwebconf::TextParameter mqttRootTopicParam = iotwebconf::TextParameter("MQTT Root Topic", "mqttRootTopic", _mqttRootTopic, IOTWEBCONF_WORD_LEN); const char *ntpServer = "pool.ntp.org"; void publishDiscovery() { char buffer[STR_LEN]; - StaticJsonDocument<1024> doc; // MQTT discovery + JsonDocument doc; doc["name"] = _iotWebConf.getThingName(); sprintf(buffer, "%X", _uniqueId); doc["unique_id"] = buffer; @@ -42,14 +44,14 @@ void publishDiscovery() doc["avty_t"] = "~/tele/LWT"; doc["pl_avail"] = "Online"; doc["pl_not_avail"] = "Offline"; - JsonObject device = doc.createNestedObject("device"); + JsonObject device = doc["device"].to(); device["name"] = "SkyeTracker"; device["sw_version"] = CONFIG_VERSION; device["manufacturer"] = "SkyeTracker"; sprintf(buffer, "ESP32-Bit (%X)", _uniqueId); device["model"] = buffer; - JsonArray identifiers = device.createNestedArray("identifiers"); - identifiers.add(_uniqueId); + sprintf(buffer, "%X_%s", _uniqueId, "SkyeTracker"); + device["identifiers"] = buffer; doc["~"] = _mqttRootTopic; String s; serializeJson(doc, s); @@ -92,12 +94,12 @@ void WiFiEvent(WiFiEvent_t event) { logd("[WiFi-event] event: %d", event); String s; - StaticJsonDocument<128> doc; + JsonDocument doc; switch (event) { case SYSTEM_EVENT_STA_GOT_IP: // logd("WiFi connected, IP address: %s", WiFi.localIP().toString().c_str()); - doc["IP"] = WiFi.localIP().toString().c_str(); + doc["IP"] = WiFi.localIP(); doc["ApPassword"] = TAG; serializeJson(doc, s); s += '\n'; @@ -212,13 +214,12 @@ void IOT::Init() } mqttReconnectTimer = xTimerCreate("mqttTimer", pdMS_TO_TICKS(5000), pdFALSE, (void *)0, reinterpret_cast(connectToMqtt)); WiFi.onEvent(WiFiEvent); - _iotWebConf.setupUpdateServer(&_httpUpdater); - _iotWebConf.addParameter(&seperatorParam); - _iotWebConf.addParameter(&mqttServerParam); - _iotWebConf.addParameter(&mqttPortParam); - _iotWebConf.addParameter(&mqttUserNameParam); - _iotWebConf.addParameter(&mqttUserPasswordParam); - _iotWebConf.addParameter(&mqttRootTopicParam); + MQTT_group.addItem(&mqttServerParam); + MQTT_group.addItem(&mqttPortParam); + MQTT_group.addItem(&mqttUserNameParam); + MQTT_group.addItem(&mqttUserPasswordParam); + MQTT_group.addItem(&mqttRootTopicParam); + _iotWebConf.addParameterGroup(&MQTT_group); boolean validConfig = _iotWebConf.init(); if (!validConfig) { @@ -288,7 +289,7 @@ void IOT::Run() { String s = Serial.readStringUntil('}'); s += "}"; - StaticJsonDocument<128> doc; + JsonDocument doc; DeserializationError err = deserializeJson(doc, s); if (err) { @@ -298,7 +299,7 @@ void IOT::Run() { if (doc.containsKey("ssid") && doc.containsKey("password")) { - IotWebConfParameter *p = _iotWebConf.getWifiSsidParameter(); + iotwebconf::Parameter *p = _iotWebConf.getWifiSsidParameter(); strcpy(p->valueBuffer, doc["ssid"]); logd("Setting ssid: %s", p->valueBuffer); p = _iotWebConf.getWifiPasswordParameter(); @@ -306,7 +307,7 @@ void IOT::Run() logd("Setting password: %s", p->valueBuffer); p = _iotWebConf.getApPasswordParameter(); strcpy(p->valueBuffer, TAG); // reset to default AP password - _iotWebConf.configSave(); + _iotWebConf.saveConfig(); esp_restart(); // force reboot } else diff --git a/code/ESP32/src/Tracker.cpp b/code/ESP32/src/Tracker.cpp index 3ad5ff4..9bb8a10 100644 --- a/code/ESP32/src/Tracker.cpp +++ b/code/ESP32/src/Tracker.cpp @@ -269,7 +269,7 @@ namespace SkyeTracker char data[64]; unsigned int commandIndex = 0; unsigned int dataIndex = 0; - StaticJsonDocument<64> doc; + JsonDocument doc; for (unsigned int i = 0; i < strlen(input); i++) { if (input[i] == '|')