Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend Wifi mode and power change to ESP8266 #1680

Merged
merged 2 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/use/boards.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions environments.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
17 changes: 8 additions & 9 deletions main/User_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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--------------*/
Expand Down
56 changes: 49 additions & 7 deletions main/main.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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++;
Expand Down Expand Up @@ -2492,17 +2534,17 @@ 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();

if (WiFi.status() != WL_CONNECTED) {
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);
Expand Down