Outputs don´t work as desired after a long period of time with Blynk automation

I´m using an ESP32 along with Blynk´s automation feature to create an automated irrigation system. The system is supposed to turn valves on or off after certain soil humidity percentage is read. Each sensor controls one valve, and I have 4 sensors and 4 valves in total. I´m testing the code by putting the sensors in and out a cup of water and instead of valves I´m checking if leds turn on or off. What has happened is, lets say that I have 2 sensors that are wet and 2 that are dry. When the sensors are dry the valves should remain on, and they do for about a day, but randomly the valves that were on get turned off. I check the app to see what the sensors are reading but the humidity is accurate and it also says that the valves are on when they aren´t. I´ve been able to fix this by putting those sensors inside water and then taking them out again and it starts working correctly again, without having to reset the controller or anything like that. I haven´t been able to notice if the ESP32 gets disconnected from the internet when this happens since it happens randomly and I notice some time later. So far I haven´t noticed anything wrong with my code so I´m not sure if the problem has anything to do with that or if has something to do with my internet connection or the connection to the cloud, but I´m not sure that´s the problem because then why do the work just after getting the sensors wet again, anyway, I was hoping that someone could help me figure out what could be the cause of the problem. Thank you in advance :slight_smile:


#define BLYNK_TEMPLATE_ID ""
#define BLYNK_TEMPLATE_NAME ""
#include <BlynkSimpleEsp32.h>


//Se incluye librería para el WiFi
#include <WiFi.h>

//Se define el código de Blynk
char auth[] ="";

//Se definen nombre de red y contraseña a la que se va a conectar el controlador
const char* ssid = "";
const char* pass = "";

//Se inicializa el temporizador
BlynkTimer timer;

//Se conectan los sensores a los pines 36, 39, 34 y 35
int sensor1 = 36;
int sensor2 = 39;
int sensor3 = 34;
int sensor4 = 35;

//Se conectan los relé a los pines 23, 22, 21 y 19
int led1 = 23;
int led2 = 22;
int led3 = 21;
int led4 = 19;

//Se establecen las condiciones para que los leds se enciendan o apaguen
BLYNK_WRITE(V4)
 {
 if(param.asInt()==1)
  {
     digitalWrite(led1, HIGH);
  }
  else
  {
     digitalWrite(led1, LOW);
  }
 }

 BLYNK_WRITE(V5)
 {
 if(param.asInt()==1)
  {
     digitalWrite(led2, HIGH);
  }
  else
  {
     digitalWrite(led2, LOW);
  }
 }

 BLYNK_WRITE(V6)
 {
 if(param.asInt()==1)
  {
     digitalWrite(led3, HIGH);
  }
  else
  {
     digitalWrite(led3, LOW);
  }
 }

 BLYNK_WRITE(V7)
 {
 if(param.asInt()==1)
  {
     digitalWrite(led4, HIGH);
  }
  else
  {
     digitalWrite(led4, LOW);
  }
 }

void setup() {

//Hacer que el temporizador corra cada segundo
timer.setInterval(1000L, DatosSensor);
Serial.begin(115200);

  //Se inicia la conexión a la red WiFi definida
  WiFi.begin(ssid, pass);
  //delay(2000);

  //Mostrar asteriscos mientras se realiza la conexión
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print("*");
  }

  //Se anuncia cuando ya se haya conectado a la red WiFi y se muestra la dirección IP que será usada para el código en LabVIEW
  Serial.println("");
  Serial.println("Se ha conectado a la red WiFi");
  
  //Funciones para que el controlador se conecte automaticamente al WiFi en caso de perder la conexión
  WiFi.setAutoReconnect(true);
  WiFi.persistent(true);

  //Se inicia la conexión a la nube
  Blynk.begin(auth, ssid, pass);

  // Se establecen los sensores como entrada
  pinMode(sensor1, INPUT);
  pinMode(sensor2, INPUT);
  pinMode(sensor3, INPUT);
  pinMode(sensor4, INPUT);

  //Se establecen los relé como salida
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
}

//Se crea la función para enviar datos hacia la plataforma
void DatosSensor() { 
//Se lee la información del sensor 1 y se envía hacia el pin virtual "V0"
int humedad1=analogRead(sensor1);
int porcentaje1 = map(humedad1, 3200, 1600, 0, 100);
Blynk.virtualWrite(V0,porcentaje1);

//Se lee la información del sensor 2 y se envía hacia el pin virtual "V1"
int humedad2=analogRead(sensor2);
int porcentaje2 = map(humedad2, 3200, 1600, 0, 100);
Blynk.virtualWrite(V1,porcentaje2);

//Se lee la información del sensor 3 y se envía hacia el pin virtual "V2"
int humedad3=analogRead(sensor3);
int porcentaje3 = map(humedad3, 3200, 1600, 0, 100);
Blynk.virtualWrite(V2,porcentaje3);

//Se lee la información del sensor 4 y se envía hacia el pin virtual "V3"
int humedad4=analogRead(sensor4);
int porcentaje4 = map(humedad4, 3200, 1600, 0, 100);
Blynk.virtualWrite(V3,porcentaje4);
} 


void loop() {

Blynk.run();
timer.run();

}

You should be able to see the off/online events in the timeline.

I suspect that your issue is due to disconnections and/or device reboots. Have you tried adding a BLYNK_CONNECTED() function with Blynk.syncVirtual(VPin) commands inside it for each of your virtual pins that are controlled by the automation?

Pete.

I checked the events and it doesn´t show anny offline/online events around the time that this happens. I´ll try using the functions that you mentioned. I wanted to ask, would BLYNK_CONNECTED() go right after declaring the pin numbers for my sensors and leds? or does it go inside the void setup?

It doesn’t matter, it’s a callback function that the Blynk library calls, just like the BLYNK_WRITE(Vpin) functions.

NO! You cannot put one function inside another, so BLYNK_CONNECTED() cannot go inside void loop()

Pete.