From 26501b2176aa3858d109e2f6b1ed01723ec47480 Mon Sep 17 00:00:00 2001 From: Florian <1technophile@users.noreply.github.com> Date: Wed, 8 Apr 2020 15:14:14 -0500 Subject: [PATCH] Recover ESP8266 build and ESPWifiManualSetup #563 Move protocol change method outside of wifimanager scope and correct build error for ESP8266 --- main/User_config.h | 2 +- main/main.ino | 42 +++++++++++++++++++++--------------------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/main/User_config.h b/main/User_config.h index e9738ee..6a1080b 100644 --- a/main/User_config.h +++ b/main/User_config.h @@ -107,7 +107,6 @@ char mqtt_topic[mqtt_topic_max_size] = Base_Topic; char gateway_name[parameters_size * 2] = Gateway_Name; #if defined(ESP8266)||defined(ESP32) - #include "esp_wifi.h" #define ATTEMPTS_BEFORE_BG 10 // Number of wifi connection attempts before going to BG protocol #define ATTEMPTS_BEFORE_B 20 // Number of wifi connection attempts before going to B protocol #endif @@ -115,6 +114,7 @@ char gateway_name[parameters_size * 2] = Gateway_Name; // WIFI mode, uncomment to force a wifi mode, if not uncommented the ESP will connect without a mode forced // if there is a reconnection issue it will try to connect with G mode and if not working with B mode #ifdef ESP32 + #include "esp_wifi.h" uint8_t wifiProtocol = 0; // default mode, automatic selection //uint8_t wifiProtocol = WIFI_PROTOCOL_11B; //uint8_t wifiProtocol = WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G; // can't have only one https://github.com/espressif/esp-idf/issues/702 diff --git a/main/main.ino b/main/main.ino index 30f6014..97a350a 100644 --- a/main/main.ino +++ b/main/main.ino @@ -134,7 +134,6 @@ int failure_number_mqtt = 0; // number of failure connecting to MQTT #elif defined(ESP8266) #include #include - #include "esp_wifi.h" #include #include #include @@ -753,6 +752,27 @@ void setup() Log.notice(F("Setup OpenMQTTGateway end" CR)); } +#if defined(ESP8266) || defined(ESP32) +// the 2 methods below are used to recover wifi connection by changing the protocols +void forceWifiProtocol(){ +#ifdef ESP32 + Log.trace(F("ESP32: Forcing to wifi %d" CR), wifiProtocol); + esp_wifi_set_protocol(WIFI_IF_STA, wifiProtocol); +#elif ESP8266 + Log.trace(F("ESP8266: Forcing to wifi %d" CR), wifiProtocol); + WiFi.setPhyMode((WiFiPhyMode_t)wifiProtocol); +#endif +} + +void reinit_wifi() +{ + delay(10); + WiFi.mode(WIFI_STA); + if (!wifiProtocol) forceWifiProtocol(); + WiFi.begin(); +} +#endif + #if defined(ESPWifiManualSetup) void setup_wifi() { @@ -843,16 +863,6 @@ void eraseAndRestart() #endif } -void forceWifiProtocol(){ -#ifdef ESP32 - Log.trace(F("ESP32: Forcing to wifi %d" CR), wifiProtocol); - esp_wifi_set_protocol(WIFI_IF_STA, wifiProtocol); -#elif ESP8266 - Log.trace(F("ESP8266: Forcing to wifi %d" CR), wifiProtocol); - WiFi.setPhyMode(wifiProtocol); -#endif -} - void setup_wifimanager(bool reset_settings) { @@ -1010,16 +1020,6 @@ void setup_wifimanager(bool reset_settings) } } -void reinit_wifi() -{ - - delay(10); - WiFi.mode(WIFI_STA); - if (!wifiProtocol) forceWifiProtocol(); - WiFi.begin(); - -} - #else // Arduino case void setup_ethernet() {