Clean up wifi connection / disconnection

This commit is contained in:
Theo Arends 2020-02-26 13:45:46 +01:00
parent ad357eadc0
commit 407334be6b
4 changed files with 23 additions and 9 deletions

View File

@ -132,7 +132,7 @@ typedef union { // Restricted by MISRA-C Rule 18.4 bu
uint32_t spare28 : 1;
uint32_t spare29 : 1;
uint32_t spare30 : 1;
uint32_t force_sdk_erase : 1; // bit 31 (v8.1.0.9) - SetOption113 - Force erase of SDK wifi calibrate secore on restart
uint32_t spare31 : 1; // bit 31
};
} SysBitfield4;

View File

@ -774,6 +774,10 @@ void SettingsErase(uint8_t type)
_sectorEnd = _sectorStart +1; // SDK end of phy area and Core calibration sector (0xxFCFFF)
}
*/
else {
return;
}
AddLog_P2(LOG_LEVEL_DEBUG, PSTR(D_LOG_APPLICATION D_ERASE " from 0x%08X to 0x%08X"), _sectorStart * SPI_FLASH_SEC_SIZE, (_sectorEnd * SPI_FLASH_SEC_SIZE) -1);
// EspErase(_sectorStart, _sectorEnd); // Arduino core and SDK - erases flash as seen by SDK

View File

@ -21,7 +21,8 @@
* Wifi
\*********************************************************************************************/
// Enable only one of two below debug options
// Enable one of three below options for wifi re-connection debugging
//#define WIFI_FORCE_RF_CAL_ERASE // Erase rf calibration sector on restart only
//#define WIFI_RF_MODE_RF_CAL // Set RF_MODE to RF_CAL for restart and deepsleep during user_rf_pre_init
//#define WIFI_RF_PRE_INIT // Set RF_MODE to RF_CAL for restart, deepsleep and power on during user_rf_pre_init
@ -34,7 +35,7 @@
const uint8_t WIFI_CONFIG_SEC = 180; // seconds before restart
const uint8_t WIFI_CHECK_SEC = 20; // seconds
const uint8_t WIFI_RETRY_OFFSET_SEC = 20; // seconds
const uint8_t WIFI_RETRY_OFFSET_SEC = 12; // seconds
#include <ESP8266WiFi.h> // Wifi, MQTT, Ota, WifiManager
#if LWIP_IPV6
@ -645,7 +646,7 @@ void WifiConnect(void)
WifiSetOutputPower();
WiFi.persistent(false); // Solve possible wifi init errors
Wifi.status = 0;
Wifi.retry_init = WIFI_RETRY_OFFSET_SEC + ((ESP.getChipId() & 0xF) * 2);
Wifi.retry_init = WIFI_RETRY_OFFSET_SEC + (ESP.getChipId() & 0xF); // Add extra delay to stop overrun by simultanous re-connects
Wifi.retry = Wifi.retry_init;
Wifi.counter = 1;
@ -659,29 +660,34 @@ void WifiConnect(void)
void WifiShutdown(bool option = false)
{
// option = false - Legacy disconnect also used by DeepSleep
// option = true - Disconnect with SDK wifi calibrate sector erase
// option = true - Disconnect with SDK wifi calibrate sector erase when WIFI_FORCE_RF_CAL_ERASE enabled
delay(100); // Allow time for message xfer - disabled v6.1.0b
#ifdef USE_EMULATION
UdpDisconnect();
delay(100); // Flush anything in the network buffers.
#endif // USE_EMULATION
if (Settings.flag.mqtt_enabled) { // SetOption3 - Enable MQTT
MqttDisconnect();
delay(100); // Flush anything in the network buffers.
}
if (option && Settings.flag4.force_sdk_erase) { // SetOption113 - Force erase of SDK wifi calibrate sector on restart
#ifdef WIFI_FORCE_RF_CAL_ERASE
if (option) {
WiFi.disconnect(false); // Disconnect wifi
SettingsErase(4); // Delete SDK wifi config and calibrate data
} else {
} else
#endif // WIFI_FORCE_RF_CAL_ERASE
{
// Enable from 6.0.0a until 6.1.0a - disabled due to possible cause of bad wifi connect on core 2.3.0
// Re-enabled from 6.3.0.7 with ESP.restart replaced by ESP.reset
// Courtesy of EspEasy
WiFi.persistent(true); // use SDK storage of SSID/WPA parameters
WiFi.persistent(true); // use SDK storage of SSID/WPA parameters
ETS_UART_INTR_DISABLE();
wifi_station_disconnect(); // this will store empty ssid/wpa into sdk storage
ETS_UART_INTR_ENABLE();
WiFi.persistent(false); // Do not use SDK storage of SSID/WPA parameters
WiFi.persistent(false); // Do not use SDK storage of SSID/WPA parameters
}
delay(100); // Flush anything in the network buffers.
}

View File

@ -1882,6 +1882,7 @@ void HandleOtherConfiguration(void)
}
#ifdef USE_EMULATION
#if defined(USE_EMULATION_WEMO) || defined(USE_EMULATION_HUE)
WSContentSend_P(PSTR("<p></p><fieldset><legend><b>&nbsp;" D_EMULATION "&nbsp;</b></legend><p>")); // Keep close to Friendlynames so do not use <br>
for (uint32_t i = 0; i < EMUL_MAX; i++) {
#ifndef USE_EMULATION_WEMO
@ -1899,6 +1900,7 @@ void HandleOtherConfiguration(void)
}
}
WSContentSend_P(PSTR("</p></fieldset>"));
#endif // USE_EMULATION_WEMO || USE_EMULATION_HUE
#endif // USE_EMULATION
WSContentSend_P(HTTP_FORM_END);
@ -1917,8 +1919,10 @@ void OtherSaveSettings(void)
SettingsUpdateText(SET_WEBPWD, (!strlen(tmp)) ? "" : (strchr(tmp,'*')) ? SettingsText(SET_WEBPWD) : tmp);
Settings.flag.mqtt_enabled = WebServer->hasArg("b1"); // SetOption3 - Enable MQTT
#ifdef USE_EMULATION
#if defined(USE_EMULATION_WEMO) || defined(USE_EMULATION_HUE)
WebGetArg("b2", tmp, sizeof(tmp));
Settings.flag2.emulation = (!strlen(tmp)) ? 0 : atoi(tmp);
#endif // USE_EMULATION_WEMO || USE_EMULATION_HUE
#endif // USE_EMULATION
snprintf_P(message, sizeof(message), PSTR(D_LOG_OTHER D_MQTT_ENABLE " %s, " D_CMND_EMULATION " %d, " D_CMND_FRIENDLYNAME), GetStateText(Settings.flag.mqtt_enabled), Settings.flag2.emulation);