How do you make the system on the ESP8266 keep running automatically, when the wifi or internet connection is down?

how do you make the system on the ESP8266 keep running automatically, when the wifi or internet connection is down?

Hello, I’m Andhika working on a project for my final assignment, on this topic I want to ask about how to make the system on ESP8266 run automatically even if the wifi or internet connection is off

Thank You

Hardware : ESP8266 V3 LUA
Smartphone : Android 12
Blynk Server

This has been covered many times before on the forum.
You need to manually manage your WiFi connection, then use Blynk.config(optional timeout) and Blynk.connect() instead of Blynk.begin().
You also need your Blynk.run() inside a Blynk.connected() logical test.

Do a bit of searching and you’ll find more info.

Pete.

hi peter knight thank you for replying to this topic, for an example program is there a reference, just to shorten the time because I am being chased by a work deadline

This is my Code

#define BLYNK_TEMPLATE_ID "TMPLmHgrX3De"
#define BLYNK_DEVICE_NAME "TA"
#include <LiquidCrystal_I2C.h>
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Servo.h>
BlynkTimer timer;
LiquidCrystal_I2C lcd(0x27, 20, 4);
bool pinValue = 0;
int frekuensi=250;
int Mode=0;
int maximal=0;
int minimal=0;
//-----------------------------------------------------------------
//Konfigurasi
// 14 untuk token blynk template
// 15 konfigurasi ke wifi dengan memasukan ssid wifi
// 16 konfigurasi ke wifi dengan memasukan password wifi
char auth[] = "DUgjSPuOAr8tfk7JeB7JoAPEtOnDS-0V";//Enter your Auth token
char ssid[] = "Jangan nyambung ini virus";
char pass[] = "";
// set variabel untuk pewaktu blynk dan set variable untuk motor servo
Servo relay;
>//-----------------------------------------------------------------
// mendefinisikan pin yang digunakan masing masing komponen
// Define the component pins
#define trig 12
#define echo 13
#define LED1 0
#define LED2 2
#define LED3 3
#define buzzer 14
//------------------------------------------------------------------
//Proses pengekesusian
//33 merupakan sebuah fungsi yang akan menjalankan program dan setiap fungsi fungsi yang berada dalam void setup akan dieksekusi lebih dahulu dan akan berjalanan hanya satu kali
void setup() {
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0,0);
  lcd.print("-PINTU AIR OTOMATIS-");
  lcd.setCursor(0,1);
  lcd.print("====================");
  pinMode(trig, OUTPUT);// sebagai awal pembacaan sensor jarak HCSR-04
  pinMode(echo, INPUT);// sebagai awal pembacaan sensor jarak HCSR-04
  pinMode(LED1, OUTPUT);// sebagai perintah untuk melakukan setting LED 1 ke mode OUTPUT
  pinMode(LED2, OUTPUT);// sebagai perintah untuk melakukan setting LED 2 ke mode OUTPUT
  pinMode(LED3, OUTPUT);// sebagai perintah untuk melakukan setting LED 3 ke mode OUTPUT
  pinMode(buzzer, OUTPUT);// sebagai perintah untuk melakukan setting Buzzer ke mode OUTPUT
  relay.attach(15);
  relay.write(0);
  Blynk.begin(auth, ssid, pass);
  lcd.setCursor(0,2);
  lcd.print(ssid);
  delay(1000);
  lcd.clear();
  while (WiFi.status() != WL_CONNECTED)
  {
    lcd.backlight();
    lcd.print(0,3);
    lcd.print("WiFi connected");
    delay(500);
    lcd.clear();
  }
  delay(1000);
  lcd.clear();
  timer.setInterval(1000L, ultrasonic);
}
  BLYNK_WRITE(V1) {
 pinValue = param.asInt();
 if ((Mode == 0 ) && (pinValue == 1)){
 relay.write(180);
 lcd.setCursor(0,0);
 lcd.print("[PINTU AIR MANUAL]");
 lcd.setCursor(0,1);
 lcd.print("====================");
 lcd.setCursor(0,2);
 lcd.print("Status : AMAN");
 lcd.setCursor(0,3);
 lcd.print("Pintu Air Terbuka");
 delay(1500);
 lcd.clear();
}
 else if ((Mode == 0 ) && (pinValue == 0)){
 relay.write(0);
  lcd.setCursor(0,0);
  lcd.print("[PINTU AIR MANUAL]");
  lcd.setCursor(0,1);
  lcd.print("====================");
  lcd.setCursor(0,2);
  lcd.print("Status : AMAN");
  lcd.setCursor(0,3);
  lcd.print("Pintu Air Tertutup");
  delay(1500);
  lcd.clear();
}
}
BLYNK_WRITE(V2){
  pinValue = param.asInt();
  Mode=pinValue;
}
BLYNK_WRITE(V3)
{
  maximal = param.asInt();
}
BLYNK_WRITE(V4){
  minimal = param.asInt();
}
BLYNK_CONNECTED(){
  Blynk.syncAll();
}
void ultrasonic() {
  long t;
  int jarak=0;
  int jarakAir = 15;
  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  t = pulseIn(echo, HIGH);
  jarak = (t / 2) /29.1;
  int tampilblynk = (jarak - jarakAir)* -1;
  if(Mode==1){
  if (jarak <= jarakAir) {
  Blynk.virtualWrite(V0, tampilblynk);
  }
  else{
  Blynk.virtualWrite(V0, 0);
  }
  lcd.setCursor(0,0);
  lcd.print("[PINTU AIR OTOMATIS]");
  lcd.setCursor(0,1);
  lcd.print("====================");
  if (jarak == maximal && jarak < minimal){
    lcd.setCursor(0,2);
    lcd.print("Status : BAHAYA ");
    lcd.setCursor(0,3);
    lcd.print("Pintu Air Terbuka");
    delay(1000);
    lcd.clear();
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED3, HIGH);
    tone(buzzer, frekuensi);
    relay.write(180);
  } 
      else if (jarak == minimal) {
    lcd.setCursor(0,2);
    lcd.print("Status : AMAN ");
    lcd.setCursor(0,3);
    lcd.print("Pintu Air Tertutup");
    delay(1000);
    lcd.clear();
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, LOW);
    noTone (buzzer);
    relay.write(0);
   }
  }
