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

NodeMCU drop Blynk connection at 2:01 AM

#1

Hi,

I’m working on a full enviroment control system in a grow tent with a Blynk controller.
The setup is easy:
NodeMcu v3, BME280, DS18B20, 16x2 LCD display, 4 relay board.
I’m still in the testing phase, but i have a strange issue. At 2:01 AM not everyday but every second-third day my Blynk application says my NodeMcu is offline. But if i check my router, NodeMcu is online, even if i ping it every package is arriving, but my Blynk application doesn’t see the hardware.
If u power up the NodeMcu again, my Blynk application see the hardware again.

// V1 - Slider - Hőmérséklet állító 10-30
// V2 - Slider - Para állító 30-70

// V3 - Páratartaloma
// V4 - Hőmérséklet
// V5 - Légnyomás

// V6 - Lampa visszajelzo
// V7 - Hősugárzó visszajelzo
// V8 - Keringető visszajelzo
// V9 - Parasito visszajelző
// V10 - Idő megadás
// V11 - Föld hőmérséklet

// D8 - Keringeto
// D7 - Parasito
// D6 - Hosugarzo
// D5 - Lampa

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>


BlynkTimer timer;
WidgetRTC rtc;
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7);


#define Lampa V6
#define Hosugarzo V7
#define Hosugarzo_min V12    
#define Hosugarzo_max V13
#define Keringeto V8
#define Parasito V9
#define Parasito_min V14
#define Parasito_max V15

#define ONE_WIRE_BUS 17
#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BME280 bme;
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

char auth[] = "";
char ssid[] = "";
char pass[] = "";
long kezdomp;
long stopmp;
long aktualismp;

int Para_min_limit;
int Para_max_limit;
int Para_kikapcs_limit;

int Hofok_min_limit;
int Hofok_max_limit;

const int Lampa_rele = 14;
const int Hosugarzo_rele = 12;
const int Parasito_rele = 13;
const int Keringeto_rele = 15;

void setup()
{
  pinMode(Lampa_rele, OUTPUT);
  pinMode(Hosugarzo_rele, OUTPUT);
  pinMode(Parasito_rele, OUTPUT);
  pinMode(Keringeto_rele, OUTPUT);

  digitalWrite(Lampa_rele, HIGH);
  digitalWrite(Hosugarzo_rele, HIGH);
  digitalWrite(Parasito_rele, HIGH);
  digitalWrite(Keringeto_rele, HIGH);
  
  lcd.setBacklightPin(3,POSITIVE);
  lcd.setBacklight(HIGH);
   
  lcd.home ();
  lcd.begin(16,2);

  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  
  int mytimeout = millis() / 1000;
  while (Blynk.connect() == false) 
  {
    if((millis() / 1000) > mytimeout + 8)
    {
       break;
    }
  }
  rtc.begin();
  sensors.begin();
  
  timer.setInterval(30000L, CheckConnection);
  timer.setInterval(10000L, LampaVezerles);
  timer.setInterval(1000L, KornyezetiVezerles);
  bool status;
  status = bme.begin();  
}


BLYNK_CONNECTED() 
{
  Blynk.syncAll();
}

  BLYNK_WRITE(V1) 
{
  int pinValue = param.asInt();
  Hofok_min_limit  = pinValue * 0.9;
  Blynk.virtualWrite(Hosugarzo_min, Hofok_min_limit);
  Hofok_max_limit = pinValue * 1.1;
  Blynk.virtualWrite(Hosugarzo_max, Hofok_max_limit);
  Serial.println("Hofok:   ");
  Serial.println(pinValue);
}

  BLYNK_WRITE(V2) 
{
  int pinValue = param.asInt();
  Para_min_limit  = pinValue * 0.8;
  Blynk.virtualWrite(Parasito_min, Para_min_limit);
  Para_max_limit = pinValue * 1.3;
  Blynk.virtualWrite(Parasito_max, Para_max_limit);
  Para_kikapcs_limit = pinValue * 1.4;
  Serial.println("Para:   ");
  Serial.println(pinValue);
}


