BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Blynk Goes Offline and can't reconnect

i’m in trouble with my project. my blynk offline randomly. it usually happens when the relay changed conditions from LOW to HIGH. but sometimes before relay changed conditions, my blynk suddenly offline too. i’m trying to use reconnect code but its not working. i don’t know what happens
This is the situation…

Arduino Mega 2560
ESP8266-01 WiFi Shield 
1 x DHT21 sensor to measure humidity
1 x DS18B20 sensor
1 x Capacitive Soil Moisture sensor
4 x 5V relays 
L293D motor driver to drive 12VDC Motor

for my esp i used 3.3v adaptor 1A and my arduino connect to my laptop. i used blynk server

How can i solve ?
Thank You


#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <DallasTemperature.h>
#include <OneWire.h>
#include <DHT.h>

#define DHTPIN 6
#define DHTTYPE DHT21
#define relayheater 4
#define pengaduk 3
#define IN_3 24
#define IN_4 25
#define relay 5
#define PL1 30
#define PL2 31
const int pb1 = 35; 
int persen, sensorsoil, suhu, outputvalue, lim1, suhudimmer, h;
unsigned long interval=2*60000;
unsigned long previousMillis=0;

char auth[] = "zsVnSQHmphEPtsjltOkuVE7x0kLDfN0y";
char ssid[] = "SamsungJ2Prime";
char pass[] = "12345678";
char server[] = "192.168.1.100";  // URL for Blynk Cloud Server
int port = 8080;

#define EspSerial Serial1
//#include <SoftwareSerial.h>
//SoftwareSerial EspSerial(2, 3); // RX, TX
#define ESP8266_BAUD 115200
ESP8266 wifi(&EspSerial);
#define ONE_WIRE_BUS 7

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);// Pass the oneWire reference to Dallas Temperature.
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
WidgetLCD lcd(V14);

int buttonState1=0;
int ReCnctFlag;  // Reconnection Flag
int ReCnctCount = 0;  // Reconnection counter


void setup() {
Serial.begin(115200);
EspSerial.begin(ESP8266_BAUD);
delay(10);  
  pinMode(pb1, INPUT_PULLUP);
  pinMode(relay, OUTPUT);
  pinMode(relayheater, OUTPUT);
  pinMode(pengaduk, OUTPUT);
  pinMode(PL1, OUTPUT);
  pinMode(PL2, OUTPUT);
  pinMode(IN_3, OUTPUT);
  pinMode(IN_4, OUTPUT);
  digitalWrite(pb1, HIGH);
  digitalWrite(PL1, HIGH);
  digitalWrite(PL2, HIGH);
  digitalWrite(relay, HIGH);
  digitalWrite(relayheater, HIGH);
  wifi.setDHCP(1, 1, 1);
  Blynk.config(wifi, auth, server, port);
    if (Blynk.connectWiFi(ssid, pass)) {
      Blynk.connect();
    }
  sensors.begin();
  dht.begin();
  timer.setInterval(2000L, heater);
  }

void loop(){
  timer.run();
  if (Blynk.connected()) {  // If connected run as normal
    Blynk.run();
  } else if (ReCnctFlag == 0) {  // If NOT connected and not already tring to reconnect, set timer to try to reconnect in 60 seconds
    ReCnctFlag = 1;  // Set reconnection Flag
    Serial.println("Starting reconnection timer in 30 seconds...");
    timer.setTimeout(30000L, []() {  // Lambda Reconnection Timer Function
      ReCnctFlag = 0;  // Reset reconnection Flag
      ReCnctCount++;  // Increment reconnection Counter
      Serial.print("Attempting reconnection #");
      Serial.println(ReCnctCount);
      wifi.setDHCP(1, 1, 1); //Enable dhcp in station mode and save in flash of esp8266
      Blynk.config(wifi, auth, server, port);
      Blynk.connect();  // Try to reconnect to the server
      if (Blynk.connectWiFi(ssid, pass)) {
        Blynk.connect();
      }
    });
  }
}
void heater() {
lcd.print(1, 0, "Alat READY");
buttonState1 = digitalRead(pb1);
digitalWrite(PL2, LOW);
digitalWrite(PL1, LOW);
lcd.clear();
while (buttonState1 == 0){
  sensorsoil = analogRead(A0); 
   float persen=-0.04*sensorsoil+32.5;   // konversi ke persen 
  float h = dht.readHumidity();
  sensors.requestTemperatures();
  float suhu= (sensors.getTempCByIndex(0));
    suhudimmer = suhu;
  outputvalue = map(h, 0, 100, 100, 0);
  Blynk.virtualWrite(V1, suhu);
  Blynk.virtualWrite(V9, outputvalue);
  Blynk.virtualWrite(V8, persen);
  Blynk.virtualWrite(V7, h);
  digitalWrite(PL2, HIGH);
  digitalWrite(PL1, LOW);
  unsigned long currentMillis = millis();
    if ((unsigned long)(currentMillis - previousMillis) <= interval)
    {
     digitalWrite(relayheater, LOW);
     lcd.print(3, 0, "PENGERINGAN");
     lcd.print(1, 1, "Heater Menyala");
           
    }
    else {
      if(suhudimmer < 50){
        digitalWrite(relay, LOW);
        digitalWrite(relayheater, LOW);
        lcd.print(3, 0, "PENGERINGAN");
        lcd.print(0, 1, "HeaterBlower ON");      
        }
      else if (suhudimmer >= 50){
        digitalWrite(relay, HIGH);
        digitalWrite(relayheater, HIGH);
        lcd.print(3, 0, "PENGERINGAN");
        lcd.print(0, 1, "HeaterBlower OFF");
        }
      digitalWrite(IN_3, HIGH);
      digitalWrite(IN_4, LOW);
      analogWrite(pengaduk, outputvalue);
 
    }
  }
}