else if(Mode==0){
  if (jarak <= jarakAir) {
  Blynk.virtualWrite(V0, tampilblynk);
  }
  else{
  Blynk.virtualWrite(V0, 0);
  }
  lcd.setCursor(0,0);
  lcd.print("[PINTU AIR MANUAL]");
  lcd.setCursor(0,1);
  lcd.print("====================");
  if ((jarak <= 5 ) && (jarak > 0)){
    lcd.setCursor(0,2);
    lcd.print("Status : BAHAYA ");
    delay(1000);
    lcd.clear();
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED3, HIGH);
    tone(buzzer, frekuensi);
  } 
      else if (jarak == 12) {
    lcd.setCursor(0,2);
    lcd.print("Status : AMAN ");
    delay(1000);
    lcd.clear();
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, LOW);
    noTone (buzzer);
   }
      else if ((jarak <= 12) && (jarak >= 6)){
    lcd.setCursor(0,2);
    lcd.print("Status : AWAS ");
    delay(1000);
    lcd.clear();
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED3, LOW);
    noTone (buzzer);
  }
  }
}
void backupp (){
  long t;
  int jarak;
  int jarakAir = 15;
  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  t = pulseIn(echo, HIGH);
  jarak = (t / 2) /29.1;
  int tampilblynk = (jarak - jarakAir)* -1;
  if ((jarak <= 5 ) && (jarak > 0)){
    lcd.setCursor(0,2);
    lcd.print("Status : BAHAYA ");
    lcd.setCursor(0,3);
    lcd.print("Pintu Air Terbuka");
    delay(1000);
    lcd.clear();
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED3, HIGH);
    tone(buzzer, frekuensi);
    relay.write(180);
  } 
      else if (jarak == 12) {
    lcd.setCursor(0,2);
    lcd.print("Status : AMAN ");
    lcd.setCursor(0,3);
    lcd.print("Pintu Air Tertutup");
    delay(1000);
    lcd.clear();
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, LOW);
    noTone (buzzer);
    relay.write(0);
   }
      else if ((jarak <= 12) && (jarak >= 6)){
    lcd.setCursor(0,2);
    lcd.print("Status : AWAS ");
    lcd.setCursor(0,3);
    lcd.print("Pintu Air Siaga");
    delay(1000);
    lcd.clear();
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED3, LOW);
    noTone (buzzer);
  }
} 
void loop() {
  Blynk.run();
  if (WiFi.status()== WL_CONNECTED){
  ultrasonic();
  }
  else if (WiFi.status()== WL_DISCONNECTED){
    lcd.backlight();
    lcd.print(0,3);
    lcd.print("WiFi diconnected");
    delay(500);
    lcd.clear();
  backupp();
  }
  }
}

