Esp8266 goes offline

Hello.
I use simple scetch for control water valve. Sometimes Esp8266 goes offline. I must switch off and then switch on ESP8266 for online. What could be the reason of disconnection?

ESP8266 5V WiFi relay module (ESP-01)
Android
Blynk server
Blynk Library 0.4.10

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <TimeLib.h>
#include <WidgetRTC.h>

#define BLYNK_PRINT Serial

bool rele;
int current_month;
long startTimeInSecs;

char auth[] = "6fab85009a914a1c84b9da5247fb46d1";
char ssid[] = "Lugovaya 1-1-48";
char pass[] = "1111111111";

BlynkTimer timer_Connect;
WidgetRTC rtc;

BLYNK_WRITE(V1) {
 startTimeInSecs = param[0].asLong();
}

BLYNK_WRITE(V0)
{
 rele=param.asInt();
 toggleRelay(rele);

}

BLYNK_CONNECTED() {
 Blynk.syncAll();
 rtc.begin();
}

void reconnectBlynk() {
 if (!Blynk.connected()) {
   if (Blynk.connect()) {
     BLYNK_LOG("Reconnected");      
   } else {
     BLYNK_LOG("Not reconnected");      
   }
 }
}

void toggleRelay(bool relayState)
{
   if (relayState)
   {
       const byte miBufferON[] = {0xA0, 0x01, 0x01, 0xA2};
       Serial.write(miBufferON, sizeof(miBufferON));
       // val = 1;
   }
   else
   {
       //To disable the Relay send it by serial port:
       const byte miBufferOFF[] = {0xA0, 0x01, 0x00, 0xA1};
       Serial.write(miBufferOFF, sizeof(miBufferOFF));
       // val = 0;
   }
}

void setup()
{
 current_month = 1;
 Serial.begin(9600);
 timer_Connect.setInterval(30000L, reconnectBlynk);  // проверяем каждые 30 секунд, если все еще подключен к серверу 
 
//  Blynk.begin(auth, ssid, pass);
 Blynk.config(auth); // поэтому конфигурируем соединение
 Blynk.disconnect(); //разорвать соединение
 Blynk.connect();  // а потом пробуем уже конектиться
 setSyncInterval(10 * 60); // Sync interval in seconds (10 minutes)
}

void loop()
{

timer_Connect.run();

//  Blynk.run();
if (Blynk.connected()) 
{
   Blynk.run();
   
}

 if(current_month != month())
 { 
     if((3600*hour()+60*minute()) >= startTimeInSecs)
     {  
         toggleRelay(1);
         delay(5000);
         toggleRelay(0);
         current_month = month();
     }
 }
}

Hmmmm, I wonder what could possibly causing that!

Pete.

Hello.

Shoud I remove timer_Connect.run() from void loop() too? I mean this instruction like Blynk.Run().

Igor

@Igor_Chaus:

Under “Limitations and Recommendations” the documentation clearly says:
Avoid using long delays with delay() – it may cause connection breaks.

You put this …

  delay(5000);

… in your loop() and wonder what is going wrong? :wink: