when I try to store a value 33.89 to ESP32 eeprom I only get 33 out on serial monitor.
I figure it has something to do with bits and bytes in memory, but I dont get it.
I also tried to multiply 33.89 with 100 = 3389 and store that number and on eeprom.read divide by 100, no luck
Any tip to get?
Code:
#include <EEPROM.h>
int ADDRESS = 3; //eeprom adresse å skrive og lese til
float calibration_factor = 33.89;
void setup() {
Serial.begin(57600);
while (!Serial) // wait for user to open serial monitor
delay(1000);
EEPROM.begin(512); //Initialize EEPROM
Serial.println("");
Serial.print("calibration_factor to store = ");
Serial.println(calibration_factor);
// write to EEPROM esp32
EEPROM.write(ADDRESS, thousandCal);
EEPROM.commit();
}
void loop() {
Serial.println("");
Serial.println("EEPROM read Stored calibration_factor");
Serial.print("Adresse: ");
Serial.println(ADDRESS);
Serial.print("Value: ");
float value = (EEPROM.read(ADDRESS));
Serial.println(value);
delay(3000);
}
#include <Preferences.h>
Preferences preferences;
float counter = 23.67;
void setup() {
Serial.begin(115200);
Serial.println();
// Open Preferences with my-app namespace. Each application module, library, etc
// has to use a namespace name to prevent key name collisions. We will open storage in
// RW-mode (second parameter has to be false).
// Note: Namespace name is limited to 15 chars.
preferences.begin("my-app", false);
// Remove all preferences under the opened namespace
preferences.clear();
// Or remove the counter key only
preferences.remove("counter");
// Store the counter to the Preferences
preferences.putFloat("counter", 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.
counter = preferences.getFloat("counter", 0);
// Print the counter to Serial Monitor
Serial.printf("Current counter value: %u\n", counter);
// Close the Preferences
preferences.end();
// Wait 10 seconds
Serial.println("Restarting in 10 seconds...");
delay(10000);
// Restart ESP
ESP.restart();
}
void loop() {
}
You need to be careful with printf
If you use the wrong format description then you get garbage (sometimes the compiler will warn you if you turn on all warnings)
#include <Preferences.h>
Preferences preferences;
float counter = 23.67;
void setup() {
Serial.begin(115200);
Serial.println();
// Open Preferences with my-app namespace. Each application module, library, etc
// has to use a namespace name to prevent key name collisions. We will open storage in
// RW-mode (second parameter has to be false).
// Note: Namespace name is limited to 15 chars.
preferences.begin("my-app", false);
// Remove all preferences under the opened namespace
preferences.clear();
// Or remove the counter key only
preferences.remove("counter");
// Store the counter to the Preferences
preferences.putFloat("counter", 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.
counter = preferences.getFloat("counter", 0);
// Print the counter to Serial Monitor
//Serial.printf("Current counter value: %u\n", counter);
Serial.printf("Current counter value: %f\n", counter);
// Close the Preferences
preferences.end();
// Wait 10 seconds
Serial.println("Restarting in 10 seconds...");
delay(10000);
// Restart ESP
ESP.restart();
}
void loop() {
}