Issue in Preferences getString

Hello Experts,

I am working on a project with ESP8266 01 module with ESP8266WebServer.h & Preferences.h header files. In this the user sends some data using Webserver & the received data is then stored using Preferences.

This issue it that the data is not getting stored or unable to read it.
I tried to check if the key existaor not but it is showing that the key does not exist (Output is 0).

Below is the relevant code & the output of the code:

Code:

void AutoManTogHandler() {
  preferences.begin("System", false);
  String AutoManTogState = webServer.arg("switchStateRelay");
  Serial.println("Html Auto_Man Btn State: " + AutoManTogState);
  preferences.putString("AutoManS", AutoManTogState);
  Serial.println("Actual: " + preferences.getString("AutoManS", "0"));
  int freeEntries = preferences.freeEntries();
  Serial.print("Free preference slots: ");
  Serial.println(freeEntries);
  Serial.println(preferences.isKey("AutoManS"));
  preferences.end();
  webServer.send(200);
}

void ManOnfTogHandler() {
  preferences.begin("System", false);
  String ManOnfTogState = webServer.arg("switchStateRelay");
  Serial.println("Html Relat Btn State State: " + ManOnfTogState);
  preferences.putString("RelayS", ManOnfTogState);
  Serial.println("Actual: " + preferences.getString("RelayS", "0"));
  int freeEntries = preferences.freeEntries();
  Serial.print("Free preference slots: ");
  Serial.println(freeEntries);
  Serial.println(preferences.isKey("RelayS"));
  preferences.end();
  webServer.send(200);
}

Output:

18:32:56.768 -> Html Auto_Man Btn State: 0
18:32:57.088 -> Actual: 0
18:32:57.088 -> Free preference slots: 1000
18:32:57.088 -> 0
18:32:58.018 -> Html Auto_Man Btn State: 1
18:32:58.339 -> Actual: 0
18:32:58.339 -> Free preference slots: 1000
18:32:58.339 -> 0
18:33:02.321 -> Html Relat Btn State State: 1
18:33:02.641 -> Actual: 0
18:33:02.641 -> Free preference slots: 1000
18:33:02.641 -> 0
18:33:04.023 -> Html Relat Btn State State: 0
18:33:04.344 -> Actual: 0
18:33:04.344 -> Free preference slots: 1000
18:33:04.344 -> 0

According to the output it looks like the values sent by webserver are received but isn't stored. I also tried checking the preferences slots are available or not & it shown 1000. Why the isKey shown as 0?

Someone kindly help me to point out what I am mistaken in the code.

Thanks & Regards!!

--SRJ

what does preferences.begin() returns ?

Thanks for your reply.

Serial.println(preferences.begin("System", false));

This returns:

1

can you try a small code without the web server testing this out?

#include <Preferences.h>
Preferences preferences;

void setup() {
  Serial.begin(115200);
  bool success = preferences.begin("System", false);
  if (success) {
    String AutoManTogState = "hello world";
    Serial.print("Writing: ");
    Serial.println(AutoManTogState);

    preferences.putString("AutoManS", AutoManTogState);

    Serial.print("Reading back: ");
    Serial.println(preferences.getString("AutoManS", "default value error"));

    Serial.print("AutoManS isKey: ");
    Serial.println(preferences.isKey("AutoManS") ? "YES" : "NO");
    preferences.end();
  } else Serial.println("Could not open 'System' Preferences in R/W mode");

  Serial.println("Trying to read in a new session now");

  success = preferences.begin("System", true); // true for read only
  if (success) {
    Serial.print("Reading again: ");
    Serial.println(preferences.getString("AutoManS", "default value error"));

    Serial.print("AutoManS isKey: ");
    Serial.println(preferences.isKey("AutoManS") ? "YES" : "NO");
    preferences.end();
  } else Serial.println("Could not open 'System' Preferences in read only mode");
}

void loop() {}

Tried your provided code & the output is:

19:14:04.415 -> �Writing: hello world
19:14:04.736 -> Reading back: default value error
19:14:04.736 -> AutoManS isKey: NO
19:14:04.736 -> Trying to read in a new session now
19:14:04.736 -> Reading again: default value error
19:14:04.736 -> AutoManS isKey: NO

What does it mean?

It seems it means that Preferences don't work on your ESP-01 (I never tried on that platform, only with ESP32) as the spec states

The Preferences library is unique to arduino-esp32. It should be considered as the replacement for the Arduino EEPROM library.