@andhika_pratama21 Please edit your post, using the pencil icon at the bottom, and remove the Blockquotes that you’ve used then add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Copy and paste these if you can’t find the correct symbol on your keyboard.

Pete.

This is my Code

#define BLYNK_TEMPLATE_ID "TMPLmHgrX3De"
#define BLYNK_DEVICE_NAME "TA"
#include <LiquidCrystal_I2C.h>
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Servo.h>
BlynkTimer timer;
LiquidCrystal_I2C lcd(0x27, 20, 4);
bool pinValue = 0;
int frekuensi=250;
int Mode=0;
int maximal=0;
int minimal=0;
//-----------------------------------------------------------------
//Konfigurasi
// 14 untuk token blynk template
// 15 konfigurasi ke wifi dengan memasukan ssid wifi
// 16 konfigurasi ke wifi dengan memasukan password wifi
char auth[] = "DUgjSPuOAr8tfk7JeB7JoAPEtOnDS-0V";//Enter your Auth token
char ssid[] = "Jangan nyambung ini virus";
char pass[] = "";
// set variabel untuk pewaktu blynk dan set variable untuk motor servo
Servo relay;
>//-----------------------------------------------------------------
// mendefinisikan pin yang digunakan masing masing komponen
// Define the component pins
#define trig 12
#define echo 13
#define LED1 0
#define LED2 2
#define LED3 3
#define buzzer 14
//------------------------------------------------------------------
//Proses pengekesusian
//33 merupakan sebuah fungsi yang akan menjalankan program dan setiap fungsi fungsi yang berada dalam void setup akan dieksekusi lebih dahulu dan akan berjalanan hanya satu kali
void setup() {
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0,0);
  lcd.print("-PINTU AIR OTOMATIS-");
  lcd.setCursor(0,1);
  lcd.print("====================");
  pinMode(trig, OUTPUT);// sebagai awal pembacaan sensor jarak HCSR-04
  pinMode(echo, INPUT);// sebagai awal pembacaan sensor jarak HCSR-04
  pinMode(LED1, OUTPUT);// sebagai perintah untuk melakukan setting LED 1 ke mode OUTPUT
  pinMode(LED2, OUTPUT);// sebagai perintah untuk melakukan setting LED 2 ke mode OUTPUT
  pinMode(LED3, OUTPUT);// sebagai perintah untuk melakukan setting LED 3 ke mode OUTPUT
  pinMode(buzzer, OUTPUT);// sebagai perintah untuk melakukan setting Buzzer ke mode OUTPUT
  relay.attach(15);
  relay.write(0);
  Blynk.begin(auth, ssid, pass);
  lcd.setCursor(0,2);
  lcd.print(ssid);
  delay(1000);
  lcd.clear();
  while (WiFi.status() != WL_CONNECTED)
  {
    lcd.backlight();
    lcd.print(0,3);
    lcd.print("WiFi connected");
    delay(500);
    lcd.clear();
  }
  delay(1000);
  lcd.clear();
  timer.setInterval(1000L, ultrasonic);
}
  BLYNK_WRITE(V1) {
 pinValue = param.asInt();
 if ((Mode == 0 ) && (pinValue == 1)){
 relay.write(180);
 lcd.setCursor(0,0);
 lcd.print("[PINTU AIR MANUAL]");
 lcd.setCursor(0,1);
 lcd.print("====================");
 lcd.setCursor(0,2);
 lcd.print("Status : AMAN");
 lcd.setCursor(0,3);
 lcd.print("Pintu Air Terbuka");
 delay(1500);
 lcd.clear();
}
 else if ((Mode == 0 ) && (pinValue == 0)){
 relay.write(0);
  lcd.setCursor(0,0);
  lcd.print("[PINTU AIR MANUAL]");
  lcd.setCursor(0,1);
  lcd.print("====================");
  lcd.setCursor(0,2);
  lcd.print("Status : AMAN");
  lcd.setCursor(0,3);
  lcd.print("Pintu Air Tertutup");
  delay(1500);
  lcd.clear();
}
}
BLYNK_WRITE(V2){
  pinValue = param.asInt();
  Mode=pinValue;
}
BLYNK_WRITE(V3)
{
  maximal = param.asInt();
}
BLYNK_WRITE(V4){
  minimal = param.asInt();
}
BLYNK_CONNECTED(){
  Blynk.syncAll();
}
void ultrasonic() {
  long t;
  int jarak=0;
  int jarakAir = 15;
  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  t = pulseIn(echo, HIGH);
  jarak = (t / 2) /29.1;
  int tampilblynk = (jarak - jarakAir)* -1;
  if(Mode==1){
  if (jarak <= jarakAir) {
  Blynk.virtualWrite(V0, tampilblynk);
  }
  else{
  Blynk.virtualWrite(V0, 0);
  }
  lcd.setCursor(0,0);
  lcd.print("[PINTU AIR OTOMATIS]");
  lcd.setCursor(0,1);
  lcd.print("====================");
  if (jarak == maximal && jarak < minimal){
    lcd.setCursor(0,2);
    lcd.print("Status : BAHAYA ");
    lcd.setCursor(0,3);
    lcd.print("Pintu Air Terbuka");
    delay(1000);
    lcd.clear();
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED3, HIGH);
    tone(buzzer, frekuensi);
    relay.write(180);
  } 
      else if (jarak == minimal) {
    lcd.setCursor(0,2);
    lcd.print("Status : AMAN ");
    lcd.setCursor(0,3);
    lcd.print("Pintu Air Tertutup");
    delay(1000);
    lcd.clear();
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, LOW);
    noTone (buzzer);
    relay.write(0);
   }
  }
