[SYS] Extend Wifi mode and power change to ESP8266 (#1680)
Extend Wifi mode and power change to ESP8266 (Fix the spelling of setESPWifiProtocolTxPower)
This commit is contained in:
1 parent
4d8ba2a680
commit
959c3afc6f
4 files changed
+63
-20
No files matched your search
+8
-9
@@ -688,18 +688,17 @@ Adafruit_NeoPixel leds2(ANEOPIX_IND_NUM_LEDS, ANEOPIX_IND_DATA_GPIO2, ANEOPIX_IN
|
||||
# define LOG_LEVEL LOG_LEVEL_NOTICE
|
||||
#endif
|
||||
|
||||
/*-------------------ESP32 Wifi band and tx power ---------------------*/
|
||||
//Certain sensors are sensitive to ESP32 Wifi which can cause interference with their normal operation
|
||||
/*-------------------ESP Wifi band and tx power ---------------------*/
|
||||
//Certain sensors are sensitive to Wifi which can cause interference with their normal operation
|
||||
//For example it can cause false triggers on a PIR HC-SR501
|
||||
//It is reccomended to change Wifi BAND to G and reduce tx power level to 11dBm
|
||||
//Since the WiFi protocol is persisted in the flash of the ESP32 you have to run at least once with `WiFiGMode` defined false to get Band N back.
|
||||
#ifdef ESP32
|
||||
# ifndef WifiGMode
|
||||
//Since the WiFi protocol is persisted in the flash of the ESP you have to run at least once with `WiFiGMode` defined false to get Band N back.
|
||||
#ifndef WifiGMode
|
||||
//# define WifiGMode true
|
||||
# endif
|
||||
# ifndef WifiPower
|
||||
//# define WifiPower WIFI_POWER_11dBm
|
||||
# endif
|
||||
#endif
|
||||
#ifndef WifiPower
|
||||
//# define WifiPower WIFI_POWER_11dBm //When using an ESP32
|
||||
//# define WifiPower 11 //When using an ESP8266
|
||||
#endif
|
||||
|
||||
/*-----------PLACEHOLDERS FOR WebUI DISPLAY--------------*/
|
||||
|
||||
+49
-7
@@ -722,7 +722,7 @@ void callback(char* topic, byte* payload, unsigned int length) {
|
||||
}
|
||||
|
||||
#if defined(ESP32) && (defined(WifiGMode) || defined(WifiPower))
|
||||
void setESP32WifiPorotocolTxPower() {
|
||||
void setESPWifiProtocolTxPower() {
|
||||
//Reduce WiFi interference when using ESP32 using custom WiFi mode and tx power
|
||||
//https://github.com/espressif/arduino-esp32/search?q=WIFI_PROTOCOL_11G
|
||||
//https://www.letscontrolit.com/forum/viewtopic.php?t=671&start=20
|
||||
@@ -763,6 +763,48 @@ void setESP32WifiPorotocolTxPower() {
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(ESP8266) && (defined(WifiGMode) || defined(WifiPower))
|
||||
void setESPWifiProtocolTxPower() {
|
||||
# if WifiGMode == true
|
||||
if (!wifi_set_phy_mode(PHY_MODE_11G)) {
|
||||
Log.error(F("Failed to change WifiMode." CR));
|
||||
}
|
||||
# endif
|
||||
|
||||
# if WifiGMode == false
|
||||
if (!wifi_set_phy_mode(PHY_MODE_11N)) {
|
||||
Log.error(F("Failed to change WifiMode." CR));
|
||||
}
|
||||
# endif
|
||||
|
||||
phy_mode_t getprotocol = wifi_get_phy_mode();
|
||||
if (getprotocol == PHY_MODE_11N) {
|
||||
Log.notice(F("WiFi_Protocol_11n" CR));
|
||||
}
|
||||
if (getprotocol == PHY_MODE_11G) {
|
||||
Log.notice(F("WiFi_Protocol_11g" CR));
|
||||
}
|
||||
if (getprotocol == PHY_MODE_11B) {
|
||||
Log.notice(F("WiFi_Protocol_11b" CR));
|
||||
}
|
||||
|
||||
# ifdef WifiPower
|
||||
Log.notice(F("Requested WiFi power level: %i dBm" CR), WifiPower);
|
||||
|
||||
int i_dBm = int(WifiPower * 4.0f);
|
||||
|
||||
// i_dBm 82 == 20.5 dBm
|
||||
if (i_dBm > 82) {
|
||||
i_dBm = 82;
|
||||
} else if (i_dBm < 0) {
|
||||
i_dBm = 0;
|
||||
}
|
||||
|
||||
system_phy_set_max_tpw((uint8_t)i_dBm);
|
||||
# endif
|
||||
}
|
||||
#endif
|
||||
|
||||
void setup() {
|
||||
//Launch serial for debugging purposes
|
||||
Serial.begin(SERIAL_BAUD);
|
||||
@@ -1088,8 +1130,8 @@ bool wifi_reconnect_bypass() {
|
||||
Log.notice(F("Attempting Wifi connection with saved AP: %d" CR), wifi_autoreconnect_cnt);
|
||||
|
||||
WiFi.begin();
|
||||
# if defined(ESP32) && (defined(WifiGMode) || defined(WifiPower))
|
||||
setESP32WifiPorotocolTxPower();
|
||||
# if (defined(ESP8266) || defined(ESP32)) && (defined(WifiGMode) || defined(WifiPower))
|
||||
setESPWifiProtocolTxPower();
|
||||
# endif
|
||||
delay(1000);
|
||||
wifi_autoreconnect_cnt++;
|
||||
@@ -2492,8 +2534,8 @@ void MQTTtoSYS(char* topicOri, JsonObject& SYSdata) { // json object decoding
|
||||
|
||||
Log.warning(F("Attempting connection to new AP %s" CR), (const char*)SYSdata["wifi_ssid"]);
|
||||
WiFi.begin((const char*)SYSdata["wifi_ssid"], (const char*)SYSdata["wifi_pass"]);
|
||||
# if defined(ESP32) && (defined(WifiGMode) || defined(WifiPower))
|
||||
setESP32WifiPorotocolTxPower();
|
||||
# if (defined(ESP8266) || defined(ESP32)) && (defined(WifiGMode) || defined(WifiPower))
|
||||
setESPWifiProtocolTxPower();
|
||||
# endif
|
||||
WiFi.waitForConnectResult();
|
||||
|
||||
@@ -2501,8 +2543,8 @@ void MQTTtoSYS(char* topicOri, JsonObject& SYSdata) { // json object decoding
|
||||
Log.error(F("Failed to connect to new AP; falling back" CR));
|
||||
WiFi.disconnect(true);
|
||||
WiFi.begin(prev_ssid.c_str(), prev_pass.c_str());
|
||||
# if defined(ESP32) && (defined(WifiGMode) || defined(WifiPower))
|
||||
setESP32WifiPorotocolTxPower();
|
||||
# if (defined(ESP8266) || defined(ESP32)) && (defined(WifiGMode) || defined(WifiPower))
|
||||
setESPWifiProtocolTxPower();
|
||||
# endif
|
||||
}
|
||||
ESPRestart(7);
|
||||
|
||||
Reference in new issue
Block a user