From 69d8fa92543c63e86cfb3bf9dc9ac393d4743724 Mon Sep 17 00:00:00 2001 From: Gunnar Skjold Date: Thu, 27 Oct 2022 19:17:50 +0200 Subject: [PATCH] Made wifi sleep mode configurable --- src/AmsConfiguration.cpp | 4 ++++ src/AmsConfiguration.h | 3 ++- src/AmsToMqttBridge.ino | 4 +++- src/web/AmsWebServer.cpp | 4 ++++ web/wifi.html | 26 ++++++++++++++++++++------ 5 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/AmsConfiguration.cpp b/src/AmsConfiguration.cpp index aa09044e..93999f3b 100644 --- a/src/AmsConfiguration.cpp +++ b/src/AmsConfiguration.cpp @@ -24,6 +24,7 @@ bool AmsConfiguration::getWiFiConfig(WiFiConfig& config) { EEPROM.begin(EEPROM_SIZE); EEPROM.get(CONFIG_WIFI_START, config); EEPROM.end(); + if(config.sleep > 2) config.sleep = 0xFF; return true; } else { clearWifi(config); @@ -33,6 +34,7 @@ bool AmsConfiguration::getWiFiConfig(WiFiConfig& config) { bool AmsConfiguration::setWiFiConfig(WiFiConfig& config) { WiFiConfig existing; + if(config.sleep > 2) config.sleep = 0xFF; if(getWiFiConfig(existing)) { wifiChanged |= strcmp(config.ssid, existing.ssid) != 0; wifiChanged |= strcmp(config.psk, existing.psk) != 0; @@ -45,6 +47,7 @@ bool AmsConfiguration::setWiFiConfig(WiFiConfig& config) { } wifiChanged |= strcmp(config.hostname, existing.hostname) != 0; wifiChanged |= config.power != existing.power; + wifiChanged |= config.sleep != existing.sleep; } else { wifiChanged = true; } @@ -70,6 +73,7 @@ void AmsConfiguration::clearWifi(WiFiConfig& config) { #endif strcpy(config.hostname, (String("ams-") + String(chipId, HEX)).c_str()); config.mdns = true; + config.sleep = 0xFF; } void AmsConfiguration::clearWifiIp(WiFiConfig& config) { diff --git a/src/AmsConfiguration.h b/src/AmsConfiguration.h index f0ad1098..d5e09344 100644 --- a/src/AmsConfiguration.h +++ b/src/AmsConfiguration.h @@ -54,7 +54,8 @@ struct WiFiConfig { char hostname[32]; bool mdns; uint8_t power; -}; // 210 + uint8_t sleep; +}; // 211 struct MqttConfig86 { char host[128]; diff --git a/src/AmsToMqttBridge.ino b/src/AmsToMqttBridge.ino index bfc9a6d8..3d75ebd7 100644 --- a/src/AmsToMqttBridge.ino +++ b/src/AmsToMqttBridge.ino @@ -1054,7 +1054,6 @@ void WiFi_connect() { } #endif WiFi.mode(WIFI_STA); - WiFi.setSleep(WIFI_PS_MAX_MODEM); #if defined(ESP32) if(wifi.power >= 195) WiFi.setTxPower(WIFI_POWER_19_5dBm); @@ -1115,6 +1114,9 @@ void WiFi_connect() { WiFi.setAutoReconnect(true); WiFi.persistent(true); if(WiFi.begin(wifi.ssid, wifi.psk)) { + if(wifi.sleep <= 2) { + WiFi.setSleep(wifi.sleep); + } yield(); } else { if (Debug.isActive(RemoteDebug::ERROR)) debugI("Unable to start WiFi"); diff --git a/src/web/AmsWebServer.cpp b/src/web/AmsWebServer.cpp index 850a2090..ff28d2d6 100644 --- a/src/web/AmsWebServer.cpp +++ b/src/web/AmsWebServer.cpp @@ -444,6 +444,9 @@ void AmsWebServer::configWifiHtml() { html.replace(F("{h}"), wifi.hostname); html.replace(F("{m}"), wifi.mdns ? F("checked") : F("")); html.replace(F("{w}"), String(wifi.power / 10.0, 1)); + html.replace(F("{z0}"), wifi.sleep == 0 ? "selected" : ""); + html.replace(F("{z1}"), wifi.sleep == 1 ? "selected" : ""); + html.replace(F("{z2}"), wifi.sleep == 2 ? "selected" : ""); #if defined(ESP32) html.replace(F("{wm}"), "19.5"); #elif defined(ESP8266) @@ -1256,6 +1259,7 @@ void AmsWebServer::handleSave() { strcpy(wifi.hostname, server.arg(F("h")).c_str()); } wifi.power = server.arg(F("w")).toFloat() * 10; + wifi.sleep = server.arg(F("z")).toInt(); config->setWiFiConfig(wifi); } diff --git a/web/wifi.html b/web/wifi.html index 83db137e..ae3c5b32 100644 --- a/web/wifi.html +++ b/web/wifi.html @@ -37,7 +37,7 @@
WiFi
-
+
IP @@ -45,7 +45,7 @@
WiFi
-
+
Netmask @@ -53,7 +53,7 @@
WiFi
-
+
Gateway @@ -61,7 +61,7 @@
WiFi
-
+
DNS 1 @@ -69,7 +69,7 @@
WiFi
-
+
DNS 2 @@ -89,7 +89,21 @@
WiFi
-
+ +
+
+
+ Power saving +
+ +
+
+