void LampaVezerles() 
{ 
Blynk.syncVirtual(V10);
}

BLYNK_WRITE(V10)    
{   
    TimeInputParam t(param);
    aktualismp = ((hour() * 3600) + (minute() * 60) + second());
    kezdomp = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);
    Serial.print(aktualismp);
    Serial.print(" - ");
    Serial.println(kezdomp);
     if(aktualismp >= kezdomp)
     {  
      if(aktualismp <= kezdomp + 90)
        {
        digitalWrite(Lampa_rele, LOW); 
        Blynk.virtualWrite(Lampa, 1);
        Serial.print("Lampa felkapcsolva");
        Serial.println();
        lcd.setCursor(12,1);
        lcd.print("F");
        }      
      }
    else
    {  
    }
    stopmp = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
    if(aktualismp >= stopmp)
    {
      digitalWrite(Lampa_rele, HIGH); 
      Blynk.virtualWrite(Lampa, 0);
      Serial.print("Lampa lekapcsolva");
      Serial.println();
      lcd.setCursor(12,1);
      lcd.print("L");
      if(aktualismp <= stopmp + 90)
      {   
        digitalWrite(Lampa_rele, HIGH);
        Blynk.virtualWrite(Lampa, 0);
        Serial.print("Lampa lekapcsolva");
        Serial.println();
        lcd.setCursor(12,1);
        lcd.print("L");
      }              
    }
    else{
      if(aktualismp >= kezdomp)
            {  
        digitalWrite(Lampa_rele, LOW); 
        Blynk.virtualWrite(Lampa, 1);
        Serial.print("Lampa felkapcsolva");
        Serial.println();
        lcd.setCursor(12,1);
        lcd.print("F");
            }          
        }
}
        
