pcace
November 1, 2022, 9:12pm
1
Hi there, i am trying to read / write float values to and from EEPROM on a esp32, but it just reads 0. i am trying to do it by doing this:
for read:
float distA = EEPROM.get(sizeof(float) * 0, distA) > 0 ? EEPROM.get(sizeof(float) * 0, distA) : 0;
float distB = EEPROM.get(sizeof(float) * 1, distB) > 0 ? EEPROM.get(sizeof(float) * 1, distB) : 0;
float totalDist = EEPROM.get(sizeof(float) * 2, totalDist) > 0 ? EEPROM.get(sizeof(float) * 2, totalDist) : 0;
float totalTime = EEPROM.get(sizeof(float) * 3, totalTime) > 0 ? EEPROM.get(sizeof(float) * 3, totalTime) : 0;
float dailyTime = EEPROM.get(sizeof(float) * 4, dailyTime) > 0 ? EEPROM.get(sizeof(float) * 4, dailyTime) : 0;
float dailyDist = EEPROM.get(sizeof(float) * 5, dailyDist) > 0 ? EEPROM.get(sizeof(float) * 5, dailyDist) : 0;
and for write:
EEPROM.put(sizeof(float) * 0, distA);
EEPROM.put(sizeof(float) * 1, distB);
EEPROM.put(sizeof(float) * 2, totalDist);
EEPROM.put(sizeof(float) * 3, totalTime);
i also have
EEPROM.begin(sizeof(float)*5); in the setup().
since it does not load any value from the EEPROM i guess i have a wrong understanding...
how can point me in the right direction?
Thanks a lot!!
PS.: is it necessary to do this:
float distA = EEPROM.get(sizeof(float) * 0, distA) > 0 ? EEPROM.get(sizeof(float) * 0, distA) : 0;
or would it be just fine to do this:
float distA = EEPROM.get(sizeof(float) * 0, distA)
?
EEPROM.get(sizeof(float) * 0
Multiplying by zero is rarely a good idea
Please post a complete sketch that can be copied and compiled
pcace:
float distA =…
I’ll bet all these are declared within a function().
— SCOPE—
Those variables are not available outside that function()
EEPROM, as a note, on the ESP32 has been depreciated.
The Arduino programming language Reference, organized into Functions, Variable and Constant, and Structure keywords.
Here's an example of a float array with Preferences.h.
#include <Preferences.h>
Preferences prefs;
float floatStore[3] = {123.45, 456.89, 789.01};
float floatRetrieve[3];
void setup() {
Serial.begin(115200);
prefs.begin("FloatArray"); //namespace
//bytes can be put/get in namespace directly see prefs2struct example
prefs.putBytes("FloatArray", (byte*)(&floatStore), sizeof(floatStore));
prefs.getBytes("FloatArray", &floatRetrieve, sizeof(floatRetrieve));
Serial.println(floatRetrieve[0]);
Serial.println(floatRetrieve[1]);
Serial.println(floatRetrieve[2]);
}
void loop() {}
3 Likes
pcace
November 2, 2022, 5:45pm
6
Hi, i have found the problem... i was redefining the values in setup(). Stupid.. sorry for the hussle!
here is what i have changed:
committed 06:40PM - 02 Nov 22 UTC
Cheers and thanks for the help!!
What's the error or issue?
pcace
November 2, 2022, 6:16pm
8
oh after reboot the values are all 0.
after reboot the values are all 0.
Can you provide a complete, minimal example code which compiles, runs and demonstrates the issue.
Working with complete code posted on github is not practicable.
pcace
November 2, 2022, 6:44pm
10
hi, see post 6.. fixed it.
Thanks a lot!!
htekin
November 2, 2022, 6:45pm
11
@pcace , I suggest you to use preferences library, we use preferences library on our works for saving data on power down. but be careful about names of variables. if you give long names, it doesn't work. ( it took lots of time for me to figure out issue once a time).
Preferences.h uses the underlying non volatile storage library of the ESP32
nvs.h
https://docs.espressif.com/projects/esp-idf/en/v4.2.2/esp32/api-reference/storage/nvs_flash.html
That library has a limit of 15 characters for the key name.
Key
Zero-terminated ASCII string containing a key name. Maximum string length is 15 bytes, excluding a zero terminator.
1 Like
system
Closed
May 1, 2023, 7:31pm
14
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.