Close

Record weight to EEPROM

A project log for WiFi Bamboo Bathroom Scale

Records weight online via MQTT. Displays time, date, inside temp, outside temp data received from a second existing controller.

nodemcu12ecanadanodemcu12ecanada 07/23/2022 at 00:370 Comments

On a power failure the recorded weight and zero offset was lost or defaulted to an old value put in the program.

To prevent this it now reads the previous weight and zero offset stored in the EEPROM on boot only.

NodeMCU "EEPROM" is actually flash memory configured to act like an EEPROM. Flash memory has a limited number of cycles for writing.

Writing is only guaranteed for 10,000 cycles so only write when needed. In this case only once per day. 

It may or may not last 100,000 cycles or more.

The float weightrecord value is broken into two 8 bit integers to be stored. One value to the left of the decimal point and the other to the right. After the values are read they are combined back to a float value as shown.

In the setup I added:

   EEPROM.begin(512);

        epromrd1 = EEPROM.read(1); // read previous weight stored in EEPROM on boot
        epromrd2 = EEPROM.read(2); 
        weightrecord = (epromrd1*10.0 + epromrd2)/10.0; // calculate weightrecord from EEPROM on boot

In the loop I added EEPROM.write commands  

New weightrecord written to EEPROM once between 5 and 9 am if above 165 lbs (me). 

   
    if((hr==4)&&(mn==0)){  // set save and read bits once a day at 4 a.m.
      savebit = 1;
      readbit = 1;
    }
   

  if ((hr>4)&&(hr<9)&&(weightlbsdisplay > 165.0)){ 
        weightrecord = weightlbsdisplay;

         epromwt1 = weightrecord; // break weightrecord into before and after decimal integers, 
         epromwt2 = (weightrecord*10) - (epromwt1*10);
        
         if (savebit == 1){ // save weightrecord to EEPROM reads on boot so latest weight not lost on power failure
          EEPROM.write(1,epromwt1);
          EEPROM.write(2,epromwt2);
          EEPROM.commit(); // copy entire contents to EEPROM, only writes if data is different
          savebit=0;
      }
   

Similar additions were made for saving and reading the zero offset.

The 548503 or thereabouts zero offset is broken into three 8-bit numbers. 54, 85, 03 for EEPROM storage. 

255 is the max number stored in an EEPROM memory location. Up to 512 memory locations.

For more info about using the EEPROM commands with NodeMCU or ESP32 go to:

 https://randomnerdtutorials.com/esp32-flash-memory/

Discussions