Can the virtual pin value be kept when device disconnected from wifi?

I am using a virtual pin V8 to store the water level by counting the motor ON time.
Initial value of V8 = 1000 (i.e. max water level)
when I turn on motor for 3 sec, V8 will be 997
when I turn on motor for 5 sec, V8 will be 992, etc.

However, in reality, the device may be disconnected from the wifi for a while, the value of V8 will be backed to 1000, is any method that I can keep the value of V8 even disconnect from wifi?

Below is my coding


//  LIBRARY
//====================================================
#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
//====================================================

//  DEKLARASI AWAL
//====================================================
char auth[] = "";
char ssid[] = "";
char pass[] = "";
#define EspSerial Serial3
#define ESP8266_BAUD 115200
ESP8266 wifi(&EspSerial);


//====================================================

//BLYNK_WRITE(V0) 
//{
//  drink = param.asInt(); 

//}
BlynkTimer timer;
int pump_on;
int interval;
int interval1;
int timer_no;
int drink_level1 = 1000;
  
BLYNK_WRITE(V6)
{
  pump_on = param.asInt();
  if (pump_on == 1)
  {
  digitalWrite(52, HIGH);
  timer_no = timer.setTimeout(interval1, []()
  {
  digitalWrite(52, LOW);
  Blynk.virtualWrite(V6, 0);
  drink_level1 = drink_level1 - interval;
  });
  
  }
  else {digitalWrite(52,LOW);
  timer.deleteTimer(timer_no);
  }
}

BLYNK_READ(V8)
{
 Blynk.virtualWrite(V8, drink_level1);
}

BLYNK_WRITE(V7)
{
  interval = param.asInt();
  interval1 = interval * 1000.0;
}


//====================================================


void setup()
{
  pinMode(52, OUTPUT);
  Serial.begin(115200);
  Serial3.begin(115200);

  delay(10);
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);                          //Reguler server
  //Blynk.begin(auth, wifi, ssid, pass, "192.168.43.120", 8080);    //Local server
}

void loop()
{
  Blynk.run();
  timer.run();
  if ( Serial3.available() )   {
    Serial.write( Serial3.read() );
  }
  if ( Serial.available() )       {
    Serial3.write( Serial.read() );
  }

}

Well, Blynk.begin is a blocking function, so if you don’t have a connection to either WiFi or the Blynk server then you code won’t progress beyond the Blynk.begin line
Setting-up your own WiFi connection then using Blunk.config and Blynk.connect will fix that one. A search of the forum will reveal some examples, although life is more difficult when you use the Arduino/ESP-01 combo.

Then, I’d get rid of all the BLYNK_READ(V8) stuff and simply maintain a count of the water level within your code and upload it to Blynk if you can.
It would also make sense to write the value to non-volatile memory so that it persists if power is lost, but with the Arduino the only option there is EEPROM which has a read-write life of around 100,000 operations, so isn’t a great idea.
If you were using an ESP8266 NodeMCU device then you could use SPIFFS, or the new FS, and if you were using an ESP32 then you could also use RTC memory.

Pete.

1 Like

Thank you Pete

Thank you Pete, now it works


int drink_level;

BLYNK_CONNECTED()
{
Blynk.syncVirtual(V6);
}

BLYNK_WRITE(V6)
{
drink_level = param.asInt();
}

BLYNK_WRITE(V5) //Button to refill
{
   int refill = param.asInt();
   if (refill == 1)
   {
   drink_level = drink_level + 50000.0;
   Blynk.virtualWrite(V6, drink_level); //V6 is level widget
   }
}

BLYNK_WRITE(V1) //Button to activate pump
{
int confirmPressed = param.asInt();
if (confirmPressed == 1)
{
pump_on();
}
}

void pump_on()
{
digitalWrite(1, HIGH)
int pumpTime = 5000;
timer.getTimeout(pumpTime, []()
{
digitalWrite(1, LOW)
drink_level = drink_level - pumpTime;
Blynk.virtualWrite(V6, drink_level);
}

void setup()
{
  Serial.begin(115200);
Blynk.config(auth);
Blynk.connectWiFi(ssid, pass);
Blynk.connect();
pinMode(1, OUTPUT);
}

void loop()
{
Blynk.run();
}