EEPROM commands, UNO vs ESP8266

Hi

I have a code that is made for UNO (Atmega328) and I try to reuse it to a ESP8266.

The code is to auto-calibrate a weigth/scale. And also show actual load on scale after a reboot, and not a tare() value =0.

The store to eeprom seems to not work with my ESP8266 system so I wonder if the code should be different regarding EEPROM commands

The serial monitor report NOT INIT !!!! + MUST CALIBIRATE after a calibration factor calculation

Here is the original code (I have commented out RF module and LCD crystal)

My question is to get help to sort out EEPROM commands to store calibration factor in EPPROM


// https://www.youtube.com/watch?v=Sfp3wD4SVbM&t=321s
// https://create.arduino.cc/projecthub/talofer99/auto-calibrate-hx711-scale-with-known-weight-7ae97d




#include "HX711.h"
#include <Wire.h>
//#include <LiquidCrystal_I2C.h>
//#include <nRF24L01.h>
//#include "printf.h"
//#include <RF24.h>
//#include <RF24_config.h>
#include <math.h>
#include <EEPROM.h>


#define LB2KG  0.45352  //omregningsfaktor
#define CALWEIGHT 1.38   // kjent vekt i kg
#define DEFAULT_CALIFACTOR 21    // tilfeldig valgt, min vekt 2 er ca 21
#define EEPROM_SIZE 12


#define id 1
#define number_of_digits 1

#define DOUT D3
#define CLK D4
#define button D1  //pin connected to Tare on sender

HX711 scale;
//LiquidCrystal_I2C lcd(0x27, 16, 2);
//RF24 radio (4, 5);
const byte addresses[][6] = {"00001", "00002"};
long currentOffset;
float calibration_factor;