void KornyezetiVezerles()
{
  float h = bme.readHumidity();
  float t = bme.readTemperature();
  float p = bme.readPressure() / 100.0F;
  sensors.requestTemperatures(); 
  int s = sensors.getTempCByIndex(0); 

  if (isnan(h) || isnan(t) || isnan(p))
  {
    Serial.println("Nincs BME csatlakoztatva !");
    return;
  }  
  Blynk.virtualWrite(V3, h);
  Blynk.virtualWrite(V4, t);
  Blynk.virtualWrite(V5, p);
  Blynk.virtualWrite(V11, s);
  
  
  Serial.print("Para: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print(" - Hofok: ");
  Serial.print(t);
  Serial.print(" *C");
  Serial.print(" - Legnyomas: ");
  Serial.print(p);
  Serial.print(" mbar");
  Serial.print(" - Fold hofok: ");
  Serial.print(s);
  Serial.println(" *C");
  
  lcd.print(h);
  lcd.print("% ");
  lcd.setCursor(0,1);
  lcd.print(t);
  lcd.print("*C ");
  
      if(t <= Hofok_min_limit){
        digitalWrite(Hosugarzo_rele, LOW); 
        Blynk.virtualWrite(Hosugarzo, 1);
        Serial.print("Hosugarzo felkapcsolva");
        Serial.println();   
    }
    else{  

    }
    if(t >= Hofok_max_limit)
    {
      digitalWrite(Hosugarzo_rele, HIGH); 
      Blynk.virtualWrite(Hosugarzo, 0);
      Serial.print("Hosugarzo lekapcsolva");
      Serial.println();             
    }
    else{   
         }

      if(h <= Para_min_limit){
        digitalWrite(Keringeto_rele, HIGH); 
        digitalWrite(Parasito_rele, LOW);
        Blynk.virtualWrite(Keringeto, 0);
        Blynk.virtualWrite(Parasito, 1);
        Serial.println("Keringeto lekapcsolva");
        Serial.println("Parasito felkapcsolva");
        Serial.println();     
    }
    else{  
        digitalWrite(Keringeto_rele, LOW);
        Blynk.virtualWrite(Keringeto, 1);
        Serial.println("Keringeto felkapcsolva"); 
    }
    if(h >= Para_max_limit)
    {
      digitalWrite(Keringeto_rele, LOW);
      digitalWrite(Parasito_rele, HIGH);
      Blynk.virtualWrite(Keringeto, 1);
      Blynk.virtualWrite(Parasito, 0);
      Serial.println("Keringeto felkapcsolva");
      Serial.println("Parasito lekapcsolva");
      Serial.println();              
    }
    else{         
         }
}

void CheckConnection(){
  if(!Blynk.connected()){
    Serial.println("Nincs felcsatlakozva a Blynk szerverre"); 
    Blynk.connect();
  }
  else{
    Serial.println("Csatlakoztatva a Blynk szerverre");     
  }
}

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

I have a plus void for checking connection:

void CheckConnection(){
  if(!Blynk.connected()){
    Serial.println("Nincs felcsatlakozva a Blynk szerverre"); 
    Blynk.connect();
  }
  else{
    Serial.println("Csatlakoztatva a Blynk szerverre");     
  }
}

But no seccess. Anybody a solution?

0 Likes

#2

You will need to set up some form of in-sketch debugging/tracking (Serial prints perhaps?) to monitor the state of various variables and possibly millis() value and compare that with your “disconnection” time to see if there is any patterns. We can’t do that here.

Your timers are overlapping every 10 and 30 seconds (AKA running two or three at the same time each time they coincide)… perhaps something they do causes a reaction over time?? Look at staggering the timers to non-coinciding timeframes.

You are also syncing your Time Input Widget function, Why? The idea of it is set and forget until set again. As the sketch should use that widgets function to take the parameters and assign them to global variables that your sketch continuously uses as needed in other timed functions.

And finally, you are using Blynk.begin(), which is a blocking command, so if something goes wrong it may stop your entire sketch from running. Look at Blynk.config()

Check this example (and search around this forum) for ways of keeping your sketch running and running proper reconnection routines if disconnections happen.

0 Likes

#3

Looks better?

// V1 - Slider - Hőmérséklet állító 10-30
// V2 - Slider - Para állító 30-70

// V3 - Páratartaloma
// V4 - Hőmérséklet
// V5 - Légnyomás

// V6 - Lampa visszajelzo
// V7 - Hősugárzó visszajelzo
// V8 - Keringető visszajelzo
// V9 - Parasito visszajelző
// V10 - Idő megadás
// V11 - Föld hőmérséklet

// D8 - Keringeto
// D7 - Parasito
// D6 - Hosugarzo
// D5 - Lampa

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>


BlynkTimer timer;
WidgetRTC rtc;
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7);


#define Lampa V6
#define Hosugarzo V7
#define Hosugarzo_min V12    
#define Hosugarzo_max V13
#define Keringeto V8
#define Parasito V9
#define Parasito_min V14
#define Parasito_max V15

#define ONE_WIRE_BUS 17
#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BME280 bme;
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

char auth[] = "";
char ssid[] = "";
char pass[] = "";
char server[] = "blynk-cloud.com";

int port = 8080;
long kezdomp;
long stopmp;
long aktualismp;

int Para_min_limit;
int Para_max_limit;
int Para_kikapcs_limit;

int Hofok_min_limit;
int Hofok_max_limit;

const int Lampa_rele = 14;
const int Hosugarzo_rele = 12;
const int Parasito_rele = 13;
const int Keringeto_rele = 15;

int ReCnctFlag;
int ReCnctCount = 0;

String displaycurrenttimepluswifi;
String Ho;
String Pa;
int wifisignal;

