Optimize dynamic WiFi power

This commit is contained in:
Theo Arends 2023-05-17 10:45:00 +02:00
parent 4e5c62ce33
commit 57f85ae478
6 changed files with 70 additions and 77 deletions

View File

@ -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

View File

@ -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)

View File

@ -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
\*********************************************************************************************/

View File

@ -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));

View File

@ -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)

View File

@ -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;
}
/*