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