-1

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

   }
6
  • have you actually examined your own program? ... what's the point of using the EEPROM when first thing you do is to wipe out the stored value ?
    – jsotola
    Oct 3 at 4:18
  • @jsotola I am a beginner to programming and I am trying very hard. Which part of the code that will wipe out the stored value? I thought I put the EEPROM command in the void setup so that the tank height will be put inside the EEPROM? Am I doing it wrongly?
    – clement01
    Oct 3 at 4:24
  • take a look at all program lines that start with EEPROM ... see what each one does
    – jsotola
    Oct 3 at 4:26
  • why do you want to save the water level?
    – jsotola
    Oct 3 at 4:34
  • 1
    and you do not see a problem with first saving the water level after reboot? ... think about it ... this really has nothing to do with programming
    – jsotola
    Oct 3 at 6:40
1

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.

2
  • How about if I add an virtual button in the Blynk apps so that I can calibrate it using the Blynk app instead a physical button? Regarding the calibration and EEPROM, am I supposed to create another loop in another function or inside the void setup function is enough ? Thank you
    – clement01
    Oct 5 at 14:57
  • virtual button could work. if there is some gui in the Blynk apps, then your call to write eeprom belongs in something called by Blynk.run() (which your loop function calls) not setup.
    – Abel
    Oct 6 at 13:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.