Store float to eeprom ESP32

Hi

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);
}

Try;

Serial.println(value,n);

Where n is the number of decimal places you want printed.

1 Like

what does the doc/code say about the returned value of a read() or the parameter of a write()?

(use the ESP32 preference library, there is no EEPROM anyway on an ESP32 - tutorial here for example)


not the issue here, if it were a full float being read, the default print would give you 2 digits after the decimal point anyway

1 Like

Your code is a mess...
Please point me a line, where you store the calibration_factor value to the EEPROM? You didn't do it at all!

Where is the thousandCal declared?

If the printout had not been 33.89, but 33.00, its a clue.

1 Like

Use EEPROM.get() and EEPROM.put() instead of EEPROM.read() and EEPROM.write() (or use the Preferences library as mentioned before).

Hi

thanks for helping me understand ESP32.

I tested the http://RandomNerdTutorials.com tutorial, it worked for a int
https://raw.githubusercontent.com/RuiSantosdotme/Random-Nerd-Tutorials/master/Projects/ESP32/Preferences/ESP32_StartCounter.ino
and it worked for an int

I will put a
float counter = 23.67;

in memory with the preferences library

Result 536870912, not 23.67:

image

My code:

#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() {

}

// Serial.printf("Current counter value: %u\n", counter);
Serial.printf("Current counter value: %f\n", counter);

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)

1 Like

Hurray

@cattledog

This is the trick!!!

Serial.printf("Current counter value: %f\n", counter);

image

And thanks to random Neard!

Final code:

#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() {

}

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