From d8c298f6fe8ec08039d96a29dcbe5d20a1b5fb69 Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Sun, 17 Sep 2023 09:27:08 +0200 Subject: [PATCH] network init --- lib/framework/NetworkSettingsService.cpp | 51 ++++++++++++++---------- platformio.ini | 4 +- src/version.h | 2 +- 3 files changed, 33 insertions(+), 24 deletions(-) diff --git a/lib/framework/NetworkSettingsService.cpp b/lib/framework/NetworkSettingsService.cpp index 8dcee3bd8..a9c831686 100644 --- a/lib/framework/NetworkSettingsService.cpp +++ b/lib/framework/NetworkSettingsService.cpp @@ -54,31 +54,36 @@ void NetworkSettingsService::manageSTA() { } // Connect or reconnect as required - if ((WiFi.getMode() & WIFI_STA) == 0) { - if (_state.staticIPConfig) { - WiFi.config(_state.localIP, _state.gatewayIP, _state.subnetMask, _state.dnsIP1, _state.dnsIP2); // configure for static IP - } + WiFi.disconnect(true); // turn radio off + WiFiMode_t currentWiFiMode = WiFi.getMode(); + if (currentWiFiMode == WIFI_MODE_APSTA || currentWiFiMode == WIFI_MODE_AP) { + WiFi.mode(WIFI_MODE_AP); + } else { + WiFi.mode(WIFI_MODE_NULL); + } + if (_state.staticIPConfig) { + WiFi.config(_state.localIP, _state.gatewayIP, _state.subnetMask, _state.dnsIP1, _state.dnsIP2); // configure for static IP + } + WiFi.setHostname(_state.hostname.c_str()); // set hostname - WiFi.setHostname(_state.hostname.c_str()); // set hostname + // www.esp32.com/viewtopic.php?t=12055 + if (_state.bandwidth20) { + esp_wifi_set_bandwidth((wifi_interface_t)ESP_IF_WIFI_STA, WIFI_BW_HT20); + } else { + esp_wifi_set_bandwidth((wifi_interface_t)ESP_IF_WIFI_STA, WIFI_BW_HT40); + } + if (_state.nosleep) { + WiFi.setSleep(false); // turn off sleep - WIFI_PS_NONE + } + WiFi.begin(_state.ssid.c_str(), _state.password.c_str()); // attempt to connect to the network - // www.esp32.com/viewtopic.php?t=12055 - read([&](NetworkSettings & networkSettings) { - if (networkSettings.bandwidth20) { - esp_wifi_set_bandwidth((wifi_interface_t)ESP_IF_WIFI_STA, WIFI_BW_HT20); - } else { - esp_wifi_set_bandwidth((wifi_interface_t)ESP_IF_WIFI_STA, WIFI_BW_HT40); - } - if (networkSettings.nosleep) { - WiFi.setSleep(false); // turn off sleep - WIFI_PS_NONE - } - WiFi.begin(_state.ssid.c_str(), _state.password.c_str()); // attempt to connect to the network - esp_wifi_set_max_tx_power(networkSettings.tx_power * 4); // set power after wifi is startet for C3 - }); + // set power after wifi is startet, fixed value for C3_V1 #ifdef BOARD_C3_MINI_V1 - // v1 needs this value, see https://github.com/emsesp/EMS-ESP32/pull/620#discussion_r993173979 - WiFi.setTxPower(WIFI_POWER_8_5dBm); // https://www.wemos.cc/en/latest/c3/c3_mini_1_0_0.html#about-wifi + // v1 needs this value, see https://github.com/emsesp/EMS-ESP32/pull/620#discussion_r993173979 + WiFi.setTxPower(WIFI_POWER_8_5dBm); // https://www.wemos.cc/en/latest/c3/c3_mini_1_0_0.html#about-wifi +#else + esp_wifi_set_max_tx_power(_state.tx_power * 4); #endif - } } // handles if wifi stopped @@ -89,4 +94,8 @@ void NetworkSettingsService::WiFiEvent(WiFiEvent_t event) { _stopping = false; } } + // wait 3 seconds before reconnecting + if (event == ARDUINO_EVENT_WIFI_STA_DISCONNECTED) { + _lastConnectionAttempt = millis(); + } } diff --git a/platformio.ini b/platformio.ini index e254cc9e3..0a4692f41 100644 --- a/platformio.ini +++ b/platformio.ini @@ -48,8 +48,8 @@ extra_scripts = scripts/rename_fw.py [espressi32_base_tasmota] - ; use Tasmota's 2.0.11 core based libary from https://github.com/tasmota/platform-espressif32 which removes some libs (like mbedtsl) and increases available heap -platform = https://github.com/tasmota/platform-espressif32/releases/download/2023.08.01/platform-espressif32.zip + ; use Tasmota's 2.0.13 core based libary from https://github.com/tasmota/platform-espressif32 which removes some libs (like mbedtsl) and increases available heap +platform = https://github.com/tasmota/platform-espressif32/releases/download/2023.09.01/platform-espressif32.zip framework = arduino build_flags = ${common.build_flags} build_unflags = ${common.unbuild_flags} diff --git a/src/version.h b/src/version.h index 915467bd0..3f20b9401 100644 --- a/src/version.h +++ b/src/version.h @@ -1 +1 @@ -#define EMSESP_APP_VERSION "3.7.0-dev.0" +#define EMSESP_APP_VERSION "3.6.2-dev.0a"