void setup()
{
  pinMode(Lampa_rele, OUTPUT);
  pinMode(Hosugarzo_rele, OUTPUT);
  pinMode(Parasito_rele, OUTPUT);
  pinMode(Keringeto_rele, OUTPUT);

  digitalWrite(Lampa_rele, HIGH);
  digitalWrite(Hosugarzo_rele, HIGH);
  digitalWrite(Parasito_rele, HIGH);
  digitalWrite(Keringeto_rele, HIGH);
  
  lcd.setBacklightPin(3,POSITIVE);
  lcd.setBacklight(HIGH);
   
  lcd.home ();
  lcd.begin(16,2);

  Serial.begin(9600);
  WiFi.begin(ssid, pass);
  Blynk.config(auth, server, port);
  Blynk.connect();
  
  int mytimeout = millis() / 1000;
  while (Blynk.connect() == false) 
  {
    if((millis() / 1000) > mytimeout + 8)
    {
       break;
    }
  }
  rtc.begin();
  sensors.begin();
  
  timer.setInterval(10020L, LampaVezerles);
  timer.setInterval(1000L, KornyezetiVezerles);
  timer.setInterval(5015L, clockvalue);  // check value for time
  timer.setInterval(5030L, sendWifi);    // Wi-Fi singal
  bool status;
  status = bme.begin();  
}


BLYNK_CONNECTED() 
{
  Blynk.syncAll();
}

  BLYNK_WRITE(V1) 
{
  int pinValue = param.asInt();
  Hofok_min_limit  = pinValue * 0.9;
  Blynk.virtualWrite(Hosugarzo_min, Hofok_min_limit);
  Hofok_max_limit = pinValue * 1.1;
  Blynk.virtualWrite(Hosugarzo_max, Hofok_max_limit);
  Serial.println("Hofok:   ");
  Serial.println(pinValue);
}

  BLYNK_WRITE(V2) 
{
  int pinValue = param.asInt();
  Para_min_limit  = pinValue * 0.8;
  Blynk.virtualWrite(Parasito_min, Para_min_limit);
  Para_max_limit = pinValue * 1.3;
  Blynk.virtualWrite(Parasito_max, Para_max_limit);
  Para_kikapcs_limit = pinValue * 1.4;
  Serial.println("Para:   ");
  Serial.println(pinValue);
}


BLYNK_WRITE(V10)    
{   
    TimeInputParam t(param);
    kezdomp = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);
    stopmp = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
}


void sendWifi() {
  wifisignal = map(WiFi.RSSI(), -105, -40, 0, 100);
}

void clockvalue() // Digital clock display of the time
{

 int gmthour = hour();
  if (gmthour == 24){
     gmthour = 0;
  }
  String displayhour =   String(gmthour, DEC);
  int hourdigits = displayhour.length();
  if(hourdigits == 1){
    displayhour = "0" + displayhour;
  }
  String displayminute = String(minute(), DEC);
  int minutedigits = displayminute.length();  
  if(minutedigits == 1){
    displayminute = "0" + displayminute;
  }  

  displaycurrenttimepluswifi = "Ido:  " + displayhour + ":" + displayminute + "           Terero:  " + wifisignal +" %";
  Blynk.setProperty(V10, "label", displaycurrenttimepluswifi);
}

void LampaVezerles() 
{
    aktualismp = ((hour() * 3600) + (minute() * 60) + second());
    Serial.print("Aktualis: ");
    Serial.print(aktualismp);
    Serial.print(" - ");
    Serial.print("Kezdo: ");
    Serial.print(kezdomp);
    Serial.print(" - ");
    Serial.print("Stop: ");
    Serial.print(stopmp);
    Serial.println();
     if(aktualismp >= kezdomp)
     {  
      if(aktualismp <= kezdomp + 90)
        {
        digitalWrite(Lampa_rele, LOW); 
        Blynk.virtualWrite(Lampa, 1);
        Serial.print("Lampa felkapcsolva");
        Serial.println();
        lcd.setCursor(12,1);
        lcd.print("F");
        }      
      }
    else
    {  
    }

    if(aktualismp >= stopmp)
    {
      digitalWrite(Lampa_rele, HIGH); 
      Blynk.virtualWrite(Lampa, 0);
      Serial.print("Lampa lekapcsolva");
      Serial.println();
      lcd.setCursor(12,1);
      lcd.print("L");
      if(aktualismp <= stopmp + 90)
      {   
        digitalWrite(Lampa_rele, HIGH);
        Blynk.virtualWrite(Lampa, 0);
        Serial.print("Lampa lekapcsolva");
        Serial.println();
        lcd.setCursor(12,1);
        lcd.print("L");
      }              
    }
    else{
      if(aktualismp >= kezdomp)
            {  
        digitalWrite(Lampa_rele, LOW); 
        Blynk.virtualWrite(Lampa, 1);
        Serial.print("Lampa felkapcsolva");
        Serial.println();
        lcd.setCursor(12,1);
        lcd.print("F");
            }          
        }
}
        
