fixes for txPower

This commit is contained in:
Proddy
2024-02-11 23:01:32 +01:00
parent 7c97aaf735
commit 65cf8005a4
3 changed files with 46 additions and 46 deletions

View File

@@ -38,10 +38,11 @@ ESP8266React::ESP8266React(AsyncWebServer * server, FS * fs)
AsyncWebServerResponse * response = request->beginResponse_P(200, contentType, content, len); AsyncWebServerResponse * response = request->beginResponse_P(200, contentType, content, len);
response->addHeader("Content-Encoding", "gzip"); response->addHeader("Content-Encoding", "gzip");
// response->addHeader("Content-Encoding", "br"); // only works over HTTPS
// response->addHeader("Cache-Control", "public, immutable, max-age=31536000"); // response->addHeader("Cache-Control", "public, immutable, max-age=31536000");
response->addHeader("Last-Modified", last_modified); response->addHeader("Last-Modified", last_modified);
response->addHeader("ETag", hash); response->addHeader("ETag", hash);
// response->addHeader("Content-Encoding", "br"); // only works over HTTPS
request->send(response); request->send(response);
}; };

View File

@@ -88,32 +88,25 @@ void NetworkSettingsService::manageSTA() {
} }
#ifdef BOARD_C3_MINI_V1 #ifdef BOARD_C3_MINI_V1
// hardcode Tx power for Wemos CS Mini v1 // always hardcode Tx power for Wemos CS Mini v1
// v1 needs this value, see https://github.com/emsesp/EMS-ESP32/pull/620#discussion_r993173979 // v1 needs this value, see https://github.com/emsesp/EMS-ESP32/pull/620#discussion_r993173979
// https://www.wemos.cc/en/latest/c3/c3_mini_1_0_0.html#about-wifi // https://www.wemos.cc/en/latest/c3/c3_mini_1_0_0.html#about-wifi
WiFi.setTxPower(WIFI_POWER_8_5dBm); WiFi.setTxPower(WIFI_POWER_8_5dBm);
return; return;
#else
if (_state.tx_power != 0) {
// if not set to Auto (0) set the Tx power now
WiFi.setTxPower((wifi_power_t)_state.tx_power);
}
#endif #endif
} else { // not connected but STA-mode active => disconnect } else { // not connected but STA-mode active => disconnect
reconfigureWiFiConnection(); reconfigureWiFiConnection();
} }
} }
// set the Tx WiFi power // set the TxPower based on the RSSI (signal strength), picking the lowest value
void NetworkSettingsService::setWiFiPower() { // code is based of RSSI (signal strength) and copied from Tasmota's WiFiSetTXpowerBasedOnRssi() which is copied ESPEasy's ESPEasyWifi.SetWiFiTXpower() function
// hardcode Tx power for Wemos CS Mini v1 void NetworkSettingsService::setWiFiPowerOnRSSI() {
// v1 needs this value, see https://github.com/emsesp/EMS-ESP32/pull/620#discussion_r993173979
// https://www.wemos.cc/en/latest/c3/c3_mini_1_0_0.html#about-wifi
#ifdef BOARD_C3_MINI_V1
WiFi.setTxPower(WIFI_POWER_8_5dBm);
return;
#endif
auto set_power = _state.tx_power; // get user settings. 0 means auto
// If Auto set the TxPower based on the RSSI (signal strength), picking the lowest value
// based of RSSI (signal strength) and copied from Tasmota's WiFiSetTXpowerBasedOnRssi() which is copied ESPEasy's ESPEasyWifi.SetWiFiTXpower() function
if (set_power == 0) {
// Range ESP32 : 2dBm - 20dBm // Range ESP32 : 2dBm - 20dBm
// 802.11b - wifi1 // 802.11b - wifi1
// 802.11a - wifi2 // 802.11a - wifi2
@@ -121,9 +114,9 @@ void NetworkSettingsService::setWiFiPower() {
// 802.11n - wifi4 // 802.11n - wifi4
// 802.11ac - wifi5 // 802.11ac - wifi5
// 802.11ax - wifi6 // 802.11ax - wifi6
int max_tx_pwr = MAX_TX_PWR_DBM_n; // assume wifi4 int max_tx_pwr = MAX_TX_PWR_DBM_n; // assume wifi4
int threshold = WIFI_SENSITIVITY_n; int threshold = WIFI_SENSITIVITY_n + 30; // Margin in dBm * 10 on top of threshold
threshold += 30; // Margin in dBm * 10 on top of threshold
// Assume AP sends with max set by ETSI standard. // Assume AP sends with max set by ETSI standard.
// 2.4 GHz: 100 mWatt (20 dBm) // 2.4 GHz: 100 mWatt (20 dBm)
@@ -139,13 +132,9 @@ void NetworkSettingsService::setWiFiPower() {
min_tx_pwr = max_tx_pwr; min_tx_pwr = max_tx_pwr;
} }
set_power = min_tx_pwr / 10; // this is the recommended power setting to use uint8_t set_power = min_tx_pwr / 10; // this is the recommended power setting to use
#ifdef EMSESP_DEBUG
emsesp::EMSESP::logger().debug("Recommended set WiFi Tx Power (set_power %d, rssi %d, threshold %d", set_power, rssi, threshold);
#endif
}
// use the user setting, which is a value from WiFIGeneric.h // from WiFIGeneric.h use:
// WIFI_POWER_19_5dBm = 78,// 19.5dBm // WIFI_POWER_19_5dBm = 78,// 19.5dBm
// WIFI_POWER_19dBm = 76,// 19dBm // WIFI_POWER_19dBm = 76,// 19dBm
// WIFI_POWER_18_5dBm = 74,// 18.5dBm // WIFI_POWER_18_5dBm = 74,// 18.5dBm
@@ -178,6 +167,13 @@ void NetworkSettingsService::setWiFiPower() {
else if (set_power >= 5) else if (set_power >= 5)
p = WIFI_POWER_5dBm; p = WIFI_POWER_5dBm;
#ifdef EMSESP_DEBUG
emsesp::EMSESP::logger().debug("Recommended set WiFi Tx Power (set_power %d, new power %d, rssi %d, threshold %d", set_power, p, rssi, threshold);
#else
char result[10];
emsesp::EMSESP::logger().info("Setting WiFi Tx Power to %s dBm", emsesp::Helpers::render_value(result, (double)(p / 4), 1));
#endif
WiFi.setTxPower(p); WiFi.setTxPower(p);
} }
@@ -347,7 +343,10 @@ void NetworkSettingsService::WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info)
case ARDUINO_EVENT_WIFI_STA_CONNECTED: case ARDUINO_EVENT_WIFI_STA_CONNECTED:
// Set the TxPower after the connection is established // Set the TxPower after the connection is established
setWiFiPower(); // if we're using TxPower = 0 (Auto)
if (_state.tx_power == 0) {
setWiFiPowerOnRSSI();
}
if (_state.enableIPv6) { if (_state.enableIPv6) {
WiFi.enableIpV6(); WiFi.enableIpV6();
} }

View File

@@ -108,7 +108,7 @@ class NetworkSettingsService : public StatefulService<NetworkSettings> {
const char * disconnectReason(uint8_t code); const char * disconnectReason(uint8_t code);
void reconfigureWiFiConnection(); void reconfigureWiFiConnection();
void manageSTA(); void manageSTA();
void setWiFiPower(); void setWiFiPowerOnRSSI();
}; };
#endif #endif