I have been having issues with the preferences library not saving values or at least they aren’t remembered after a power cycle (I believe I tracked the issue down to the put command/statement).
I encounter this problem randomly as the saving works until I add another variable to save or change the key value for an existing variable. could the issue occur if you don’t add the .exit()
at the end of the preferences read/write cycle?
Hi.
Can you show me the example you’re running that is not working?
You need to call .end() at the end of the preferences read/write cycle.
Regards,
Sarea
Here is a snippet, I have been using the library with out an issue until the non-saving started when I changed the Wifi[0]’s key to “WiFi SSID” to allow for more legibility.
I have the same format for other variables which work fine, until do a similar edit
Start your code here void WiFiValueSaver() { #pragma region Saving prefs.putString("WiFi SSID", Wifi[0]); prefs.putString("WiFI Password", Wifi[1]); prefs.putString("Mode", Wifi[2]); prefs.putString("AP SSID", Wifi[3]); prefs.putString("AP Password", Wifi[4]); #pragma endregion Serial.println("\nWifi Credential\n"); for (int i = 0; i < 5; i++) { Serial.print(i); Serial.print(":"); Serial.println(Wifi[i]); } Serial.println("ESP RESTARTING"); ESP.restart(); }
Try adding
preferences.end();
before restarting the board.
How are you reading the values?
I use the .get()
command to retrieve the values int he setup method of the program and store the data/values in the loop through out the program.
preferences.getString();
preferences.getUInt();
preferences.getBool();
I’m sorry, but I don’t understand…
Can you provide more details?
What happens exactly when you say it doesn’t save the values?
I have rewritten the saving and loading from preferences to include the .end()
statement, however it still doesn’t remember the variables after a power cycle.
I have included the new code below:
void PrefWifiCred(int saveVal) { prefs.begin("WifiCredentials", false); switch (saveVal) { case LOAD: Wifi[0] = prefs.getString("WiFi SSID", "MadmanGuest"); Wifi[1] = prefs.getString("WiFi Password", "DeanRudi20"); Wifi[2] = prefs.getString("Mode", "1"); Wifi[3] = prefs.getString("AP SSID", "Madman Bravo"); Wifi[4] = prefs.getString("AP Password", ""); break; case SAVE: prefs.putString("WiFi SSID", Wifi[0]); prefs.putString("WiFi Password", Wifi[1]); prefs.putString("Mode", Wifi[2]); prefs.putString("AP SSID", Wifi[3]); prefs.putString("AP Password", Wifi[4]); break; } prefs.end(); }
I call the method with defined tags:
PrefWifiCred(SAVE);
or
PrefWifiCred(LOAD);
Hi.
Taking a look at that snippet, everything seems fine.
The Namespace name is limited to 15 characters. Yours have exactly 15 characters. Try to give a different name to the namespace and see if it changes something.
prefs.begin("WifiCredentials", false);
Regards,
Sara
Hi.
Thank you, it needed the .end()
and the smaller namespace name, it all works perfectly now.