It uses a portion of the on-board non-volatile memory (NVS) of the ESP32 to store data. This data is retained across restarts and loss of power events to the system.

Preferences works best for storing many small values, rather than a few large values. If large amounts of data are to be stored, consider using a file system library such as LitteFS.

The Preferences library is usable by all ESP32 variants.

So it seems that's proving the documentation is correct.

You should find an alternate way for storing your preferences, may be

which is using LittleFS

In the same code I have

preferences.begin("Events", false);

But it is working. I am able to write & read data from the Events. Just facing error in System.

So I just tried replacing system to Events & It started working...

19:31:36.089 -> Html Auto_Man Btn State: 0
19:31:36.506 -> Actual: 0
19:31:36.506 -> Free preference slots: 1000
19:31:36.538 -> 1
19:31:38.946 -> Html Auto_Man Btn State: 1
19:31:39.365 -> Actual: 1
19:31:39.365 -> Free preference slots: 1000
19:31:39.397 -> 1
19:31:40.811 -> Html Auto_Man Btn State: 0
19:31:41.646 -> Actual: 0
19:31:41.646 -> Free preference slots: 1000
19:31:41.679 -> 1
19:31:43.316 -> Html Auto_Man Btn State: 1
19:31:43.605 -> Actual: 1
19:31:43.605 -> Free preference slots: 1000
19:31:43.637 -> 1

Let me know was I doing wrong by using two preferences.begin() with different names?

Or any problem with the name 'System'?

may be System is a "reserved" word ?

which Preferences library are you using ?

Hey, Sorry for late reply.

I am using preference library by vshymanskyy.
Github: GitHub - vshymanskyy/Preferences: Preferences library for Arduino, ESP8266, RP2040 and Particle. ESP32-compatible API

I downloaded it from Arduino Library Manager.

OK - so that's why you have something working on the ESP-01

there might be limitations, for example I see that they say

  • getType() and freeEntries() methods are not supported (returning dummy values)

so that's why you see 1000 when you call freeEntries()

I don't know why the "System" name would not work when "Events" work. Are you using two named preferences inside the same app? may be it supports only one ?

Oh, I didn't see this part thanks for highlighting the point.

Yes, I am using two named preferences in a single app.

I would like to know does ESP32 supports 2 named preferences in a single app?

Thanks for your valuable time & advice in this matter.

Please have a look at my new topic/ issue:

Thanks.

--SRJ

Yes. An esp32 using the standard Preferences.h will support multiple named preferences.

I though you were having issues with an esp8266 and a special library for that device.

1 Like

Yes on ESP32 you can have multiple "domain names"

this should work

#include <Preferences.h>
Preferences preferences;

void setup() {
  Serial.begin(115200);

  Serial.println("Writing to DomainName1: ");
  bool success = preferences.begin("DomainName1", false);
  if (success) {
    String AutoManTogState = "hello world DomainName1";
    Serial.print("Writing: ");  Serial.println(AutoManTogState);
    preferences.putString("AutoManS", AutoManTogState);
    preferences.end();
  } else Serial.println("Could not open 'DomainName1' Preferences in R/W mode");

  Serial.println("Writing to DomainName2: ");
  success = preferences.begin("DomainName2", false);
  if (success) {
    String AutoManTogState = "hello world DomainName2";
    Serial.print("Writing: "); Serial.println(AutoManTogState);
    preferences.putString("AutoManS", AutoManTogState);
    preferences.end();
  } else Serial.println("Could not open 'DomainName2' Preferences in R/W mode");


  Serial.println("Reading back from DomainName1: ");
  success = preferences.begin("DomainName1", false);
  if (success) {
    Serial.println(preferences.getString("AutoManS", "default value error"));
    Serial.print("AutoManS isKey: ");
    Serial.println(preferences.isKey("AutoManS") ? "YES" : "NO");
    preferences.end();
  } else Serial.println("Could not open 'DomainName1' Preferences in R/W mode");


  Serial.println("Reading back from DomainName2 ");
  success = preferences.begin("DomainName2", false);
  if (success) {
    Serial.println(preferences.getString("AutoManS", "default value error"));
    Serial.print("AutoManS isKey: ");
    Serial.println(preferences.isKey("AutoManS") ? "YES" : "NO");
    preferences.end();
  } else Serial.println("Could not open 'DomainName2' Preferences in R/W mode");


}

void loop() {}
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.