Skip to content

Commit 1f102ca

Browse files
authored
Fix LED-animations briefly pausing at bootup (ESP32 only) (#5337)
* fix bootup output glitch by using NVM stored credentials * add ifdef guards for ESP8266
1 parent 43e86d0 commit 1f102ca

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

wled00/wled.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ void WLED::setup()
507507
#ifndef ESP8266
508508
WiFi.setScanMethod(WIFI_ALL_CHANNEL_SCAN);
509509
#endif
510-
WiFi.persistent(false);
510+
WiFi.persistent(false); // note: on ESP32 this is only applied if WiFi.mode() is called and only if mode changes, use low level esp_wifi_set_storage() to change storage method immediately
511511
WiFi.onEvent(WiFiEvent);
512512
WiFi.mode(WIFI_STA); // enable scanning
513513
findWiFi(true); // start scanning for available WiFi-s
@@ -670,6 +670,27 @@ void WLED::initAP(bool resetAP)
670670

671671
void WLED::initConnection()
672672
{
673+
#ifdef ARDUINO_ARCH_ESP32
674+
static bool firstCall = true;
675+
bool updateWiFiNVM = false;
676+
if (firstCall) {
677+
// check cached WiFi config in flash, esp_wifi_get_config still contains NVM values at this point
678+
// note: connecting to the NVM stored wifi is much faster and prevents boot-up glitches on LEDs (there is no benefit on ESP8266)
679+
wifi_config_t cachedConfig;
680+
esp_err_t configResult = esp_wifi_get_config( (wifi_interface_t)ESP_IF_WIFI_STA, &cachedConfig );
681+
if( configResult == ESP_OK ) {
682+
if (strncmp((const char*)cachedConfig.sta.ssid, multiWiFi[0].clientSSID, 32) != 0 ||
683+
strncmp((const char*)cachedConfig.sta.password, multiWiFi[0].clientPass, 64) != 0) {
684+
updateWiFiNVM = true; // SSID or pass changed, update NVM at next WiFi.begin() call
685+
DEBUG_PRINTLN(F("WiFi config NVM update triggered"));
686+
}
687+
}
688+
firstCall = false;
689+
}
690+
else
691+
esp_wifi_set_storage(WIFI_STORAGE_RAM); // do not update NVM credentials while running to prevent wear on flash
692+
#endif
693+
673694
DEBUG_PRINTF_P(PSTR("initConnection() called @ %lus.\n"), millis()/1000);
674695
#ifdef WLED_ENABLE_WEBSOCKETS
675696
ws.onEvent(wsEvent);
@@ -720,6 +741,11 @@ void WLED::initConnection()
720741
char hostname[25];
721742
prepareHostname(hostname);
722743

744+
#ifdef ARDUINO_ARCH_ESP32
745+
if(updateWiFiNVM && selectedWiFi == 0) // NVM only can store one wifi: store credentials of first WiFi even if multiple are configured
746+
esp_wifi_set_storage(WIFI_STORAGE_FLASH); // temporary override WiFi.persistent(false) to store credentials in flash (is reset to RAM on next initConnection() call)
747+
#endif
748+
723749
#ifdef WLED_ENABLE_WPA_ENTERPRISE
724750
if (multiWiFi[selectedWiFi].encryptionType == WIFI_ENCRYPTION_TYPE_PSK) {
725751
DEBUG_PRINTLN(F("Using PSK"));

0 commit comments

Comments
 (0)