user80615: I would like to save the height of a tank in case of power outage with EEPROM and every time I reboot the board. However, I could not do so after using EEPROM.commit() , EEPROM.get and EEPROM.put as the reading of the height tank changes every time I tried to reboot it . Am I using it wrongly?
I used EEPROM.get , EEPROM.put , EEPROM.commit in the void setup section so that it will save the data during boot up. However, the tank height just change whenever I changed to a different tank with different height after boot up.
How could I recalibrate the height of the tank if I would like to change to another tank one day, can I just click the RST button on the ESP8266 board?
Thank you
#include <LiquidCrystal_I2C.h>  //1602 LCD library
#include <Wire.h>
#include <EEPROM.h>
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>  //NodeMCU ESP8266 library
#include <BlynkSimpleEsp8266.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);


WidgetLED led1(V2);


 
char auth[] = ""; //Enter your Auth token
char ssid[] = ""; //Enter your WIFI name
char pass[] = ""; //Enter your WIFI password
 
BlynkTimer timer;
bool pinValue = 1;


long duration;
int distance; 
int percentage;
int addr=0;
float tankheight, val;

 
#define trig D7
#define echo D8
#define relay D5
 
void setup() {
  Serial.begin(9600);
  EEPROM.begin(512);
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  pinMode(relay, OUTPUT);
  Wire.begin(D2, D1);
  lcd.init();
  lcd.backlight();
  Blynk.begin(auth, ssid, pass);
  digitalWrite(trig, LOW);
  delayMicroseconds(4);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  duration = pulseIn(echo, HIGH);
  tankheight = duration * 0.034 / 2;
  Serial.print("Tank height");
  Serial.print(tankheight);
  EEPROM.put(addr, tankheight);
  EEPROM.commit();
  val=EEPROM.get(addr, tankheight);
  Serial.print("Addr");
  Serial.print(val);
  timer.setInterval(1L, Wlevel);
  digitalWrite(relay, HIGH);
}
 


BLYNK_CONNECTED(){
  Blynk.syncAll();

  }
 
BLYNK_WRITE(V0) {
  pinValue = param.asInt();

}



void loop() {
  Blynk.run();
  timer.run();
  WiFi.setSleepMode(WIFI_NONE_SLEEP);

}

void Wlevel(){  // Initialize detecting water level and relay)
  {
  digitalWrite(trig, LOW);
  delayMicroseconds(4);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  duration = pulseIn(echo, HIGH);
  distance = duration * 0.034 / 2;
  percentage= map(distance,val,10 ,0, 100);
 
  Blynk.virtualWrite(V1, percentage);  

  if(percentage<0)
  {
    percentage = 0;
  }
  else if(percentage>100)
  {
    percentage = 100;
  }

 }
  

 if (pinValue == 1)
    {
       if (percentage < 100)
       {
          digitalWrite(relay, LOW);
          Serial.print("Water level:");
          Serial.print(percentage);
          Serial.print("%");
          Serial.println(" Pump is ON");
          led1.on();
          lcd.setCursor(0,0);
          lcd.print("Water Lvl:");
          lcd.print(percentage);
          lcd.print("%  ");
          lcd.setCursor(0,1);
          lcd.print("Pump: ON  (AUTO)");
          
       } 
       
       else 
       {
          digitalWrite(relay, HIGH);
          Serial.println("Water level is 100%   Pump is OFF");
          led1.off();
          lcd.setCursor(0, 0);
          lcd.print("Water Lvl:");
          lcd.print(percentage);
          lcd.print("%  ");
          lcd.setCursor(0,1);
          lcd.print("Pump: OFF (AUTO)");
          
       }
    }
   else
    {
      digitalWrite(relay, HIGH);
      Serial.print("Water level:");
      Serial.print(percentage);
      Serial.print("%");
      Serial.println("System is OFF, Check Blynk");
      lcd.setCursor(0,0);
      lcd.print("Water Lvl:");
      lcd.print(percentage);
      lcd.print("%  ");
      lcd.setCursor(0,1);
      lcd.print("System is OFF   ");
      led1.off();
      
    }

   }


user68729: Use a different button - you may have to wire something up...
In the loop, read the button and only if it is pressed should you re-calibrate and set the value to eeprom.  You can do it in setup too but then you would have to reboot with the button pressed to trigger re-calibration.
You can also read a serial command in the loop.  Anything external that can be triggered with a separate intent to re-calibrate would work.  A power outage resembles the normal reset too much.  To execute different behavior in two different scenarios, you must be able to detect which scenario you are in.