else if(Mode==0){
  if (jarak <= jarakAir) {
  Blynk.virtualWrite(V0, tampilblynk);
  }
  else{
  Blynk.virtualWrite(V0, 0);
  }
  lcd.setCursor(0,0);
  lcd.print("[PINTU AIR MANUAL]");
  lcd.setCursor(0,1);
  lcd.print("====================");
  if ((jarak <= 5 ) && (jarak > 0)){
    lcd.setCursor(0,2);
    lcd.print("Status : BAHAYA ");
    delay(1000);
    lcd.clear();
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED3, HIGH);
    tone(buzzer, frekuensi);
  } 
      else if (jarak == 12) {
    lcd.setCursor(0,2);
    lcd.print("Status : AMAN ");
    delay(1000);
    lcd.clear();
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, LOW);
    noTone (buzzer);
   }
      else if ((jarak <= 12) && (jarak >= 6)){
    lcd.setCursor(0,2);
    lcd.print("Status : AWAS ");
    delay(1000);
    lcd.clear();
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED3, LOW);
    noTone (buzzer);
  }
  }
}
void backupp (){
  long t;
  int jarak;
  int jarakAir = 15;
  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  t = pulseIn(echo, HIGH);
  jarak = (t / 2) /29.1;
  int tampilblynk = (jarak - jarakAir)* -1;
  if ((jarak <= 5 ) && (jarak > 0)){
    lcd.setCursor(0,2);
    lcd.print("Status : BAHAYA ");
    lcd.setCursor(0,3);
    lcd.print("Pintu Air Terbuka");
    delay(1000);
    lcd.clear();
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED3, HIGH);
    tone(buzzer, frekuensi);
    relay.write(180);
  } 
      else if (jarak == 12) {
    lcd.setCursor(0,2);
    lcd.print("Status : AMAN ");
    lcd.setCursor(0,3);
    lcd.print("Pintu Air Tertutup");
    delay(1000);
    lcd.clear();
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, LOW);
    noTone (buzzer);
    relay.write(0);
   }
      else if ((jarak <= 12) && (jarak >= 6)){
    lcd.setCursor(0,2);
    lcd.print("Status : AWAS ");
    lcd.setCursor(0,3);
    lcd.print("Pintu Air Siaga");
    delay(1000);
    lcd.clear();
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED3, LOW);
    noTone (buzzer);
  }
} 
void loop() {
  Blynk.run();
  if (WiFi.status()== WL_CONNECTED){
  ultrasonic();
  }
  else if (WiFi.status()== WL_DISCONNECTED){
    lcd.backlight();
    lcd.print(0,3);
    lcd.print("WiFi diconnected");
    delay(500);
    lcd.clear();
  backupp();
  }
  }
}

Why have you posted two sketches? Are they the same?

You can’t use delays like this with Blynk if you want reliable results.
You should read this…

https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

Pete.