SRJ
April 10, 2024, 7:36am
1
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
J-M-L
April 10, 2024, 7:54am
2
what does preferences.begin()
returns ?
SRJ
April 10, 2024, 7:59am
3
Thanks for your reply.
Serial.println(preferences.begin("System", false));
This returns:
1
J-M-L
April 10, 2024, 8:10am
4
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() {}
SRJ
April 10, 2024, 8:16am
6
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?
J-M-L
April 10, 2024, 8:24am
7
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
SRJ
April 10, 2024, 8:34am
9
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'?
J-M-L
April 10, 2024, 9:09am
10
may be System is a "reserved" word ?
J-M-L
April 10, 2024, 11:36am
11
which Preferences library are you using ?
SRJ
April 12, 2024, 4:58pm
12
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.
J-M-L
April 12, 2024, 5:13pm
13
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 ?
SRJ
April 12, 2024, 5:27pm
14
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:
Hello Experts,
I am working on a project which uses ESP8266WebServer.h on my ESP8266 01 module (tried the same code with ESP8266 Nodemcu)
The issue I am facing with both the boards is when I load the page by visiting its IP address then complete HTML code is not loaded.
Understandably, there are mistakes in the code if the same part of the code is missing every time, But in my case, different parts are missed every time. Like sometimes the last lines are missing. Sometimes half of tag is mis…
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
J-M-L
April 12, 2024, 5:41pm
16
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
system
Closed
October 9, 2024, 5:42pm
17
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.