I have tried and tried to find a way to store a float to eeprom on my ESP32.
Have tried to store float, and int but I really dont get it.
And google for 2h + openAI...
Appreciate any ideas
This is a try to multiply by 1000, but get noting good out:
18:35:40.437 -> J⸮k⸮HnJJ⸮calToStore: 12345
18:35:40.590 -> .
18:35:40.590 -> 0
18:35:40.590 -> EEPROM read Stored calibration_factor: memPlace: 0
18:35:40.590 -> Verdi 1: 0.0000
18:35:40.590 -> Verdi 2: 0
18:35:40.590 -> Verdi 3: 1.6972
18:35:45.543 -> EEPROM read Stored calibration_factor: memPlace: 0
18:35:45.581 -> Verdi 1: 1.6972
18:35:45.581 -> Verdi 2: 0
18:35:45.581 -> Verdi 3: 1.6972
#include <EEPROM.h>
//float redValue = 0;
int redValue = 0;
float value = 0;
int memPlace = 0;
float calibrationFactor = 12.345;
int xFact = 1000;
int calToStore = 0;
void setup() {
Serial.begin(57600);
while (!Serial) // wait for user to open serial monitor
delay(500);
int calToStore = calibrationFactor * xFact;
Serial.print("calToStore: ");
Serial.println(calToStore);
EEPROM.begin(515); //Initialize EEPROM
Serial.println(F("."));
// write to EEPROM ESP32
Serial.println(redValue);
EEPROM.write(memPlace, calToStore);
//EEPROM.write(memPlace, *(byte*)(&calibrationFactor)); //also dont work...
EEPROM.commit();
}
void loop() {
Serial.print("EEPROM read Stored calibration_factor: memPlace: ");
Serial.println(memPlace);
int redValue = EEPROM.read(memPlace);
Serial.printf("Verdi 1: %0.4f", redValue / xFact); //f=float, %0.2f to display the floating-point number with 2 decimal places.
Serial.println(F(""));
Serial.print("Verdi 2: ");
Serial.println(redValue / xFact);
Serial.printf("Verdi 3: %0.4f", EEPROM.read(memPlace) / xFact); //f=float, %0.4f to display the floating-point number with 4 decimal places.
Serial.println(F(""));
delay(5000);
}
Here is fasit to store and retrieve a float (or more) for an ESP32 borad.
Issue is that eeprom is not part of ESP so need to use library: Preferences.h
NB in serial print give number of decimals, also note that it rounds up
3.4567 with 3 desimals will be printed as 3.457
Both codes testet with power loss.
#include <Preferences.h>
Preferences preferences;
void setup() {
Serial.begin(57600);
Serial.println();
preferences.begin("hiveMon", false);
float valueToSave = 3.456789;
preferences.putFloat("hiveMon", valueToSave); // diable to test if value is stored after power/rest
preferences.end();
}
void loop() {
preferences.begin("hiveMon", false);
float retrievedValue = preferences.getFloat("hiveMon", 0.0);
preferences.end();
// Use the retrievedValue
Serial.print("retrievedValue: ");
Serial.println(retrievedValue,4); // DESIMALS IMPORTANT I YOU NEED MORE THAN 2
delay(1000);
}
If more than one float see this example:
#include <Preferences.h>
Preferences preferences;
void setup() {
Serial.begin(57600);
Serial.println();
preferences.begin("hiveMon", false);
// Remove all preferences under the opened namespace
//preferences.clear();
// Or remove the counter key only
//preferences.remove("counter");
// Get the counter value, if the key does not exist, return a default value of 0
// Note: Key name is limited to 15 chars.
float calibration_factor = -9000.234567;
float currentOffset = 4.56789;
float calYesNo = 0;
preferences.putFloat("myCal_factor", calibration_factor); // max 15 letters NameSpace
preferences.putFloat("myCurrentOffset", currentOffset);
preferences.putFloat("myCalYesNo", calYesNo);
preferences.end();
}
void loop() {
preferences.begin("hiveMon", false);
float calibration_factor = preferences.getFloat("myCal_factor", 0.0);
float currentOffset = preferences.getFloat("myCurrentOffset", 0.0);
float calYesNo = preferences.getFloat("myCalYesNo", 0.0);
preferences.end();
Serial.print("calibration_factor: ");
Serial.println(calibration_factor, 4);
Serial.print("currentOffset: ");
Serial.println(currentOffset, 4);
Serial.print("calYesNo: ");
Serial.println(calYesNo, 4);
delay(1000);
}