void KornyezetiVezerles()
{
  float h = bme.readHumidity();
  float t = bme.readTemperature();
  float p = bme.readPressure() / 100.0F;
  sensors.requestTemperatures(); 
  int s = sensors.getTempCByIndex(0); 

  if (isnan(h) || isnan(t) || isnan(p))
  {
    Serial.println("Nincs BME csatlakoztatva !");
    return;
  }  
  Blynk.virtualWrite(V3, h);
  Blynk.virtualWrite(V4, t);
  Blynk.virtualWrite(V5, p);
  Blynk.virtualWrite(V11, s);


  String displayt = String(t);
  String displayh = String(h); 
  Ho = "Homerseklet:  " + displayt + " °C";
  Pa = "Paratartalom:  " + displayh +" %";
  Blynk.setProperty(V1, "label", Ho);
  Blynk.setProperty(V2, "label", Pa);
  



  
  
  
  Serial.print("Para: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print(" - Hofok: ");
  Serial.print(t);
  Serial.print(" *C");
  Serial.print(" - Legnyomas: ");
  Serial.print(p);
  Serial.print(" mbar");
  Serial.print(" - Fold hofok: ");
  Serial.print(s);
  Serial.println(" *C");
  
  lcd.print(h);
  lcd.print("% ");
  lcd.setCursor(0,1);
  lcd.print(t);
  lcd.print("*C ");
  
      if(t <= Hofok_min_limit){
        digitalWrite(Hosugarzo_rele, LOW); 
        Blynk.virtualWrite(Hosugarzo, 1);
        Serial.print("Hosugarzo felkapcsolva");
        Serial.println();   
    }
    else{  

    }
    if(t >= Hofok_max_limit)
    {
      digitalWrite(Hosugarzo_rele, HIGH); 
      Blynk.virtualWrite(Hosugarzo, 0);
      Serial.print("Hosugarzo lekapcsolva");
      Serial.println();             
    }
    else{   
         }

      if(h <= Para_min_limit){
        digitalWrite(Keringeto_rele, HIGH); 
        digitalWrite(Parasito_rele, LOW);
        Blynk.virtualWrite(Keringeto, 0);
        Blynk.virtualWrite(Parasito, 1);
        Serial.println("Keringeto lekapcsolva");
        Serial.println("Parasito felkapcsolva");
        Serial.println();     
    }
    else{  
        digitalWrite(Keringeto_rele, LOW);
        Blynk.virtualWrite(Keringeto, 1);
        Serial.println("Keringeto felkapcsolva"); 
    }
    if(h >= Para_max_limit)
    {
      digitalWrite(Keringeto_rele, LOW);
      digitalWrite(Parasito_rele, HIGH);
      Blynk.virtualWrite(Keringeto, 1);
      Blynk.virtualWrite(Parasito, 0);
      Serial.println("Keringeto felkapcsolva");
      Serial.println("Parasito lekapcsolva");
      Serial.println();              
    }
    else{         
         }
}

void loop()
{
  timer.run();
  
  if (Blynk.connected()) {
    Blynk.run();
  } else if (ReCnctFlag == 0) {
    ReCnctFlag = 1;
    Serial.println("Csatlakozasi ido, 30mp...");
    timer.setTimeout(30000L, []() {
      ReCnctFlag = 0;
      ReCnctCount++; 
      Serial.print("Csatlakozási kiserlet");
      Serial.println(ReCnctCount);
      Blynk.connect();
    });
  }
}
0 Likes