• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Preferences not saving values

Q&A Forum › Category: ESP32 › Preferences not saving values
0 Vote Up Vote Down
Dean Cotton asked 3 years ago

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?

9 Answers
1 Vote Up Vote Down
Best Answer
Sara Santos Staff answered 3 years ago

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

0 Vote Up Vote Down
Dean Cotton answered 3 years ago

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();
}
0 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

Try adding 

  preferences.end();

before restarting the board.
How are you reading the values?

0 Vote Up Vote Down
Dean Cotton answered 3 years ago

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();

0 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

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?

0 Vote Up Vote Down
Dean Cotton answered 3 years ago

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);
0 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

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

0 Vote Up Vote Down
Dean Cotton answered 3 years ago

Hi.
Thank you, it needed the .end() and the smaller namespace name, it all works perfectly now.

0 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

Great!
I’m glad the issue is solved.
I’ll mark this issue as resolved. If you need further help, you just need to open a new question in our forum.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.