How to use the Preferences Library?

Rrobinet
Posts: 29
Joined: Wed Aug 16, 2017 7:05 am

How to use the Preferences Library?

Postby Rrobinet » Thu Aug 17, 2017 7:27 am

Hi,

I do not actually understand the usage of the Preference library.
I would like to reproduce the usage of the PROGMEM storage as follow:

1. Store in PROGMEM a string such as:

Code: Select all

const char control_1 [] PROGMEM = "Coordinator Model: ";
#define MODEL “COOR”
2. in the loop function print something like:

Code: Select all

Serial.print (getString(control_1)), Serial.println (MODEL);
This was perviously working with WEMOS D1 Mini.
I think I understand that PROGMEM is depreciated and replaced by the ESP32NVS (Non Volatile Storage), however I couldn’t find a decent way to replace the previous configuration.

I have tried the following without success:

Code: Select all

#include <Preferences.h>
Preferences preferences;
void setup ()
{
  Serial.begin(115200);
  preferences.begin("my-app",false);
  preferences.putString("mess_1", "Hello");
  preferences.end();
}
void loop ()
{
  Serial.println (preferences.getString("mess_1"));
  delay (1000);
}
Can somebody help me to have a better understanding of the usage of Preferences.

Thanks Robert

ESP_igrr
Posts: 2067
Joined: Tue Dec 01, 2015 8:37 am

Re: How to use the Preferences Library?

Postby ESP_igrr » Thu Aug 17, 2017 9:02 am

The purpose of Preferences is entirely different from Progmem.

Progmem provides a way to store immutable data in program memory (which is usually Flash), instead of data memory (which is usually DRAM). On the ESP32, there is no need for Progmem because constant data is automatically stored into Flash, and can be accessed from Flash without copying into RAM first. So on ESP32, Progmem is defined for compatibility with other platforms, but it is a no-op.

Preferences provides persistent (across resets) but mutable storage of various types of variables. It is similar to EEPROM library in Arduino, except that EEPROM provides a single contiguous block of storage which the sketch needs to partition between variables, while Preferences performs the partitioning itself.

The first piece of code you listed should work on ESP32. If it does not, please open an issue on Github.

The second piece of code should also work, if you initialize Preferences before your loop. Currently you call preferences.end before the loop, which may be the reason why subsequent reads do not work.

Rrobinet
Posts: 29
Joined: Wed Aug 16, 2017 7:05 am

Re: How to use the Preferences Library?

Postby Rrobinet » Fri Aug 18, 2017 7:22 am

Hi,

Thanks for your feedback, it is now much clear for me how to use it (similar to the EEPROM library).
I have modify my sketch as follow:

Code: Select all

#include <Preferences.h>
Preferences preferences;
void setup ()
{
  Serial.begin(115200);
  preferences.begin("my-app",false);
  preferences.putString("mess_1", "Hello");
  preferences.end();
}
void loop ()
{
  preferences.begin("my-app",false);
  Serial.println (preferences.getString("mess_1"));
  preferences.end();  
    delay (1000);
}
Now regarding the PROGMEM configuration it still be compatible with the Arduino ... WEMOS D1 syntax however the getString() function is causing problem:

Code: Select all

Serial.print (getString(control_1)), Serial.println (MODEL);
This one is generating a compilation error in most of the cases but not all the time, with the result is that the WEMOS LOLIN is crashing when executing this function.
Should I raise an issue for that?
Robert

SombraRPK
Posts: 18
Joined: Wed Jul 04, 2018 2:37 am

Re: How to use the Preferences Library?

Postby SombraRPK » Wed Oct 24, 2018 3:14 pm

Could you solve the issue? Rrobinet? I'm just having some troubles with the putString and getString instructions. I'm not sure if it is storing my value with putString and less if it is reading it ok the getString func. I'm trying this:

Code: Select all

#include <Preferences.h>

Preferences preferences;

void setup() {
  Serial.begin(115200);
  delay (5000);
  Serial.println();
  preferences.begin("Pref", false);
  char *buff = "nada Por Aquí";
  String entrada1 = "2410180841:26.5";
  String entrada2 = "2410180845:65";

  // Get the counter value, if the key does not exist, return a default value of 0
  // Note: Key name is limited to 15 chars.
  unsigned int SN = preferences.getUInt("SN", 0);
  preferences.putString("T1", entrada1);
  preferences.putString("T2", entrada2);
  //preferences.putString("3", "2410180846:1780.5");

  if (SN == 0){
     SN = 1;
     Serial.printf("\n\nDispositivo renovado\n\n");
     Serial.printf("Asignando número: %u\n", SN);
     preferences.putUInt("SN", SN);
  }
  
  else {
     Serial.printf("Numero: %u\n", SN);
     Serial.printf("\nColección 1: %s", preferences.getString("T1"));
     Serial.printf("\nColección 2: %s", preferences.getString("T2"));
  }
  
  // Close the Preferences
  preferences.end();

  // Wait 10 seconds
  Serial.println("Restarting in 10 seconds...");
  delay(5000);

  // Restart ESP
  ESP.restart();
}

void loop() {}
I just want to know how to get my stored String. I know that both key and value strings must be 15 characters length as maximum, but when I try to get the value always apears " ⸮R⸮? ".

Thanks in advance for your help, guys! :)!

Who is online

Users browsing this forum: gfvalvo and 59 guests