80615: <p>I would like to save the height of a tank in case of power outage with EEPROM and every time I reboot the board. However, I could not do so after using EEPROM.commit() , EEPROM.get and EEPROM.put as the reading of the height tank changes every time I tried to reboot it . Am I using it wrongly?</p>
<p>I used EEPROM.get , EEPROM.put , EEPROM.commit in the void setup section so that it will save the data during boot up. However, the tank height just change whenever I changed to a different tank with different height after boot up.</p>
<p>How could I recalibrate the height of the tank if I would like to change to another tank one day, can I just click the RST button on the ESP8266 board?</p>
<p>Thank you</p>
<pre><code>#include &lt;LiquidCrystal_I2C.h&gt;  //1602 LCD library
#include &lt;Wire.h&gt;
#include &lt;EEPROM.h&gt;
#define BLYNK_PRINT Serial
#include &lt;ESP8266WiFi.h&gt;  //NodeMCU ESP8266 library
#include &lt;BlynkSimpleEsp8266.h&gt;
LiquidCrystal_I2C lcd(0x27, 16, 2);


WidgetLED led1(V2);


 
char auth[] = &quot;&quot;; //Enter your Auth token
char ssid[] = &quot;&quot;; //Enter your WIFI name
char pass[] = &quot;&quot;; //Enter your WIFI password
 
BlynkTimer timer;
bool pinValue = 1;


long duration;
int distance; 
int percentage;
int addr=0;
float tankheight, val;

 
#define trig D7
#define echo D8
#define relay D5
 
void setup() {
  Serial.begin(9600);
  EEPROM.begin(512);
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  pinMode(relay, OUTPUT);
  Wire.begin(D2, D1);
  lcd.init();
  lcd.backlight();
  Blynk.begin(auth, ssid, pass);
  digitalWrite(trig, LOW);
  delayMicroseconds(4);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  duration = pulseIn(echo, HIGH);
  tankheight = duration * 0.034 / 2;
  Serial.print(&quot;Tank height&quot;);
  Serial.print(tankheight);
  EEPROM.put(addr, tankheight);
  EEPROM.commit();
  val=EEPROM.get(addr, tankheight);
  Serial.print(&quot;Addr&quot;);
  Serial.print(val);
  timer.setInterval(1L, Wlevel);
  digitalWrite(relay, HIGH);
}
 


BLYNK_CONNECTED(){
  Blynk.syncAll();

  }
 
BLYNK_WRITE(V0) {
  pinValue = param.asInt();

}



void loop() {
  Blynk.run();
  timer.run();
  WiFi.setSleepMode(WIFI_NONE_SLEEP);

}

void Wlevel(){  // Initialize detecting water level and relay)
  {
  digitalWrite(trig, LOW);
  delayMicroseconds(4);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  duration = pulseIn(echo, HIGH);
  distance = duration * 0.034 / 2;
  percentage= map(distance,val,10 ,0, 100);
 
  Blynk.virtualWrite(V1, percentage);  

  if(percentage&lt;0)
  {
    percentage = 0;
  }
  else if(percentage&gt;100)
  {
    percentage = 100;
  }

 }
  

 if (pinValue == 1)
    {
       if (percentage &lt; 100)
       {
          digitalWrite(relay, LOW);
          Serial.print(&quot;Water level:&quot;);
          Serial.print(percentage);
          Serial.print(&quot;%&quot;);
          Serial.println(&quot; Pump is ON&quot;);
          led1.on();
          lcd.setCursor(0,0);
          lcd.print(&quot;Water Lvl:&quot;);
          lcd.print(percentage);
          lcd.print(&quot;%  &quot;);
          lcd.setCursor(0,1);
          lcd.print(&quot;Pump: ON  (AUTO)&quot;);
          
       } 
       
       else 
       {
          digitalWrite(relay, HIGH);
          Serial.println(&quot;Water level is 100%   Pump is OFF&quot;);
          led1.off();
          lcd.setCursor(0, 0);
          lcd.print(&quot;Water Lvl:&quot;);
          lcd.print(percentage);
          lcd.print(&quot;%  &quot;);
          lcd.setCursor(0,1);
          lcd.print(&quot;Pump: OFF (AUTO)&quot;);
          
       }
    }
   else
    {
      digitalWrite(relay, HIGH);
      Serial.print(&quot;Water level:&quot;);
      Serial.print(percentage);
      Serial.print(&quot;%&quot;);
      Serial.println(&quot;System is OFF, Check Blynk&quot;);
      lcd.setCursor(0,0);
      lcd.print(&quot;Water Lvl:&quot;);
      lcd.print(percentage);
      lcd.print(&quot;%  &quot;);
      lcd.setCursor(0,1);
      lcd.print(&quot;System is OFF   &quot;);
      led1.off();
      
    }

   }
</code></pre>
 68729: <p>Use a different button - you may have to wire something up...</p>
<p>In the loop, read the button and only if it is pressed should you re-calibrate and set the value to eeprom.  You can do it in setup too but then you would have to reboot with the button pressed to trigger re-calibration.</p>
<p>You can also read a serial command in the loop.  Anything external that can be triggered with a separate intent to re-calibrate would work.  A power outage resembles the normal reset too much.  To execute different behavior in two different scenarios, you must be able to detect which scenario you are in.</p>