void setup() {
  //serial
  Serial.begin(115200);
  Serial.println("System started");
//  printf_begin();   //LCD

  // lcd
  //lcd.begin();
  //lcd.backlight();
  //lcd.clear();
  EEPROM.write(0x00,0xFF);
  Serial.println("==== EEPROM");
//  button pin
  pinMode(button, INPUT_PULLUP);
   Serial.println("==== pinMode");
  
  
  
  // eeprom
  if (EEPROM.read(0x00) != 0x01) 
  {
    Serial.println("NOT INIT !!!!");
    currentOffset = 0;
    calibration_factor = DEFAULT_CALIFACTOR;     
    // show instructions
//    lcd.setCursor(0, 0);
//    lcd.print("MUST CALIBIRATE");
//    lcd.setCursor(0, 1);
//    lcd.print("Press Button");

    Serial.println("MUST CALIBIRATE");
    Serial.println("Press Button");
    //wait for button press
    while (digitalRead(button));
//    lcd.clear();
  }
  else
  {
    EEPROM.get(0x01,currentOffset);
    EEPROM.get(0x01+sizeof(long),calibration_factor);   
    Serial.println("currentOffset = " + String(currentOffset));
    Serial.println("calibration_factor = " + String(calibration_factor));
  }
  

  //scale
  scale.begin(DOUT, CLK);
  delay(100);
  Serial.println("calibration_factor = " + String(calibration_factor));
  scale.set_scale(calibration_factor / LB2KG);

  // if button is pressed (LOW) start calibiration
  if (!digitalRead(button))
  {
    // show instructions
//    lcd.setCursor(0, 0);
//    lcd.print("Clear Scale");
Serial.print("Clear Scale ");
    // wait till person leaves the button
    while (!digitalRead(button));
    //short delay
    delay(200);
    
    // set tare and save value
    scale.tare();
    currentOffset = scale.get_offset();
    Serial.println(currentOffset);

    // show on lcd
/*    lcd.clear();
    lcd.print("Place 1,38 Kg");
    lcd.setCursor(0, 1);
    lcd.print("Press Button");
*/  
Serial.print("Place 1,38 Kg");  
delay(5000);

    //wait for button press
    while (digitalRead(button));
//    lcd.clear();
//    lcd.print("Please wait ");

    Serial.println("calibrate ");
    // calibiation
    boolean done = false;
    uint8_t flipDirCount = 0;
    int8_t direction = 1;
    uint8_t dirScale = 100;
    double data = abs(scale.get_units());
    double prevData = data;
    char runningSign[] = {'-','\\','|','/'};
    uint8_t runningSignIdx = 0;
    while (!done)
    {
      // get data
      data = abs(scale.get_units());
      Serial.println("data = " + String(data, 2));
      Serial.println("abs = " + String(abs(data - CALWEIGHT), 4));
      Serial.println("calibration_factor = " + String(calibration_factor));
      // if not match
      if (abs(data - CALWEIGHT) >= 0.01)
      {
        if (abs(data - CALWEIGHT) < abs(prevData - CALWEIGHT) && direction != 1 && data < CALWEIGHT)
        {
          direction = 1;
          flipDirCount++;
        }
        else if (abs(data - CALWEIGHT) >= abs(prevData - CALWEIGHT) && direction != -1 && data > CALWEIGHT)
        {
          direction = -1;
          flipDirCount++;
        }

        if (flipDirCount > 2)
        {
          if (dirScale != 1)
          {
            dirScale = dirScale / 10;
            flipDirCount = 0;
            Serial.println("dirScale = " + String(dirScale));
          }
        }
        // set new factor 
        calibration_factor += direction * dirScale;
        scale.set_scale(calibration_factor / LB2KG);
        // show still running 
//        lcd.setCursor(15, 1);
//        lcd.print(runningSign[runningSignIdx]);
        runningSignIdx = (runningSignIdx+1)%4;
        //short delay
        delay(5);
        // keep old data 
        prevData = data;
      }
      // if match
      else
      {
        Serial.println("NEW currentOffset = " + String(currentOffset));
        Serial.println("NEW calibration_factor = " + String(calibration_factor));
        EEPROM.put(0x00,0x01); // set init
        EEPROM.put(0x01,currentOffset);
        EEPROM.put(0x01+sizeof(long),calibration_factor);  
        done = true;
//        lcd.clear();
      }

    } // end while
  } //end if button pressed

  scale.set_offset(currentOffset);

/*  lcd.setCursor(0, 0);
  lcd.print("Scaleit");
  lcd.setCursor(13, 1);
  lcd.print("KG");
*/
Serial.print("Scaleit   ");
Serial.print("Kg");

  /*/ radio
  radio.begin();
  radio.openWritingPipe(addresses[1]); // 00002
  radio.openReadingPipe(1, addresses[0]); // 00001
  radio.setPALevel(RF24_PA_MIN);
  radio.setDataRate(RF24_250KBPS);
  radio.setChannel(100);
  radio.stopListening();
  radio.printDetails();
*/
  
  Serial.println("setup done ...");

}

void loop() {
  // get data
  double data = abs(scale.get_units());
  // issue with abs missing at the 4th digit after the dot (bug!!)
  if (0.0000 - data > 0.0001) 
    data = 0.00; //reset to zero
    
  
  // serial
  Serial.print(data, number_of_digits);
  Serial.println(" Kg");

  // lcd
//  lcd.setCursor(8, 1);
//  lcd.print(data, number_of_digits);



  // nrf
  double data2[2] = {id, data};
//  radio.write(&data2, sizeof(data2));

  // short delay
  delay(1000);

}

Did you forget to use EEPROM.begin() with the ESP8266 code to set the EEPROM size ?

... and a EEPROM.commit() after a write/put?

Solution!

Thanks!

For ESP8266 the begin must have an argument in void setup() like:

EEPROM.begin(100);

@noiasca
Don't seems that commit() is needed

It is needed. I suspect you have not tested sufficiently yet to understand why. Read this:

https://arduino-esp8266.readthedocs.io/en/latest/libraries.html#eeprom

An important thing to understand is that esp8266 does not have any EEPROM memory like an Uno does. Instead, the esp8266 EEPROM library uses some Flash memory to emulate the EEPROM in an Uno. But Flash memory is not the same as EEPROM and cannot withstand as many write operations before it wears out and stops working. So you should think carefully about how often the Flash memory will be written to when the scales are in use.

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