Blynk reconnection fail

Hi there,
I have a reconnection issue. Every time connection to the router fails for whatever reason, Blynk cannot reconnect, I have to manually cut off power and then restart it.

I have other devices on the same network, no issues there, code is pretty similar. I suspect the reason can be related to timer in the loop, which is only present in the problematic device sketch. I also found on the forum many issues related to timer and failed reconnection, but couldn’t associate to my case - maybe I miss in the loop " if(Blynk.connected()" but not really sure.

Here is the code:


//#define BLYNK_PRINT Serial
//#include <TimeLib.h>

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include "DHTesp.h"
#include <SimpleTimer.h>
#include <ArduinoOTA.h>

SimpleTimer timer;

char auth[] = "BLABLA";
char ssid[] = "BLABLA";
char pass[] = "BLABLA";

unsigned long lastMillis = 0;

//Pompe Peristaltiche
uint8_t led1 = 05;
uint8_t led2 = 18;
uint8_t led3 = 19 ;
uint8_t led4 = 21;
uint8_t led5 = 03;
uint8_t led6 = 23;

#define echoPin1 4
#define trigPin1 2
#define MOIST_PIN 36
#define CHECK_PIN 39
#define DHT22_PIN 15

#define V_MOIST_PIN V1
#define V_HUMID_PIN V2
#define V_TEMP_PIN V3
#define V_WATER1_PIN V4
#define V_CHECK_PIN V6

//#define V40 14;
//#define V41 25;
//#define V42 26;
//#define V43 27;
//#define V44 33;
//#define V45 35;


DHTesp dht;
float humidity, temp_c;

void setup()
{

  
ledcAttachPin(led1, 1); // assign RGB led pins to channels
ledcAttachPin(led2, 2);
ledcAttachPin(led3, 3);
ledcAttachPin(led4, 4);
ledcAttachPin(led5, 5);
ledcAttachPin(led6, 6);
ledcSetup(1, 2000, 12); // 12 kHz PWM, 12-bit resolution
ledcSetup(2, 2000, 12);
ledcSetup(3, 2000, 12); 
ledcSetup(4, 2000, 12);
ledcSetup(5, 2000, 12); 
ledcSetup(6, 2000, 12); 

// Chiudo tutti gli interruttori all'avvio (così magari casa non si allega ogni volta :))
ledcWrite(1, 4095);
ledcWrite(2, 4095);
ledcWrite(3, 4095);
ledcWrite(4, 4095);
ledcWrite(5, 4095);
ledcWrite(6, 4095);

digitalWrite(14, HIGH);
digitalWrite(25, HIGH);
digitalWrite(26, HIGH);
digitalWrite(27, HIGH);
digitalWrite(33, HIGH);
digitalWrite(35, HIGH);

  //Serial.begin(9600);
  
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  
  dht.setup(15, DHTesp::DHT22);
  
  Blynk.begin(auth, ssid, pass);
  Blynk.syncAll();
  ArduinoOTA.setHostname("MULTISENSOR");
  ArduinoOTA.begin();
    
  timer.setInterval(1000L, IRRIGAZIONE);
  timer.setInterval(1000L, SENSORI);
  
}

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

Any suggestion? Thanks a lot

It’s difficult to say unless you post your entire sketch, including the functions that are called by your timers.

Pete.

Wow Peter faster than light, thanks!

Here’s the full code, I didn’t want to spam:

//#define BLYNK_PRINT Serial
//#include <TimeLib.h>

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include "DHTesp.h"
#include <SimpleTimer.h>
#include <ArduinoOTA.h>

SimpleTimer timer;

char auth[] = "BLABLA";
char ssid[] = "BLABLA";
char pass[] = "BLABLA";

unsigned long lastMillis = 0;

//Pompe Peristaltiche
uint8_t led1 = 05;
uint8_t led2 = 18;
uint8_t led3 = 19 ;
uint8_t led4 = 21;
uint8_t led5 = 03;
uint8_t led6 = 23;

#define echoPin1 4
#define trigPin1 2
#define MOIST_PIN 36
#define CHECK_PIN 39
#define DHT22_PIN 15

#define V_MOIST_PIN V1
#define V_HUMID_PIN V2
#define V_TEMP_PIN V3
#define V_WATER1_PIN V4
#define V_CHECK_PIN V6

//#define V40 14;
//#define V41 25;
//#define V42 26;
//#define V43 27;
//#define V44 33;
//#define V45 35;


DHTesp dht;
float humidity, temp_c;

