diff --git a/docs/use/boards.md b/docs/use/boards.md index 8d3cef5e6c..9015027a98 100644 --- a/docs/use/boards.md +++ b/docs/use/boards.md @@ -13,12 +13,12 @@ On M5Stack boards you may do a long press to these buttons in low power mode 0 ( You can also do a long press when powering the board to reset it, this press must be done during the first 5 seconds after the start. -### WiFi interference on ESP32 ### -Certain sensors like HC-SR501 is prone to generate false signals / triggers when used on a ESP32 with WiFi enabled. To reduce or eliminate the effect the board must be put into WiFi B/G with lower TX power. +### Wifi interference on sensors when using an ESP ### +Certain sensors like HC-SR501 is prone to generate false signals / triggers when used on a ESP with Wifi enabled. To reduce or elimate the effect the board must be put into Wifi B/G with lower TX power. -This can be achieved with the following macro, `WifiGMode` defined true and `WifiPower` to e.g. WIFI_POWER_11dBm. +This can be achieved with the following macro, `WifiGMode` defined true and `WifiPower` to e.g. WIFI_POWER_11dBm (ESP32) or 11 (ESP8266). -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. +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. ### Low power mode for ESP32 OpenMQTTGateway support a low power mode for ESP32, this mode is available per default on boards with batteries. The other boards needs to have the macro `DEFAULT_LOW_POWER_MODE` defined at 0, 1 or 2 to use it. More information about the modes is available into User_config.h. diff --git a/environments.ini b/environments.ini index 146121ead6..108afb4c2a 100644 --- a/environments.ini +++ b/environments.ini @@ -1059,6 +1059,8 @@ build_flags = '-DsimplePublishing=true' '-DGateway_Name="OMG_ESP8266_ALL"' '-DDEEP_SLEEP_IN_US=120000000' + '-DWifiGMode=true' + '-DWifiPower=11' board_build.flash_mode = dout [env:nodemcuv2-fastled-test] diff --git a/main/User_config.h b/main/User_config.h index 13d1c3bbb0..78be725cf1 100644 --- a/main/User_config.h +++ b/main/User_config.h @@ -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--------------*/ diff --git a/main/main.ino b/main/main.ino index 4dcbc7bee4..1f6616e5e8 100644 --- a/main/main.ino +++ b/main/main.ino @@ -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);