[WIFI] Added macros for ESP - WifiGMode and WifiPower to reduce/eliminate Wifi interference on sensors like HC-SR501 (#1531)

* Fix WiFi interference on PIR

* Added to DOCS re: WiFi interference.

* Add sufficx -custom to configs without prod binaries

* Add ESP32 Wifi band and tx power to user_config.h

---------

Co-authored-by: andre <agreylin@gmail.com>
This commit is contained in:
Die Peter Panandandre authored and GitHub committed 2023-03-16 17:50:14 -05:00
1 parent d530841b0e
commit 3b4af96990
4 files changed
+101

No files matched your search

+7
View File
@@ -13,6 +13,13 @@ 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 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.
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.
### 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.
+28
View File
@@ -679,6 +679,34 @@ board_upload.port = /dev/cu.usbserial-0001
board_monitor.speed = 921600
board_monitor.port = /dev/cu.usbserial-0001
[env:esp32doitv1-aithinker-r01-sx1278-hcsr501-test]
extends = env:esp32doitv1-aithinker-r01-sx1278
lib_deps = ${env:esp32doitv1-aithinker-r01-sx1278.lib_deps}
${libraries.unifiedsensor}
build_flags =
${env:esp32doitv1-aithinker-r01-sx1278.build_flags}
'-DHCSR501_LED_NOTIFY_GPIO=LED_BUILTIN'
'-DWifiGMode=true'
'-DWifiPower=WIFI_POWER_11dBm'
'-DZsensorHCSR501="HCSR501"'
'-DHCSR501_GPIO=13'
[env:esp32doitv1-aithinker-r01-sx1278-hcsr501-ota-test]
extends = env:esp32doitv1-aithinker-r01-sx1278-hcsr501-test
upload_protocol = espota
upload_port = 10.0.0.108
upload_flags =
--auth=OTAPASSWORD
--port=8266
[env:esp32doitv1-aithinker-r01-sx1278-ota-test]
extends = env:esp32doitv1-aithinker-r01-sx1278
upload_protocol = espota
upload_port = 10.0.0.70
upload_flags =
--auth=OTAPASSWORD
--port=8266
[env:esp32dev-rtl_433]
platform = ${com.esp32_platform}
board = esp32dev
+14
View File
@@ -617,6 +617,20 @@ CRGB leds2[FASTLED_IND_NUM_LEDS];
# 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
//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
//# define WifiGMode true
# endif
# ifndef WifiPower
//# define WifiPower WIFI_POWER_11dBm
# endif
#endif
/*-----------PLACEHOLDERS FOR OLED/LCD DISPLAY--------------*/
// The real definitions are in config_M5.h / config_SSD1306.h
#define pubOled(...) // display the published message onto the OLED display
+52
View File
@@ -700,6 +700,48 @@ void callback(char* topic, byte* payload, unsigned int length) {
free(p);
}
#if defined(ESP32) && (defined(WifiGMode) || defined(WifiPower))
void setESP32WifiPorotocolTxPower() {
//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
# if WifiGMode == true
if (esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G) != ESP_OK) {
Log.error(F("Failed to change WifiMode." CR));
}
# endif
# if WifiGMode == false
if (esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N) != ESP_OK) {
Log.error(F("Failed to change WifiMode." CR));
}
# endif
uint8_t getprotocol;
esp_err_t err;
err = esp_wifi_get_protocol(WIFI_IF_STA, &getprotocol);
if (err != ESP_OK) {
Log.notice(F("Could not get protocol!" CR));
}
if (getprotocol & WIFI_PROTOCOL_11N) {
Log.notice(F("WiFi_Protocol_11n" CR));
}
if (getprotocol & WIFI_PROTOCOL_11G) {
Log.notice(F("WiFi_Protocol_11g" CR));
}
if (getprotocol & WIFI_PROTOCOL_11B) {
Log.notice(F("WiFi_Protocol_11b" CR));
}
# ifdef WifiPower
Log.notice(F("Requested WiFi power level: %i" CR), WifiPower);
WiFi.setTxPower(WifiPower);
# endif
Log.notice(F("Operating WiFi power level: %i" CR), WiFi.getTxPower());
}
#endif
void setup() {
//Launch serial for debugging purposes
Serial.begin(SERIAL_BAUD);
@@ -975,7 +1017,11 @@ bool wifi_reconnect_bypass() {
while (WiFi.waitForConnectResult() != WL_CONNECTED && wifi_autoreconnect_cnt < maxConnectionRetryWifi) {
# endif
Log.notice(F("Attempting Wifi connection with saved AP: %d" CR), wifi_autoreconnect_cnt);
WiFi.begin();
# if defined(ESP32) && (defined(WifiGMode) || defined(WifiPower))
setESP32WifiPorotocolTxPower();
# endif
delay(1000);
wifi_autoreconnect_cnt++;
}
@@ -2237,12 +2283,18 @@ void MQTTtoSYS(char* topicOri, JsonObject& SYSdata) { // json object decoding
Log.warning(F("Attempting connection to new AP" CR));
WiFi.begin((const char*)SYSdata["wifi_ssid"], (const char*)SYSdata["wifi_pass"]);
# if defined(ESP32) && (defined(WifiGMode) || defined(WifiPower))
setESP32WifiPorotocolTxPower();
# 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();
# endif
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
# if defined(ESP8266)
ESP.reset();