void setup()
{

  
ledcAttachPin(led1, 1); // assign RGB led pins to channels
ledcAttachPin(led2, 2);
ledcAttachPin(led3, 3);
ledcAttachPin(led4, 4);
ledcAttachPin(led5, 5);
ledcAttachPin(led6, 6);
ledcSetup(1, 2000, 12); // 12 kHz PWM, 12-bit resolution
ledcSetup(2, 2000, 12);
ledcSetup(3, 2000, 12); 
ledcSetup(4, 2000, 12);
ledcSetup(5, 2000, 12); 
ledcSetup(6, 2000, 12); 

// Chiudo tutti gli interruttori all'avvio (così magari casa non si allega ogni volta :))
ledcWrite(1, 4095);
ledcWrite(2, 4095);
ledcWrite(3, 4095);
ledcWrite(4, 4095);
ledcWrite(5, 4095);
ledcWrite(6, 4095);

digitalWrite(14, HIGH);
digitalWrite(25, HIGH);
digitalWrite(26, HIGH);
digitalWrite(27, HIGH);
digitalWrite(33, HIGH);
digitalWrite(35, HIGH);

  //Serial.begin(9600);
  
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  
  dht.setup(15, DHTesp::DHT22);
  
  Blynk.begin(auth, ssid, pass);
  Blynk.syncAll();
  ArduinoOTA.setHostname("MULTISENSOR");
  ArduinoOTA.begin();
    
  timer.setInterval(1000L, IRRIGAZIONE);
  timer.setInterval(1000L, SENSORI);
//    timer.setInterval(1000L, clockvalue);
  
  
}

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


//FUNCTIONS

int Water_CL;
int Qty1;
int Qty2;
int Qty3;
int Qty4;
int PumpStart;
int PumpState1;
int PumpState2;
int PumpState3;
int PumpState4;
int Duration1;
int Duration2;
int Duration3;
int Duration4;
int Duration5;
int Duration6;

BLYNK_WRITE(V20)
{
  Water_CL = param.asInt();  
  }

  BLYNK_WRITE(V21)
{
  Qty1 = param.asInt();  
  }

  BLYNK_WRITE(V22)
{
  Qty2 = param.asInt();  
  }

  BLYNK_WRITE(V23)
{
  Qty3 = param.asInt();  
  }

  BLYNK_WRITE(V24)
{
  Qty4 = param.asInt();  
  }
  
BLYNK_WRITE(V30)
{
  PumpStart = param.asInt();  
  }
  
BLYNK_WRITE(V31)
{
  PumpState1 = param.asInt();  
  }

  BLYNK_WRITE(V32)
{
  PumpState2 = param.asInt();  
  }

  BLYNK_WRITE(V33)
{
  PumpState3 = param.asInt();  
  }

    BLYNK_WRITE(V34)
{
  PumpState4 = param.asInt();  
  }

void IRRIGAZIONE()
{
  Duration1 = (Water_CL * Qty1)*15;
  Duration2 = (Water_CL * Qty2)*15;
  Duration3 = (Water_CL * Qty3)*15;
  Duration4 = (Water_CL * Qty4)*15;
  Duration5 = (Water_CL * 215);
  Duration6 = (Water_CL * 150);
    
  if (PumpStart == 1) {

  //AVVIO LE POMPE PERISTATICHE
      if(PumpState1 == 1) {
        ledcWrite(1, 0); //Accendi pompa1
        delay(Duration1);
        ledcWrite(1, 4095); //Spegni pompa1
        delay(500);
        Blynk.run();
      }

      if(PumpState2 == 1) {
        ledcWrite(2, 0); //Accendi pompa2
        delay(Duration2);
        ledcWrite(2, 4095); //Spegni pompa2
        delay(500);
        Blynk.run();
     }

      if(PumpState3 == 1) {
        ledcWrite(3, 0); //Accendi pompa3
        delay(Duration3);
        ledcWrite(3, 4095); //Spegni pompa3
        delay(500);
        Blynk.run();
        }

      if(PumpState4 == 1) {
        ledcWrite(4, 0); //Accendi pompa4
        delay(Duration4);
        ledcWrite(4, 4095); //Spegni pompa4
        Blynk.run();
        delay(500);
     }

  //CARICO ACQUA NELL'INTERCAPEDINE
        ledcWrite(5, 0); //Accendi pompa ingresso tanica
        
        int a;
        for( a = 1; a < 10; a = a + 1 ){
        delay(Duration5/10);
        Blynk.run();
          }
      
        ledcWrite(5, 4095); //Spegni pompa ingresso tanica
        delay(500);
        
  //ANNAFFIO DALL'INTERCAPEDINE

        ledcWrite(6, 0); //Accendi pompa uscita tanica
        
        int b;
        for( b = 1; b < 7; b = b + 1 ){
          int     check_water = analogRead(CHECK_PIN);  // Controllo ogni x secondi che l'acqua non stia sbordando
          if ( check_water < 50) {
            delay(Duration6/7*3);
            Blynk.run();
          }
          }
          int check_water = analogRead(CHECK_PIN);  // Controllo alla fine che l'acqua non sia sbordata
          if ( check_water < 50) {
            delay(5000);
          }
        ledcWrite(6, 4095); //Spegni pompa uscita tanica
        delay(500);
        
    Blynk.virtualWrite(V30, 0);
    PumpStart = 0;
    
    }
  }



