From 2d7449aebac926282311438de2ff85ebc0085b20 Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Thu, 8 Jul 2021 10:17:50 +0200 Subject: [PATCH] add network options, IPv6 for mqtt --- CHANGELOG_LATEST.md | 6 ++- interface/src/network/NetworkSettingsForm.tsx | 53 ++++++++++++++++++- interface/src/network/types.ts | 4 ++ interface/src/project/EMSESPSettingsForm.tsx | 10 ++++ interface/src/project/EMSESPtypes.ts | 1 + interface/src/validators/isIP.ts | 3 +- lib/async-mqtt-client/src/AsyncMqttClient.cpp | 33 +++++++++--- lib/async-mqtt-client/src/AsyncMqttClient.hpp | 3 ++ lib/framework/APSettingsService.cpp | 1 + lib/framework/MqttSettingsService.cpp | 1 + lib/framework/NetworkSettingsService.cpp | 19 ++++--- lib/framework/NetworkSettingsService.h | 14 ++++- src/system.cpp | 9 ++++ src/system.h | 8 ++- src/web/WebSettingsService.cpp | 4 +- src/web/WebSettingsService.h | 1 + src/web/WebStatusService.cpp | 37 ++++++++++--- 17 files changed, 180 insertions(+), 27 deletions(-) diff --git a/CHANGELOG_LATEST.md b/CHANGELOG_LATEST.md index a37fc6b77..4478bab35 100644 --- a/CHANGELOG_LATEST.md +++ b/CHANGELOG_LATEST.md @@ -2,14 +2,16 @@ ## Added -- support for IPv6 (#83) +- support for IPv6 (web/api/mqtt, not syslog) (#83) - System Log in Web UI will show current time if the NTP Service is enabled (#82) +- Network settings for Tx-power, WiFi-bandwidth, WiFi-sleepmode (#83) +- optional low clockrate (160 MHz) (#83) ## Fixed ## Changed -- removed Rx echo failures counting as incomplete telegrams. BAd telegrams show as Warning and not Errors. [#80](https://github.com/emsesp/EMS-ESP32/issues/80) +- removed Rx echo failures counting as incomplete telegrams. Bad telegrams show as Warning and not Errors. [#80](https://github.com/emsesp/EMS-ESP32/issues/80) - add upload_sec to `api/system/info` and removed # from some names to keep consistent with MQTT heartbeat - added debug target to PlatformIO build to help hunt down system crashes diff --git a/interface/src/network/NetworkSettingsForm.tsx b/interface/src/network/NetworkSettingsForm.tsx index 5928688a5..97325530d 100644 --- a/interface/src/network/NetworkSettingsForm.tsx +++ b/interface/src/network/NetworkSettingsForm.tsx @@ -51,7 +51,11 @@ class NetworkSettingsForm extends React.Component { ssid: selectedNetwork.ssid, password: '', hostname: props.data.hostname, - static_ip_config: false + static_ip_config: false, + enableIPv6: false, + bandwidth20: false, + tx_power: 20, + nosleep: false }; props.setData(networkSettings); } @@ -145,6 +149,53 @@ class NetworkSettingsForm extends React.Component { onChange={handleValueChange('hostname')} margin="normal" /> + + + } + label="Enable IPv6" + /> + + } + label="WiFi Low Bandwidth" + /> + + } + label="Disable Wifi Sleepmode" + /> { } label="Enable ADC" /> + + } + label="Low Clockrate (160MHz, changed on next reboot)" + /> #ifndef EMSESP_STANDALONE -#if defined(EMSESP_WIFI_TWEAK) #include -#endif #include #endif @@ -36,6 +34,10 @@ class NetworkSettings { String password; String hostname; bool staticIPConfig; + bool enableIPv6; + bool bandwidth20; + int8_t tx_power; + bool nosleep; // optional configuration for static IP address IPAddress localIP; @@ -50,6 +52,10 @@ class NetworkSettings { root["password"] = settings.password; root["hostname"] = settings.hostname; root["static_ip_config"] = settings.staticIPConfig; + root["enableIPv6"] = settings.enableIPv6; + root["bandwidth20"] = settings.bandwidth20; + root["tx_power"] = settings.tx_power; + root["nosleep"] = settings.nosleep; // extended settings JsonUtils::writeIP(root, "local_ip", settings.localIP); @@ -64,6 +70,10 @@ class NetworkSettings { settings.password = root["password"] | FACTORY_WIFI_PASSWORD; settings.hostname = root["hostname"] | FACTORY_WIFI_HOSTNAME; settings.staticIPConfig = root["static_ip_config"] | false; + settings.enableIPv6 = root["enableIPv6"] | false; + settings.bandwidth20 = root["bandwidth20"] | false; + settings.tx_power = root["tx_power"] | 20; + settings.nosleep = root["nosleep"] | false; // extended settings JsonUtils::readIP(root, "local_ip", settings.localIP); diff --git a/src/system.cpp b/src/system.cpp index a97812312..d752ba4ba 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -206,6 +206,9 @@ void System::get_settings() { // ADC analog_enabled_ = settings.analog_enabled; + // Sysclock + low_clock_ = settings.low_clock; + // Syslog syslog_enabled_ = settings.syslog_enabled; syslog_level_ = settings.syslog_level; @@ -275,6 +278,12 @@ void System::start(uint32_t heap_start) { // load in all the settings first get_settings(); +#ifndef EMSESP_STANDALONE + if (low_clock_) { + setCpuFrequencyMhz(160); + } +#endif + EMSESP::esp8266React.getNetworkSettingsService()->read([&](NetworkSettings & networkSettings) { hostname(networkSettings.hostname.c_str()); // sets the hostname LOG_INFO(F("System name: %s"), hostname().c_str()); diff --git a/src/system.h b/src/system.h index 721f24cd5..d786ad7f7 100644 --- a/src/system.h +++ b/src/system.h @@ -96,10 +96,14 @@ class System { void ethernet_connected(bool b) { ethernet_connected_ = b; } + void network_connected(bool b) { + network_connected_ = b; + } bool network_connected() { #ifndef EMSESP_STANDALONE - return (ethernet_connected_ || WiFi.isConnected()); + // return (ethernet_connected_ || WiFi.isConnected()); + return network_connected_; #else return true; #endif @@ -147,6 +151,7 @@ class System { uint32_t last_system_check_ = 0; bool upload_status_ = false; // true if we're in the middle of a OTA firmware upload bool ethernet_connected_ = false; + bool network_connected_ = false; uint16_t analog_; // settings @@ -155,6 +160,7 @@ class System { uint8_t led_gpio_; bool syslog_enabled_; bool analog_enabled_; + bool low_clock_; String board_profile_; uint8_t pbutton_gpio_; int8_t syslog_level_; diff --git a/src/web/WebSettingsService.cpp b/src/web/WebSettingsService.cpp index 0da7bac65..5ecffddf4 100644 --- a/src/web/WebSettingsService.cpp +++ b/src/web/WebSettingsService.cpp @@ -55,6 +55,7 @@ void WebSettings::read(WebSettings & settings, JsonObject & root) { root["dallas_parasite"] = settings.dallas_parasite; root["led_gpio"] = settings.led_gpio; root["hide_led"] = settings.hide_led; + root["low_clock"] = settings.low_clock; root["notoken_api"] = settings.notoken_api; root["analog_enabled"] = settings.analog_enabled; root["pbutton_gpio"] = settings.pbutton_gpio; @@ -165,9 +166,10 @@ StateUpdateResult WebSettings::update(JsonObject & root, WebSettings & settings) settings.hide_led = root["hide_led"] | EMSESP_DEFAULT_HIDE_LED; check_flag(prev, settings.hide_led, ChangeFlags::LED); - // these both need reboots to be applied + // these need reboots to be applied settings.ems_bus_id = root["ems_bus_id"] | EMSESP_DEFAULT_EMS_BUS_ID; settings.master_thermostat = root["master_thermostat"] | EMSESP_DEFAULT_MASTER_THERMOSTAT; + settings.low_clock = root["low_clock"] | false;; // doesn't need any follow-up actions settings.notoken_api = root["notoken_api"] | EMSESP_DEFAULT_NOTOKEN_API; diff --git a/src/web/WebSettingsService.h b/src/web/WebSettingsService.h index dccad6961..8a4a09418 100644 --- a/src/web/WebSettingsService.h +++ b/src/web/WebSettingsService.h @@ -50,6 +50,7 @@ class WebSettings { bool dallas_parasite; uint8_t led_gpio; bool hide_led; + bool low_clock; bool notoken_api; bool analog_enabled; uint8_t pbutton_gpio; diff --git a/src/web/WebStatusService.cpp b/src/web/WebStatusService.cpp index b58eacbb7..6d12a0223 100644 --- a/src/web/WebStatusService.cpp +++ b/src/web/WebStatusService.cpp @@ -35,15 +35,20 @@ void WebStatusService::WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) { switch (event) { case SYSTEM_EVENT_STA_DISCONNECTED: EMSESP::logger().info(F("WiFi Disconnected. Reason code=%d"), info.disconnected.reason); + EMSESP::system_.network_connected(false); break; case SYSTEM_EVENT_STA_GOT_IP: #ifndef EMSESP_STANDALONE EMSESP::logger().info(F("WiFi Connected with IP=%s, hostname=%s"), WiFi.localIP().toString().c_str(), WiFi.getHostname()); #endif - EMSESP::system_.wifi_tweak(); - EMSESP::system_.send_heartbeat(); - EMSESP::system_.syslog_start(); + EMSESP::esp8266React.getNetworkSettingsService()->read([&](NetworkSettings & networkSettings) { + if (!networkSettings.enableIPv6) { + EMSESP::system_.network_connected(true); + EMSESP::system_.send_heartbeat(); + EMSESP::system_.syslog_start(); + } + }); break; case SYSTEM_EVENT_ETH_START: @@ -57,8 +62,13 @@ void WebStatusService::WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) { #ifndef EMSESP_STANDALONE EMSESP::logger().info(F("Ethernet Connected with IP=%s, speed %d Mbps"), ETH.localIP().toString().c_str(), ETH.linkSpeed()); #endif - EMSESP::system_.send_heartbeat(); - EMSESP::system_.syslog_start(); + EMSESP::esp8266React.getNetworkSettingsService()->read([&](NetworkSettings & networkSettings) { + if (!networkSettings.enableIPv6) { + EMSESP::system_.network_connected(true); + EMSESP::system_.send_heartbeat(); + EMSESP::system_.syslog_start(); + } + }); EMSESP::system_.ethernet_connected(true); } break; @@ -66,20 +76,30 @@ void WebStatusService::WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) { case SYSTEM_EVENT_ETH_DISCONNECTED: EMSESP::logger().info(F("Ethernet Disconnected")); EMSESP::system_.ethernet_connected(false); + EMSESP::system_.network_connected(false); break; case SYSTEM_EVENT_ETH_STOP: EMSESP::logger().info(F("Ethernet Stopped")); EMSESP::system_.ethernet_connected(false); + EMSESP::system_.network_connected(false); break; #ifndef EMSESP_STANDALONE case SYSTEM_EVENT_STA_CONNECTED: - WiFi.enableIpV6(); + EMSESP::esp8266React.getNetworkSettingsService()->read([&](NetworkSettings & networkSettings) { + if (networkSettings.enableIPv6) { + WiFi.enableIpV6(); + } + }); break; case SYSTEM_EVENT_ETH_CONNECTED: - ETH.enableIpV6(); + EMSESP::esp8266React.getNetworkSettingsService()->read([&](NetworkSettings & networkSettings) { + if (networkSettings.enableIPv6) { + ETH.enableIpV6(); + } + }); break; case SYSTEM_EVENT_GOT_IP6: @@ -88,6 +108,9 @@ void WebStatusService::WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) { } else { EMSESP::logger().info(F("WiFi Connected with IP=%s, hostname=%s"), WiFi.localIPv6().toString().c_str(), WiFi.getHostname()); } + EMSESP::system_.network_connected(true); + EMSESP::system_.send_heartbeat(); + EMSESP::system_.syslog_start(); break; #endif