mirror of
https://github.com/esphome/esphome.git
synced 2025-09-12 00:02:21 +01:00
WIP: ESP8266 work on connection issues (#648)
* Erase all flash for USB uploads on ESP8266s Previously, only erased "write regions". Downside: Config for other FWs like tasmota could be affected Upside: Potentially fixes some ESP8266 connection issues Related: https://github.com/esphome/issues/issues/455#issuecomment-503524479 * Clear WiFi settings for ESP8266 Clears wifi settings from retained storage on ESP8266 (if set). Unsure if this is the actual issue, but it won't cause problems either. * Update wifi_component_esp8266.cpp * Revert erase chip for testing * Improve wait_time calculation
This commit is contained in:
@@ -58,19 +58,6 @@ bool WiFiComponent::wifi_mode_(optional<bool> sta, optional<bool> ap) {
|
||||
|
||||
return ret;
|
||||
}
|
||||
bool WiFiComponent::wifi_disable_auto_connect_() {
|
||||
bool ret1, ret2;
|
||||
ETS_UART_INTR_DISABLE();
|
||||
ret1 = wifi_station_set_auto_connect(0);
|
||||
ret2 = wifi_station_set_reconnect_policy(false);
|
||||
ETS_UART_INTR_ENABLE();
|
||||
|
||||
if (!ret1 || !ret2) {
|
||||
ESP_LOGV(TAG, "Disabling Auto-Connect failed!");
|
||||
}
|
||||
|
||||
return ret1 && ret2;
|
||||
}
|
||||
bool WiFiComponent::wifi_apply_power_save_() {
|
||||
sleep_type_t power_save;
|
||||
switch (this->power_save_) {
|
||||
@@ -158,9 +145,7 @@ bool WiFiComponent::wifi_sta_connect_(WiFiAP ap) {
|
||||
if (!this->wifi_mode_(true, {}))
|
||||
return false;
|
||||
|
||||
ETS_UART_INTR_DISABLE();
|
||||
wifi_station_disconnect();
|
||||
ETS_UART_INTR_ENABLE();
|
||||
this->wifi_disconnect_();
|
||||
|
||||
struct station_config conf {};
|
||||
memset(&conf, 0, sizeof(conf));
|
||||
@@ -330,11 +315,6 @@ const char *get_disconnect_reason_str(uint8_t reason) {
|
||||
}
|
||||
|
||||
void WiFiComponent::wifi_event_callback(System_Event_t *event) {
|
||||
// TODO: this callback is called while in cont context, so delay will fail
|
||||
// We need to defer the log messages until we're out of this context
|
||||
// only affects verbose log level
|
||||
// reproducible by enabling verbose log level and letting the ESP disconnect and
|
||||
// then reconnect to WiFi.
|
||||
switch (event->event) {
|
||||
case EVENT_STAMODE_CONNECTED: {
|
||||
auto it = event->event_info.connected;
|
||||
@@ -410,7 +390,62 @@ void WiFiComponent::wifi_event_callback(System_Event_t *event) {
|
||||
WiFiMockClass::_event_callback(event);
|
||||
}
|
||||
|
||||
void WiFiComponent::wifi_register_callbacks_() { wifi_set_event_handler_cb(&WiFiComponent::wifi_event_callback); }
|
||||
bool WiFiComponent::wifi_sta_pre_setup_() {
|
||||
if (!this->wifi_mode_(true, {}))
|
||||
return false;
|
||||
|
||||
// Clear saved STA config
|
||||
station_config default_config{};
|
||||
wifi_station_get_config_default(&default_config);
|
||||
bool is_zero = default_config.ssid[0] == '\0' && default_config.password[0] == '\0' && default_config.bssid[0] == 0 &&
|
||||
default_config.bssid_set == 0;
|
||||
if (!is_zero) {
|
||||
ESP_LOGV(TAG, "Clearing default wifi STA config");
|
||||
|
||||
memset(&default_config, 0, sizeof(default_config));
|
||||
ETS_UART_INTR_DISABLE();
|
||||
bool ret = wifi_station_set_config(&default_config);
|
||||
ETS_UART_INTR_ENABLE();
|
||||
|
||||
if (!ret) {
|
||||
ESP_LOGW(TAG, "Clearing default wif STA config failed!");
|
||||
}
|
||||
}
|
||||
|
||||
bool ret1, ret2;
|
||||
ETS_UART_INTR_DISABLE();
|
||||
ret1 = wifi_station_set_auto_connect(0);
|
||||
ret2 = wifi_station_set_reconnect_policy(false);
|
||||
ETS_UART_INTR_ENABLE();
|
||||
|
||||
if (!ret1 || !ret2) {
|
||||
ESP_LOGV(TAG, "Disabling Auto-Connect failed!");
|
||||
}
|
||||
|
||||
delay(10);
|
||||
return true;
|
||||
}
|
||||
|
||||
void WiFiComponent::wifi_pre_setup_() {
|
||||
wifi_set_event_handler_cb(&WiFiComponent::wifi_event_callback);
|
||||
// Make sure the default opmode is OFF
|
||||
uint8_t default_opmode = wifi_get_opmode_default();
|
||||
if (default_opmode != 0) {
|
||||
ESP_LOGV(TAG, "Setting default WiFi Mode to 0 (was %u)", default_opmode);
|
||||
|
||||
ETS_UART_INTR_DISABLE();
|
||||
bool ret = wifi_set_opmode(0);
|
||||
ETS_UART_INTR_ENABLE();
|
||||
|
||||
if (!ret) {
|
||||
ESP_LOGW(TAG, "Setting default WiFi mode failed!");
|
||||
}
|
||||
}
|
||||
|
||||
// Make sure WiFi is in clean state before anything starts
|
||||
this->wifi_mode_(false, false);
|
||||
}
|
||||
|
||||
wl_status_t WiFiComponent::wifi_sta_status_() {
|
||||
station_status_t status = wifi_station_get_connect_status();
|
||||
switch (status) {
|
||||
@@ -435,11 +470,6 @@ bool WiFiComponent::wifi_scan_start_() {
|
||||
if (!this->wifi_mode_(true, {}))
|
||||
return false;
|
||||
|
||||
station_status_t sta_status = wifi_station_get_connect_status();
|
||||
if (sta_status != STATION_GOT_IP && sta_status != STATION_IDLE) {
|
||||
wifi_station_disconnect();
|
||||
}
|
||||
|
||||
struct scan_config config {};
|
||||
memset(&config, 0, sizeof(config));
|
||||
config.ssid = nullptr;
|
||||
@@ -465,6 +495,15 @@ bool WiFiComponent::wifi_scan_start_() {
|
||||
|
||||
return ret;
|
||||
}
|
||||
bool WiFiComponent::wifi_disconnect_() {
|
||||
station_config conf{};
|
||||
memset(&conf, 0, sizeof(conf));
|
||||
ETS_UART_INTR_DISABLE();
|
||||
wifi_station_set_config(&conf);
|
||||
bool ret = wifi_station_disconnect();
|
||||
ETS_UART_INTR_ENABLE();
|
||||
return ret;
|
||||
}
|
||||
void WiFiComponent::s_wifi_scan_done_callback(void *arg, STATUS status) {
|
||||
global_wifi_component->wifi_scan_done_callback_(arg, status);
|
||||
}
|
||||
|
Reference in New Issue
Block a user