BLYNK_WRITE(V11) //slider in Virtual Pin 0 (0...4095)
{
 ledcWrite(1, param.asInt()); // write red component to channel 1, etc. 0-4095

// float voltage1 = map(PWM_LED1, 0, 4095, 0, 330);
// Blynk.virtualWrite(V33, voltage1/100);  
}

BLYNK_WRITE(V12)
{
 ledcWrite(2, param.asInt());
}

BLYNK_WRITE(V13)
{
 ledcWrite(3, param.asInt());
}

BLYNK_WRITE(V14)
{
 ledcWrite(4, param.asInt());
}

BLYNK_WRITE(V15)
{
 ledcWrite(5, param.asInt());
}

BLYNK_WRITE(V16)
{
 ledcWrite(6, param.asInt());
}

BLYNK_WRITE(V40)
{
int currentValue = param.asInt() ;
if (currentValue == 0) {
      digitalWrite(14, HIGH);
} else {
      digitalWrite(14, LOW);
}
}

BLYNK_WRITE(V41)
{
int currentValue1 = param.asInt();
if (currentValue1 == 0) {
      digitalWrite(25, HIGH);
} else {
      digitalWrite(25, LOW);
}
}

BLYNK_WRITE(V42)
{
int currentValue = param.asInt();
if (currentValue == 0) {
      digitalWrite(26, HIGH);
} else {
      digitalWrite(26, LOW);
}
}

BLYNK_WRITE(V43)
{
int currentValue = param.asInt();
if (currentValue == 0) {
      digitalWrite(27, HIGH);
} else {
      digitalWrite(27, LOW);
}
}

BLYNK_WRITE(V44)
{
int currentValue = param.asInt();
if (currentValue == 0) {
      digitalWrite(33, HIGH);
} else {
      digitalWrite(33, LOW);
}
}

BLYNK_WRITE(V45)
{
int currentValue5 = param.asInt();
if (currentValue5 == 0) {
      digitalWrite(35, HIGH);
} else {
      digitalWrite(35, LOW);
}
}

void SENSORI()
{

    lastMillis = millis();
    Blynk.virtualWrite(V0, lastMillis/1000);


    // MOISTURE SENSOR
    int     x1 = analogRead(MOIST_PIN);
    int   y1 = map(x1, 4000, 1400 , 0, 100);
//    int   y1 = map(x1, 515, 290 , 0, 100);
    Blynk.virtualWrite(V_MOIST_PIN, y1);

    // WATER EXCEEDING SENSOR
    int     x2 = analogRead(CHECK_PIN);
    int   y2 = map(x2, 0, 700 , 0, 100);
    Blynk.virtualWrite(V_CHECK_PIN, y2);
    
    //WATER LEVEL SENSOR

    long duration1, distance1;
    digitalWrite(trigPin1, LOW); 
    delayMicroseconds(2);
    digitalWrite(trigPin1, HIGH);
    delayMicroseconds(10); 
    digitalWrite(trigPin1, LOW);
    duration1 = pulseIn(echoPin1, HIGH);
    distance1 = (duration1 / 2) / 29.1;

    long   y3 = map(distance1, 20, 5 , 0, 100);
    Blynk.virtualWrite(V_WATER1_PIN, y3);

    //DHT 22
    humidity = dht.getHumidity();          // Read humidity (percent)
    temp_c = dht.getTemperature();     // Read temperature as Farenheight

    String TempC;
    String Humid;

    TempC = String(temp_c);
    Humid = String(humidity);

    Blynk.virtualWrite(V_TEMP_PIN, TempC);
    Blynk.virtualWrite(V_HUMID_PIN, humidity);
    
  }

Well, your code is a mess, and that makes it very difficult to work-out what’s causing the issue.

I suspect that using GPIO15, which needs to be HIGH during boot may be an issue, and it doesnt hel;p that you’ve done this:

but not used `DHT22_PIN here…

Also, you try to do a digitalWrite to a pin that is input only

Unfortunately you’ve chosen to use your Rx pin to attach an LED to, so you cant get any clues about what’s happening in your serial monitor…

In addition, you don’t have any connection checking/reconnection code in your timed functions, and both timers are being called simultaneously…

also your IRRIGAZIONE function is full of unnecessary delays, which you should deal with.

This…

won’t do what you expect it to. The Blynk.syncAll() command needs to be in a BLYNK_CONNECRED() function.

Hopefully that’s not the case!

Pete.

Check this article