From 57f85ae4783c0ba64d8833e86650a908b4badda9 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Wed, 17 May 2023 10:45:00 +0200 Subject: [PATCH] Optimize dynamic WiFi power --- CHANGELOG.md | 1 + RELEASENOTES.md | 1 + tasmota/include/tasmota.h | 40 +++++++++ tasmota/tasmota_support/settings.ino | 2 +- tasmota/tasmota_support/support_command.ino | 14 ++-- tasmota/tasmota_support/support_wifi.ino | 89 +++++---------------- 6 files changed, 70 insertions(+), 77 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f1c2a616..99847f538 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file. ## [12.5.0.3] ### Added - Command ``WifiPower 0`` to enable dynamic wifi power based on RSSI by @TD-er (#15443) +- Command ``WifiPower 1`` to restore default wifi power ### Breaking Changed diff --git a/RELEASENOTES.md b/RELEASENOTES.md index f8ff40185..aacd1980e 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -115,6 +115,7 @@ The latter links can be used for OTA upgrades too like ``OtaUrl https://ota.tasm - Command ``SetOption152 0/1`` to select two (0 = default) pin bistable or one (1) pin latching relay control [#18386](https://github.com/arendst/Tasmota/issues/18386) - Command ``I2cScan0`` to scan both busses on ESP32 with one command - Command ``WifiPower 0`` to enable dynamic wifi power based on RSSI by @TD-er [#15443](https://github.com/arendst/Tasmota/issues/15443) +- Command ``WifiPower 1`` to restore default wifi power - Support for TC74 temperature sensor by Michael Loftis [#18042](https://github.com/arendst/Tasmota/issues/18042) - Support for GM861 1D and 2D bar code reader [#18399](https://github.com/arendst/Tasmota/issues/18399) - Support for PCA9557 8-bit I/O expander [#18632](https://github.com/arendst/Tasmota/issues/18632) diff --git a/tasmota/include/tasmota.h b/tasmota/include/tasmota.h index 615a5bd7d..e49193ec4 100644 --- a/tasmota/include/tasmota.h +++ b/tasmota/include/tasmota.h @@ -300,6 +300,46 @@ const uint32_t LOOP_SLEEP_DELAY = 50; // Lowest number of milliseconds to #define XPT2046_MINY 346 #define XPT2046_MAXY 3870 +#ifdef ESP32 + #if defined(CONFIG_IDF_TARGET_ESP32S2) + #define MAX_TX_PWR_DBM_11b 195 + #define MAX_TX_PWR_DBM_54g 150 + #define MAX_TX_PWR_DBM_n 130 + #define WIFI_SENSITIVITY_11b -880 + #define WIFI_SENSITIVITY_54g -750 + #define WIFI_SENSITIVITY_n -720 + #elif defined(CONFIG_IDF_TARGET_ESP32S3) + #define MAX_TX_PWR_DBM_11b 210 + #define MAX_TX_PWR_DBM_54g 190 + #define MAX_TX_PWR_DBM_n 185 + #define WIFI_SENSITIVITY_11b -880 + #define WIFI_SENSITIVITY_54g -760 + #define WIFI_SENSITIVITY_n -720 + #elif defined(CONFIG_IDF_TARGET_ESP32C3) + #define MAX_TX_PWR_DBM_11b 210 + #define MAX_TX_PWR_DBM_54g 190 + #define MAX_TX_PWR_DBM_n 185 + #define WIFI_SENSITIVITY_11b -880 + #define WIFI_SENSITIVITY_54g -760 + #define WIFI_SENSITIVITY_n -730 + #else + #define MAX_TX_PWR_DBM_11b 195 + #define MAX_TX_PWR_DBM_54g 160 + #define MAX_TX_PWR_DBM_n 140 + #define WIFI_SENSITIVITY_11b -880 + #define WIFI_SENSITIVITY_54g -750 + #define WIFI_SENSITIVITY_n -700 + #endif +#endif +#ifdef ESP8266 + #define MAX_TX_PWR_DBM_11b 200 + #define MAX_TX_PWR_DBM_54g 170 + #define MAX_TX_PWR_DBM_n 140 + #define WIFI_SENSITIVITY_11b -910 + #define WIFI_SENSITIVITY_54g -750 + #define WIFI_SENSITIVITY_n -720 +#endif + /*********************************************************************************************\ * Enumeration \*********************************************************************************************/ diff --git a/tasmota/tasmota_support/settings.ino b/tasmota/tasmota_support/settings.ino index f53abb684..7962813d5 100644 --- a/tasmota/tasmota_support/settings.ino +++ b/tasmota/tasmota_support/settings.ino @@ -929,7 +929,7 @@ void SettingsDefaultSet2(void) { flag4.network_wifi |= 1; flag3.use_wifi_scan |= WIFI_SCAN_AT_RESTART; flag3.use_wifi_rescan |= WIFI_SCAN_REGULARLY; - Settings->wifi_output_power = 170; + Settings->wifi_output_power = MAX_TX_PWR_DBM_54g; Settings->dns_timeout = DNS_TIMEOUT; Settings->param[P_ARP_GRATUITOUS] = WIFI_ARP_INTERVAL; ParseIPv4(&Settings->ipv4_address[0], PSTR(WIFI_IP_ADDRESS)); diff --git a/tasmota/tasmota_support/support_command.ino b/tasmota/tasmota_support/support_command.ino index 13269587f..14ce3d846 100644 --- a/tasmota/tasmota_support/support_command.ino +++ b/tasmota/tasmota_support/support_command.ino @@ -2526,16 +2526,16 @@ void CmndLedPwmMode(void) { void CmndWifiPower(void) { if (XdrvMailbox.data_len > 0) { Settings->wifi_output_power = (uint8_t)(CharToFloat(XdrvMailbox.data) * 10); - if (Settings->wifi_output_power > 205) { - Settings->wifi_output_power = 205; + if (10 == Settings->wifi_output_power) { + // WifiPower 1 + Settings->wifi_output_power = MAX_TX_PWR_DBM_54g; + } + else if (Settings->wifi_output_power > MAX_TX_PWR_DBM_11b) { + Settings->wifi_output_power = MAX_TX_PWR_DBM_11b; } WifiSetOutputPower(); } - if (Settings->wifi_output_power) { - ResponseCmndChar(WifiGetOutputPower().c_str()); - } else { - ResponseCmndNumber(Wifi.last_tx_pwr / 10); - } + ResponseCmndChar(WifiGetOutputPower().c_str()); } void CmndWifi(void) diff --git a/tasmota/tasmota_support/support_wifi.ino b/tasmota/tasmota_support/support_wifi.ino index 72cd5fbc5..a28a3d0e7 100644 --- a/tasmota/tasmota_support/support_wifi.ino +++ b/tasmota/tasmota_support/support_wifi.ino @@ -959,10 +959,13 @@ int WifiState(void) return state; } -String WifiGetOutputPower(void) -{ +String WifiGetOutputPower(void) { char stemp1[TOPSZ]; - dtostrfd((float)(Settings->wifi_output_power) / 10, 1, stemp1); + if (Settings->wifi_output_power) { + dtostrfd((float)(Settings->wifi_output_power) / 10, 1, stemp1); + } else { + dtostrfd((float)(Wifi.last_tx_pwr) / 10, 0, stemp1); // No decimal to indicate dynamic tx power + } return String(stemp1); } @@ -973,76 +976,33 @@ void WifiSetOutputPower(void) { } /* - Dynamic WiFi transmit power based on RSSI lowering overall DC power usage - + Dynamic WiFi transmit power based on RSSI lowering overall DC power usage. Original idea by ESPEasy (@TD-er) */ -#ifdef ESP32 - #if defined(CONFIG_IDF_TARGET_ESP32S2) - #define MAX_TX_PWR_DBM_11b 195 - #define MAX_TX_PWR_DBM_54g 150 - #define MAX_TX_PWR_DBM_n 130 - #define WIFI_SENSITIVITY_11b -880 - #define WIFI_SENSITIVITY_54g -750 - #define WIFI_SENSITIVITY_n -720 - #elif defined(CONFIG_IDF_TARGET_ESP32S3) - #define MAX_TX_PWR_DBM_11b 210 - #define MAX_TX_PWR_DBM_54g 190 - #define MAX_TX_PWR_DBM_n 185 - #define WIFI_SENSITIVITY_11b -880 - #define WIFI_SENSITIVITY_54g -760 - #define WIFI_SENSITIVITY_n -720 - #elif defined(CONFIG_IDF_TARGET_ESP32C3) - #define MAX_TX_PWR_DBM_11b 210 - #define MAX_TX_PWR_DBM_54g 190 - #define MAX_TX_PWR_DBM_n 185 - #define WIFI_SENSITIVITY_11b -880 - #define WIFI_SENSITIVITY_54g -760 - #define WIFI_SENSITIVITY_n -730 - #else - #define MAX_TX_PWR_DBM_11b 195 - #define MAX_TX_PWR_DBM_54g 160 - #define MAX_TX_PWR_DBM_n 140 - #define WIFI_SENSITIVITY_11b -880 - #define WIFI_SENSITIVITY_54g -750 - #define WIFI_SENSITIVITY_n -700 - #endif -#endif -#ifdef ESP8266 - #define MAX_TX_PWR_DBM_11b 200 - #define MAX_TX_PWR_DBM_54g 170 - #define MAX_TX_PWR_DBM_n 140 - #define WIFI_SENSITIVITY_11b -910 - #define WIFI_SENSITIVITY_54g -750 - #define WIFI_SENSITIVITY_n -720 -#endif - void WiFiSetTXpowerBasedOnRssi(void) { const WiFiMode_t cur_mode = WiFi.getMode(); if (cur_mode == WIFI_OFF) { return; } // Range ESP32 : 2dBm - 20dBm // Range ESP8266: 0dBm - 20.5dBm -// int maxTXpwr = Settings->wifi_output_power; - int maxTXpwr = 210; + int max_tx_pwr = MAX_TX_PWR_DBM_11b; int threshold = WIFI_SENSITIVITY_n; int phy_mode = WiFi.getPhyMode(); switch (phy_mode) { case 1: // 11b (WIFI_PHY_MODE_11B) threshold = WIFI_SENSITIVITY_11b; - if (maxTXpwr > MAX_TX_PWR_DBM_11b) maxTXpwr = MAX_TX_PWR_DBM_11b; + if (max_tx_pwr > MAX_TX_PWR_DBM_11b) max_tx_pwr = MAX_TX_PWR_DBM_11b; break; case 2: // 11bg (WIFI_PHY_MODE_11G) threshold = WIFI_SENSITIVITY_54g; - if (maxTXpwr > MAX_TX_PWR_DBM_54g) maxTXpwr = MAX_TX_PWR_DBM_54g; + if (max_tx_pwr > MAX_TX_PWR_DBM_54g) max_tx_pwr = MAX_TX_PWR_DBM_54g; break; case 3: // 11bgn (WIFI_PHY_MODE_11N) threshold = WIFI_SENSITIVITY_n; - if (maxTXpwr > MAX_TX_PWR_DBM_n) maxTXpwr = MAX_TX_PWR_DBM_n; + if (max_tx_pwr > MAX_TX_PWR_DBM_n) max_tx_pwr = MAX_TX_PWR_DBM_n; break; } threshold += 30; // Margin in dBm * 10 on top of threshold - int minTXpwr = 0; // Assume AP sends with max set by ETSI standard. // 2.4 GHz: 100 mWatt (20 dBm) @@ -1050,30 +1010,21 @@ void WiFiSetTXpowerBasedOnRssi(void) { int rssi = WiFi.RSSI() * 10; int newrssi = rssi - 200; // We cannot send with over 20 dBm, thus it makes no sense to force higher TX power all the time. + int min_tx_pwr = 0; if (newrssi < threshold) { - minTXpwr = threshold - newrssi; + min_tx_pwr = threshold - newrssi; } - if (minTXpwr > maxTXpwr) { - minTXpwr = maxTXpwr; + if (min_tx_pwr > max_tx_pwr) { + min_tx_pwr = max_tx_pwr; } - int dBm = 0; - if (dBm > maxTXpwr) { - dBm = maxTXpwr; - } else if (dBm < minTXpwr) { - dBm = minTXpwr; - } - WiFi.setOutputPower((float)dBm / 10); - - delay(Wifi.last_tx_pwr < dBm); // If increase the TX power, give power supply of the unit some rest - Wifi.last_tx_pwr = dBm; - + WiFi.setOutputPower((float)min_tx_pwr / 10); + delay(Wifi.last_tx_pwr < min_tx_pwr); // If increase the TX power, give power supply of the unit some rest /* - int TX_pwr_int = Wifi.last_tx_pwr * 4; - int maxTXpwr_int = maxTXpwr * 4; - if (TX_pwr_int != maxTXpwr_int) { - AddLog(LOG_LEVEL_DEBUG, PSTR("WIF: TX power %d, Sensitivity %d, RSSI %d"), dBm / 10, threshold/ 10, rssi / 10); + if (Wifi.last_tx_pwr != min_tx_pwr) { + AddLog(LOG_LEVEL_DEBUG, PSTR("WIF: TX power %d, Sensitivity %d, RSSI %d"), min_tx_pwr / 10, threshold/ 10, rssi / 10); } */ + Wifi.last_tx_pwr = min_tx_pwr; } /*