From 5cfb58c2e677ea14be617cb6c36957a9733a0d4c Mon Sep 17 00:00:00 2001 From: Gunnar Skjold Date: Sat, 29 Jan 2022 20:34:35 +0100 Subject: [PATCH] Expose TX power adjustment --- src/AmsConfiguration.cpp | 40 ++++++++++++++++++++++++++++++++++++---- src/AmsConfiguration.h | 8 +++++--- src/AmsToMqttBridge.ino | 26 ++++++++++++++++++++++++++ src/web/AmsWebServer.cpp | 7 +++++++ web/wifi.html | 12 ++++++++++++ 5 files changed, 86 insertions(+), 7 deletions(-) diff --git a/src/AmsConfiguration.cpp b/src/AmsConfiguration.cpp index 2b6e43f8..615a8004 100644 --- a/src/AmsConfiguration.cpp +++ b/src/AmsConfiguration.cpp @@ -37,11 +37,14 @@ bool AmsConfiguration::setWiFiConfig(WiFiConfig& config) { wifiChanged |= strcmp(config.ssid, existing.ssid) != 0; wifiChanged |= strcmp(config.psk, existing.psk) != 0; wifiChanged |= strcmp(config.ip, existing.ip) != 0; - wifiChanged |= strcmp(config.gateway, existing.gateway) != 0; - wifiChanged |= strcmp(config.subnet, existing.subnet) != 0; - wifiChanged |= strcmp(config.dns1, existing.dns1) != 0; - wifiChanged |= strcmp(config.dns2, existing.dns2) != 0; + if(strlen(config.ip) > 0) { + wifiChanged |= strcmp(config.gateway, existing.gateway) != 0; + wifiChanged |= strcmp(config.subnet, existing.subnet) != 0; + wifiChanged |= strcmp(config.dns1, existing.dns1) != 0; + wifiChanged |= strcmp(config.dns2, existing.dns2) != 0; + } wifiChanged |= strcmp(config.hostname, existing.hostname) != 0; + wifiChanged |= config.power != existing.power; } else { wifiChanged = true; } @@ -627,6 +630,14 @@ bool AmsConfiguration::hasConfig() { configVersion = 0; return false; } + case 92: + configVersion = -1; // Prevent loop + if(relocateConfig92()) { + configVersion = 93; + } else { + configVersion = 0; + return false; + } case EEPROM_CHECK_SUM: return true; default: @@ -755,6 +766,27 @@ bool AmsConfiguration::relocateConfig91() { return ret; } +bool AmsConfiguration::relocateConfig92() { + WiFiConfig wifi; + EEPROM.begin(EEPROM_SIZE); + EEPROM.get(CONFIG_WIFI_START, wifi); + #if defined(ESP32) + wifi.power = 195; + #elif defined(ESP8266) + wifi.power = 205; + #endif + EEPROM.put(CONFIG_WIFI_START, wifi); + + EnergyAccountingConfig eac; + clearEnergyAccountingConfig(eac); + EEPROM.put(CONFIG_ENERGYACCOUNTING_START, eac); + + EEPROM.put(EEPROM_CONFIG_ADDRESS, 93); + bool ret = EEPROM.commit(); + EEPROM.end(); + return ret; +} + bool AmsConfiguration::save() { EEPROM.begin(EEPROM_SIZE); EEPROM.put(EEPROM_CONFIG_ADDRESS, EEPROM_CHECK_SUM); diff --git a/src/AmsConfiguration.h b/src/AmsConfiguration.h index e2d1ed77..5b42d689 100644 --- a/src/AmsConfiguration.h +++ b/src/AmsConfiguration.h @@ -4,7 +4,7 @@ #include "Arduino.h" #define EEPROM_SIZE 1024*3 -#define EEPROM_CHECK_SUM 92 // Used to check if config is stored. Change if structure changes +#define EEPROM_CHECK_SUM 93 // Used to check if config is stored. Change if structure changes #define EEPROM_CONFIG_ADDRESS 0 #define EEPROM_TEMP_CONFIG_ADDRESS 2048 @@ -13,7 +13,7 @@ #define CONFIG_GPIO_START 266 #define CONFIG_ENTSOE_START 290 #define CONFIG_WIFI_START 360 -#define CONFIG_ENERGYACCOUNTING_START 520 +#define CONFIG_ENERGYACCOUNTING_START 576 #define CONFIG_WEB_START 648 #define CONFIG_DEBUG_START 824 #define CONFIG_DOMOTICZ_START 856 @@ -52,7 +52,8 @@ struct WiFiConfig { char dns2[16]; char hostname[32]; bool mdns; -}; // 209 + uint8_t power; +}; // 210 struct MqttConfig86 { char host[128]; @@ -257,6 +258,7 @@ private: bool relocateConfig87(); // 1.5.4 bool relocateConfig90(); // 2.0.0 bool relocateConfig91(); // 2.0.2 + bool relocateConfig92(); // 2.0.3 int readString(int pAddress, char* pString[]); int readInt(int pAddress, int *pValue); diff --git a/src/AmsToMqttBridge.ino b/src/AmsToMqttBridge.ino index ad9862b2..8c741158 100644 --- a/src/AmsToMqttBridge.ino +++ b/src/AmsToMqttBridge.ino @@ -988,6 +988,32 @@ void WiFi_connect() { wifiReconnectCount++; WiFi.mode(WIFI_STA); + #if defined(ESP32) + if(wifi.power >= 195) + WiFi.setTxPower(WIFI_POWER_19_5dBm); + else if(wifi.power >= 190) + WiFi.setTxPower(WIFI_POWER_19dBm); + else if(wifi.power >= 185) + WiFi.setTxPower(WIFI_POWER_18_5dBm); + else if(wifi.power >= 170) + WiFi.setTxPower(WIFI_POWER_17dBm); + else if(wifi.power >= 150) + WiFi.setTxPower(WIFI_POWER_15dBm); + else if(wifi.power >= 130) + WiFi.setTxPower(WIFI_POWER_13dBm); + else if(wifi.power >= 110) + WiFi.setTxPower(WIFI_POWER_11dBm); + else if(wifi.power >= 85) + WiFi.setTxPower(WIFI_POWER_8_5dBm); + else if(wifi.power >= 70) + WiFi.setTxPower(WIFI_POWER_7dBm); + else if(wifi.power >= 50) + WiFi.setTxPower(WIFI_POWER_5dBm); + else if(wifi.power >= 20) + WiFi.setTxPower(WIFI_POWER_2dBm); + #elif defined(ESP8266) + WiFi.setOutputPower(wifi.power / 10.0); + #endif if(strlen(wifi.ip) > 0) { IPAddress ip, gw, sn(255,255,255,0), dns1, dns2; ip.fromString(wifi.ip); diff --git a/src/web/AmsWebServer.cpp b/src/web/AmsWebServer.cpp index a02af330..e068abfd 100644 --- a/src/web/AmsWebServer.cpp +++ b/src/web/AmsWebServer.cpp @@ -514,6 +514,12 @@ void AmsWebServer::configWifiHtml() { } html.replace("{h}", wifi.hostname); html.replace("{m}", wifi.mdns ? "checked" : ""); + html.replace("{w}", String(wifi.power / 10.0, 1)); + #if defined(ESP32) + html.replace("{wm}", "19.5"); + #elif defined(ESP8266) + html.replace("{wm}", "20.5"); + #endif server.setContentLength(html.length() + HEAD_HTML_LEN + FOOT_HTML_LEN); server.send_P(200, "text/html", HEAD_HTML); @@ -1187,6 +1193,7 @@ void AmsWebServer::handleSave() { if(server.hasArg("h") && !server.arg("h").isEmpty()) { strcpy(wifi.hostname, server.arg("h").c_str()); } + wifi.power = server.arg("w").toFloat() * 10; config->setWiFiConfig(wifi); } diff --git a/web/wifi.html b/web/wifi.html index 0b6b6aa2..c126da27 100644 --- a/web/wifi.html +++ b/web/wifi.html @@ -77,6 +77,18 @@ + +
+
+
+ Power +
+ +
+ dBm +
+
+