Arduino disconnects during the loop

Hi guys, I am using Blynk to manage my greenhouse. I developed this code, but Arduino frequently disconnects from the internet. I am using an arduino mega with a W5100 ethernet shield (blynk version 1.0).

This is the code:


String 
firmwareInfo      = "V0.1   ",
firmwareDate      = "29/10/21";

#include <OneWire.h>
#include <EEPROM.h> 
#include <SPI.h>
#include <SD.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <EEPROM.h>                     
#include <virtuabotixRTC.h>  
#include <SimpleDHT.h>
#include <DallasTemperature.h>
#include <WidgetRTC.h>
#include "MyTimer.h"

#define BLYNK_DEVICE_NAME           "Centralina"
#define BLYNK_AUTH_TOKEN            "xx"
#define BLYNK_PRINT Serial 
char auth[] = BLYNK_AUTH_TOKEN;
BlynkTimer timer; 
MyTimer timer_tank = MyTimer();
MyTimer a_aeroponica = MyTimer();
MyTimer b_aeroponica = MyTimer();
MyTimer c_aeroponica = MyTimer();
MyTimer waiting_aeroponica = MyTimer();
int ReCnctFlag;  // Reconnection Flag
int ReCnctCount = 0;  // Reconnection counter

#define W5100_CS  10
#define SDCARD_CS 4

#define RTC_CLK             21
#define RTC_DAT             21
#define RTC_RST             21
#define DHT_PIN             39

#define temp_cisterna_aeroponica  40               

 
#define aeroponica_lv_HIGH   46       
#define aeroponica_lv_LOW    47       

#define contalitri          34       

#define settore_1           37       // Settore 1 aeroponica
#define settore_2           29       // Settore 3 aeroponica
#define settore_3           36       // Settore 3 aeroponica

#define pompa_aeroponica    35       
#define pompa_fuorisuolo    27
#define pompa_insalate      34

SimpleDHT11 dht11(DHT_PIN);
OneWire oneWire(temp_cisterna_aeroponica);
DallasTemperature cisterna_aeroponica(&oneWire);

WidgetRTC rtc_blynk;
WidgetTerminal nutrient_terminal(V97);
WidgetTerminal terminal(V99);
WidgetLED aeroponica_pump(V100);
WidgetLED aeroponica_a(V101);
WidgetLED aeroponica_b(V102);
WidgetLED aeroponica_c(V103);
WidgetLED fuorisuolo_pump(V104);
WidgetLED fuorisuolo_riserva(V105);
WidgetLED aeroponica_riserva(V106);
WidgetLED tank_pump(V107);

unsigned long total_second;

uint8_t s1_temp_fuorisuolo[8] = { 0x28, 0xEE, 0xD5, 0x64, 0x1A, 0x16, 0x02, 0xEC };
uint8_t s1_temp_aeroponica[8] = { 0x28, 0x61, 0x64, 0x12, 0x3C, 0x7C, 0x2F, 0x27 }; // cisterna
uint8_t s2_temp_aeroponica[8] = { 0x28, 0x61, 0x64, 0x12, 0x3F, 0xFD, 0x80, 0xC6 }; // vasca
uint8_t s1_temp_insalata[8]   = { 0x28, 0xEE, 0xD5, 0x64, 0x1A, 0x16, 0x02, 0xEC };

bool empty_tank_status;      
int tank_liters;             // V37
float KW;                    // V49
byte tank_destination;       // V40 - destinazione acqua cisterna soluzione
byte tank_solution;          // V41 - soluzione selezionata 
bool tank_timer_status;
bool tank_drain_status;
struct aeroponica{
  float water_use;           // V50 - litri
  int start_automation_time; // V10 - in minuti
  int stop_automation_time;  // V10 - in minuti
  int tank_liters_estimate;  // V51 - litri
  byte a_time;               // V30 - in secondi
  byte b_time;               // V31 - in secondi
  byte c_time;               // V32 - in secondi
  byte waiting_time;         // V33 - in minuti
  byte out_a_time = 30;
  byte out_b_time = 30;
  byte out_c_time = 30;
  byte out_waiting_time = 120;
  bool automation_status;    // V20 
  bool drain_status;         // V21
  bool a_status;          
  bool b_status;
  bool c_status;
  bool pump_status;

}aeroponica;
struct fuorisuolo{
  float liters_per_plant;    // V60 - in litri
  int start_automation_time; // V11 - in minuti
  int stop_automation_time;  // V11 - in minuti
  int tank_liters_estimate;  // V52 - in litri
  byte daily_cycles;         // V34
  byte minutes_per_cycle;    // V35 - in minuti
  byte number_plants;        // V36
  byte a_time;               
  byte waiting_time;
  bool automation_status;    // V22
  bool drain_status;         // V23
  bool a_status;
  bool pump_status;
}fuorisuolo;
struct ricetta{
  int nitrato_calcio;
  int nitrato_potassio;
  int nitrato_ammonico;
  int nitrato_magnesio;
  int microelementi;
  int solfato_potassio;
  int fosfato_monopotassico;
  int solfato_magnesio;
  int acido_nitrico;
  int acido_fosforico;
  int ec;
  float ph;
  int n;
  int p;
  int k;
  int ca;
  int mg;
}ricetta;
int indirizzo_ricetta[10] = {40, 80, 120, 160, 200, 240, 280, 320, 360, 400};  // INDIRIZZI EEPROM

int minuti_now;
byte aeroponica_priority = 0;
  
BLYNK_CONNECTED() {
  Blynk.syncAll();
  rtc_blynk.begin();
  ReCnctCount = 0;
  tank_liters_cal(tank_destination);

}
BLYNK_WRITE(V9){    // Reset consumi   
  bool stato = param[0].asInt();
  if(stato){
    Serial.println("RESET CONSUMI SETTIMANALI");
    EEPROM.put(1, 0);
    KW = 0;
    Blynk.virtualWrite(V49, KW);
    stato = false;
    Blynk.virtualWrite(V9, stato);
  }
}
BLYNK_WRITE(V10){  
  TimeInputParam t(param);
  aeroponica.start_automation_time = (t.getStartHour() * 60) + t.getStartMinute();  
  aeroponica.stop_automation_time  = (t.getStopHour()  * 60) + t.getStopMinute();
  Serial.print("AEROPONICA START TIME : "); 
  Serial.print(t.getStartHour());
  Serial.print(":");
  Serial.println(t.getStartMinute());
  Serial.print("AEROPONICA STOP TIME  : "); 
  Serial.print(t.getStopHour());
  Serial.print(":");
  Serial.println(t.getStopMinute());
}
BLYNK_WRITE(V11){  
  TimeInputParam t(param);
  fuorisuolo.start_automation_time = (t.getStartHour() * 60) + t.getStartMinute();  
  fuorisuolo.stop_automation_time  = (t.getStopHour()  * 60) + t.getStopMinute();
  Serial.print("FUORISUOLO START TIME : "); 
  Serial.print(t.getStartHour());
  Serial.print(":");
  Serial.println(t.getStartMinute());
  Serial.print("FUORISUOLO STOP TIME  : "); 
  Serial.print(t.getStopHour());
  Serial.print(":");
  Serial.println(t.getStopMinute());
}
BLYNK_WRITE(V20){  
  aeroponica.automation_status = param[0].asInt();
  Serial.print("Aeroponica AUTO   : "); 
  if(aeroponica.automation_status){
    Serial.println("ON");  
    aeroponica_priority = 1;
  }
  else Serial.println("OFF"); 
}
/*BLYNK_WRITE(V21){    
  aeroponica.drain_status = param[0].asInt();
  if(aeroponica.automation_status){
    Blynk.virtualWrite(V20, LOW);
    aeroponica.automation_status = LOW;
  }
  Serial.print("Aeroponica DRAIN  : "); 
  if(aeroponica.drain_status){
    Serial.println("ON");
    digitalWrite(pompa_aeroponica, LOW);  
  }
  else{
    Serial.println("OFF");  
    digitalWrite(pompa_aeroponica, HIGH);
  }
}
*/
BLYNK_WRITE(V22){    // FUORISUOLO: automazione
  fuorisuolo.automation_status = param[0].asInt();
  Serial.print("Fuorisuolo AUTO   : "); 
  if(fuorisuolo.automation_status) Serial.println("ON");  
  else Serial.println("OFF");
}
/*BLYNK_WRITE(V23){    // FUORISUOLO: scarico
  fuorisuolo.drain_status = param[0].asInt(); 
  if(fuorisuolo.automation_status){
    Blynk.virtualWrite(V22, LOW);
    fuorisuolo.automation_status = LOW;
  }
  Serial.print("Fuorisuolo DRAIN  : "); 
  if(fuorisuolo.drain_status) Serial.println("ON");  
  else Serial.println("OFF");  
}
*/
int tank_m_timer;
#define pump_perm 40 // LITRi/M POMPA SOLUZIONE
#define delay_pump 40 // in secondi

BLYNK_WRITE(V24){    // TANK: Svuota cisterna
  empty_tank_status = param[0].asInt(); 
  Serial.print("TANK svuota cisterna  : ");
  if(empty_tank_status == true && tank_drain_status == false){
    Serial.println("ON");
    tank_timer_status = true;
    Serial.print("LITRI PREPARAZIONE: "); Serial.println(tank_liters);
    tank_m_timer = (((tank_liters / pump_perm)*60)+delay_pump);
    Serial.print("TEMPO POMPA: "); Serial.print(tank_m_timer); Serial.println("s");
    timer_tank.set(tank_m_timer);
    
    switch(tank_destination){
      case 1: // AEROPONICA
        aeroponica.tank_liters_estimate = aeroponica.tank_liters_estimate + tank_liters;
        Blynk.virtualWrite(V51, aeroponica.tank_liters_estimate);
        break;
      case 2: // FUORISUOLO
        fuorisuolo.tank_liters_estimate = fuorisuolo.tank_liters_estimate + tank_liters;
        Blynk.virtualWrite(V52, fuorisuolo.tank_liters_estimate);
        break;
    }
  }else if(empty_tank_status == true && tank_drain_status == true){
    Blynk.notify("Non puoi attivare la procedura automatica durante lo scarico manuale");
    Blynk.virtualWrite(V24, LOW);
    empty_tank_status = false;
  }else if(empty_tank_status == false){
    Serial.println("OFF");
    tank_liters_cal(tank_destination);
    tank_timer_status = false;
  }
} 
BLYNK_WRITE(V25){    // FUORISUOLO: Azzera litri cisterna
  int stato = param[0].asInt(); 
  if(stato){
    Blynk.virtualWrite(V52, 0);
    Blynk.virtualWrite(V25, LOW);
    fuorisuolo.tank_liters_estimate = 0;
    tank_liters_cal(tank_destination);
    Blynk.notify("Litri cisterna fuorisuolo azzerati");
    stato = 0;
  }
}
BLYNK_WRITE(V26){    // AEROPONICA: Azzera litri cisterna
  int stato = param[0].asInt(); 
  if(stato){
    Blynk.virtualWrite(V51, 0);
    Blynk.virtualWrite(V26, LOW);
    aeroponica.tank_liters_estimate = 0;  
    tank_liters_cal(tank_destination);  
    Blynk.notify("Litri cisterna aeroponica azzerati");
    stato = 0;
  }
}
BLYNK_WRITE(V27){    // TNAK: Scarico vasca
  int stato = param[0].asInt(); 
  if(tank_timer_status == true and stato == true){
    Blynk.notify("Non puoi scaricare la vasca durante la procedura automatica");
    Blynk.virtualWrite(V27, LOW);
    stato = false;
  }else if(tank_timer_status == false and stato == true){
    tank_drain_status = true;
  }else if(tank_timer_status == false and stato == false){
    tank_drain_status = false;
    stato = false;
  }
}
BLYNK_WRITE(V29){ // AEROPONICA: sleep notte
  aeroponica.out_waiting_time = param[0].asInt();
  Serial.print("Aereoponica sleep TIME    : "); Serial.print(aeroponica.out_waiting_time); Serial.println("m");
}
BLYNK_WRITE(V30){ // AEROPONICA: Settore A in secondi
  aeroponica.a_time = param[0].asInt();
  Serial.print("Aereoponica A TIME    : "); Serial.print(aeroponica.a_time); Serial.println("s");
}
BLYNK_WRITE(V31){ // AEROPONICA: Settore B in secondi
  aeroponica.b_time = param[0].asInt();
  Serial.print("Aereoponica B TIME    : "); Serial.print(aeroponica.b_time); Serial.println("s");
}
BLYNK_WRITE(V32){ // AEROPONICA: Settore C in secondi
  aeroponica.c_time = param[0].asInt();
  Serial.print("Aereoponica C TIME    : "); Serial.print(aeroponica.c_time); Serial.println("s");
}
BLYNK_WRITE(V33){ // AEROPONICA: Riposo in minuti
  aeroponica.waiting_time = param[0].asInt();
  Serial.print("Aereoponica WAIT TIME : "); Serial.print(aeroponica.waiting_time); Serial.println("m");
}
BLYNK_WRITE(V34){ // FUORISUOLO: Cicli giornalieri
  fuorisuolo.daily_cycles = param[0].asInt();
  Serial.print("Fuorisuolo CICLIxDAY  : "); Serial.println(fuorisuolo.daily_cycles); 
}
BLYNK_WRITE(V35){ // FUORISUOLO: Irrigazione per ciclo
  fuorisuolo.minutes_per_cycle = param[0].asInt();
  Serial.print("Fuorisuolo IRRIGAZIONE: "); Serial.print(fuorisuolo.minutes_per_cycle); Serial.println("m");
}
BLYNK_WRITE(V36){ // FUORISUOLO: Irrigazione per ciclo
  fuorisuolo.number_plants = param[0].asInt();
  Serial.print("Fuorisuolo PIANTE     : "); Serial.println(fuorisuolo.number_plants);
}
BLYNK_WRITE(V37){ // TANK: Litri preparazione soluzione
  tank_liters = param[0].asInt();
  Serial.print("TANK LITRI            : "); Serial.println(tank_liters);
  nutrient_terminal.clear();
  selected_fertilizer();
}
BLYNK_WRITE(V40){ // TANK: Scelta cisterna di destinazione
  Serial.print("TANK Destinazione     : ");
  switch(param[0].asInt()){
    case 1:
      tank_destination = 1;
      tank_liters_cal(tank_destination);
      Serial.println("AEROPONICA");
      break;
    case 2:
      tank_destination = 2;
      tank_liters_cal(tank_destination);
      Serial.println("FUORISUOLO");
      break;
    case 3:
      tank_destination = 3;
      Serial.println("ALTRO");
      break;
  }
}
BLYNK_WRITE(V41){ // TANK: Scelta ricetta nutritiva
  nutrient_terminal.clear();
  Serial.print("TANK Ricetta          : ");
  switch(param[0].asInt()){
    case 1:
      tank_solution = 1;
      Serial.println("POMODORO 1");
      break;
    case 2:
      tank_solution = 2;
      Serial.println("POMODORO 2");
      break;
    case 3:
      tank_solution = 3;
      Serial.println("POMODORO 3");
      break;
    case 4:
      tank_solution = 4;
      Serial.println("POMODORO 4");
      break;
    case 5:
      tank_solution = 5;
      Serial.println("FUORISUOLO 1");
      break;
    case 6:
      tank_solution = 6;
      Serial.println("FUORISUOLO 2");
      break;
    case 7:
      tank_solution = 7;
      Serial.println("FUORISUOLO 3");
      break;
    case 8:
      tank_solution = 8;
      Serial.println("FUORISUOLO 4");
      break;
    case 9:
      tank_solution = 9;
      Serial.println("INSALATA 1");
      break;
    case 10:
      tank_solution = 10;
      Serial.println("INSALATA 2");
      break;
  }
  selected_fertilizer();
}//27
void setup(){
  Serial.begin(9600);                 // Inizializzazione seriale    
  Blynk.begin(auth);    
  cisterna_aeroponica.begin();
 /*
  pinMode(UP,   INPUT_PULLUP);
  pinMode(DOWN, INPUT_PULLUP);
  pinMode(OK,   INPUT_PULLUP);
  pinMode(BACK, INPUT_PULLUP);
  pinMode(HOME, INPUT_PULLUP);
*/
  pinMode(aeroponica_lv_LOW, INPUT);
  pinMode(aeroponica_lv_LOW, INPUT);

  pinMode(settore_1, OUTPUT);
  pinMode(settore_2, OUTPUT);
  pinMode(settore_3, OUTPUT);
  pinMode(pompa_aeroponica, OUTPUT);
  pinMode(pompa_fuorisuolo, OUTPUT);
  pinMode(pompa_insalate, OUTPUT);

  digitalWrite(settore_1, LOW);
  digitalWrite(settore_2, LOW);
  digitalWrite(settore_3, LOW);
  digitalWrite(pompa_aeroponica, LOW);
  digitalWrite(pompa_fuorisuolo, LOW);
  digitalWrite(pompa_insalate, LOW);

  aeroponica_a.off();
  aeroponica_b.off();
  aeroponica_c.off();
  aeroponica_pump.off();
    
  aeroponica.tank_liters_estimate = 450;
  fuorisuolo.tank_liters_estimate = 110;
  
  check_aeroponica_lv();
  check_fuorisuolo_lv();
  check_dht();
  check_probes();
  rtcnow();
  
  // MAX 16 TIMER 
  timer.setInterval(600000L,  check_probes);
  timer.setInterval(320000L,  check_aeroponica_lv); // 60000L
  timer.setInterval(310000L,  check_fuorisuolo_lv);
  timer.setInterval(300000L,  check_dht);
  timer.setInterval(60000L,   rtcnow);;
  timer.setInterval(1000L,    aeroponica_rtc);
  
  Serial.println("[!] SETUP TERMINATO");
  Serial.println(""); Serial.println("");
}
void loop(){
  if (Blynk.connected()) {
    Blynk.run();
  } else if (ReCnctFlag == 0) {  // If NOT connected and not already trying to reconnect, set timer to try to reconnect in 30 seconds
    ReCnctFlag = 1;  // Set reconnection Flag
    Serial.println("AVVIO TIMER DI RICONNESSIONE OGNI 30 SECONDI");
    timer.setTimeout(30000L, []() {  // Lambda Reconnection Timer Function
      ReCnctFlag = 0;  // Reset reconnection Flag
      ReCnctCount++;  // Increment reconnection Counter
      Serial.print("TENTATIVO DI CONNESSIONE #");
      Serial.println(ReCnctCount);
      Blynk.connect();  
    });  // END Timer Function
  }  
  timer.run();  
}

void check_aeroponica_lv(){
  Serial.println("CHECK AEROPONICA LV");
  if(digitalRead(aeroponica_lv_LOW) == HIGH){ // 28 digital
    aeroponica_riserva.on();
    Blynk.virtualWrite(90, HIGH);
  }else{
    aeroponica_riserva.off();
    Blynk.virtualWrite(90, LOW);
  }
  
  if(digitalRead(aeroponica_lv_LOW) == LOW and digitalRead(aeroponica_lv_HIGH) == LOW)        Blynk.virtualWrite(V49, 1000);
  else if(digitalRead(aeroponica_lv_LOW) == LOW and digitalRead(aeroponica_lv_HIGH) == HIGH)  Blynk.virtualWrite(V49, 500);
  else if(digitalRead(aeroponica_lv_LOW) == HIGH and digitalRead(aeroponica_lv_HIGH) == HIGH) Blynk.virtualWrite(V49, 100);
}

void check_fuorisuolo_lv(){
  fuorisuolo_riserva.off(); 
  /*
  if(digitalRead(fuorisuolo_lv_LOW) == HIGH){ // 30 digital
    fuorisuolo_riserva.on();
    Blynk.virtualWrite(91, HIGH);
  }else{
    fuorisuolo_riserva.off();     
    Blynk.virtualWrite(91, LOW);                                                                             
  }
  Blynk.virtualWrite(V52, fuorisuolo.tank_liters_estimate);
  */
}

void check_dht(){
  byte temperature = 0;
  byte humidity = 0;  
  int err = SimpleDHTErrSuccess;
  if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
    Serial.print("Read DHT11 failed, err="); Serial.println(err);
  }
  Serial.print((int)temperature); Serial.print(" *C, "); 
  Serial.print((int)humidity); Serial.println(" H");
  
  Blynk.virtualWrite(V1, (int)temperature);  // TEMPERATURE
  Blynk.virtualWrite(V2, (int)humidity);  // HUMIDITY
}
void check_probes(){
  // LINK: https://lastminuteengineers.com/multiple-ds18b20-arduino-tutorial/
  cisterna_aeroponica.requestTemperatures();
  float temp = cisterna_aeroponica.getTempCByIndex(0);
  if(temp<50){
    Blynk.virtualWrite(V4, temp); // cisterna
    Serial.print("TEMP cisterna aeroponica: ");
    Serial.println(temp);
  }
/*
  Blynk.virtualWrite(V3, printTemperature(s1_temp_fuorisuolo));
  Blynk.virtualWrite(V4, printTemperature(s1_temp_aeroponica)); // cisterna
  Blynk.virtualWrite(V5, printTemperature(s2_temp_aeroponica)); // vasca
  Blynk.virtualWrite(V6, printTemperature(s1_temp_insalata));
*/
  
}


void rtcnow(){
  unsigned long ore = hour();
  unsigned long minuti = minute();
  unsigned long secondi = second();
  Serial.print(ore); Serial.print(":");
  Serial.print(minuti); Serial.print(":");
  Serial.println(secondi);
  total_second = ((ore*3600)+(minuti*60)+secondi);
  minuti_now = (ore*60)+(minuti);
}
void tank_liters_cal(byte destination){
  int centinaia;
  if(destination == 1){
    centinaia = aeroponica.tank_liters_estimate / 100;
    tank_liters = 1000 - centinaia*100;
    if(tank_liters+aeroponica.tank_liters_estimate > 1000) tank_liters = tank_liters - 100;
    Blynk.virtualWrite(V37, tank_liters);    
  }
  if(destination == 2){
    centinaia = fuorisuolo.tank_liters_estimate / 100;
    tank_liters = 1000 - centinaia*100;
    if(tank_liters+fuorisuolo.tank_liters_estimate > 1000) tank_liters = tank_liters - 100;
    Blynk.virtualWrite(V37, tank_liters);    
  }
  nutrient_terminal.clear();
  selected_fertilizer();
}
void tank_rtc(){
  if (timer_tank.check() == true and tank_timer_status == true && tank_drain_status == false) {
    tank_pump.on();
  }else if(tank_timer_status == false && tank_drain_status == true){
    tank_pump.on();
  }else if(tank_timer_status == false && tank_drain_status == false){
    tank_pump.off();
  }else if(timer_tank.check() == false && tank_timer_status == true && tank_drain_status == false){
    tank_pump.off();
    tank_timer_status = false;
    empty_tank_status = false;
    Blynk.virtualWrite(V24, LOW);
  }
}
void selected_fertilizer(){
  int pos = tank_solution-1; // perchè vettore parte da 0
  get_nutrient(indirizzo_ricetta[pos]);
  nutrient_terminal.print("PREPARAZIONE: ");
  switch(tank_solution){
    case 1:   // POMODORO 1
      nutrient_terminal.print("POMODORO 1 x ");
      break;
    case 2:   // POMODORO 2
      nutrient_terminal.print("POMODORO 2 x ");
      break;
    case 3:   // POMODORO 3
      nutrient_terminal.print("POMODORO 3 x ");
      break;
    case 4:   // POMODORO 4
      nutrient_terminal.print("POMODORO 4 x ");
      break;
    case 5:   // FUORISUOLO 1
      nutrient_terminal.print("FUORISUOLO 1 x ");
      break;
    case 6:   // FUORISUOLO 2
      nutrient_terminal.print("FUORISUOLO 2 x ");  
      break;
    case 7:   // FUORISUOLO 3
      nutrient_terminal.print("FUORISUOLO 3 x ");
      break;
    case 8:   // FUORISUOLO 4
      nutrient_terminal.print("FUORISUOLO 4 x ");   
      break;
    case 9:   // INSALATA 1
      nutrient_terminal.print("INSALATA 1 x "); 
      break;
    case 10:  // INSALATA 2
      nutrient_terminal.print("INSALATA 2 x "); 
      break;
  }
  nutrient_terminal.print(tank_liters);             nutrient_terminal.println("Litri");
  nutrient_terminal.print("EC: ");                  nutrient_terminal.print(ricetta.ec);   
  nutrient_terminal.print(" - PH: ");               nutrient_terminal.println(ricetta.ph);   
     
  nutrient_terminal.println(""); 
  nutrient_terminal.println("SOLUZIONE A");   
  
  if(ricetta.nitrato_calcio>0){
    int xx = (ricetta.nitrato_calcio/10)*(tank_liters/100);
    nutrient_terminal.print("Nitrato di calcio   : ");    nutrient_terminal.print(xx);   nutrient_terminal.println("gr");           
  }
  if(ricetta.nitrato_ammonico>0){
    int xx = (ricetta.nitrato_ammonico/10)*(tank_liters/100);
    nutrient_terminal.print("Nitrato Ammonico    : ");    nutrient_terminal.print(xx);   nutrient_terminal.println("gr");   
  }   
  if(ricetta.microelementi>0){
    int xx = (ricetta.microelementi/10)*(tank_liters/100);
    nutrient_terminal.print("Microelementi       : ");    nutrient_terminal.print(xx);   nutrient_terminal.println("gr");   
  }
  if(ricetta.nitrato_magnesio>0){
    int xx = (ricetta.nitrato_magnesio/10)*(tank_liters/100);
    nutrient_terminal.print("Nitrato Magnesio    : ");    nutrient_terminal.print(xx);   nutrient_terminal.println("gr");      
  }   

  nutrient_terminal.println("");     
  nutrient_terminal.println("SOLUZIONE B");

  if(ricetta.nitrato_potassio>0){
    int xx = (ricetta.nitrato_potassio/10)*(tank_liters/100);
    nutrient_terminal.print("Nitrato di Potassio : ");    nutrient_terminal.print(xx);   nutrient_terminal.println("gr");      
  }  
  if(ricetta.solfato_potassio>0){
    int xx = (ricetta.solfato_potassio/10)*(tank_liters/100);
    nutrient_terminal.print("Solfato di Potassio : ");    nutrient_terminal.print(xx);   nutrient_terminal.println("gr");      
  }
  if(ricetta.solfato_magnesio>0){
    int xx = (ricetta.solfato_magnesio/10)*(tank_liters/100);
    nutrient_terminal.print("Solfato di Magnesio : ");    nutrient_terminal.print(xx);   nutrient_terminal.println("gr");      
  }
  if(ricetta.fosfato_monopotassico>0){
    int xx = (ricetta.fosfato_monopotassico/10)*(tank_liters/100);
    nutrient_terminal.print("0-52-34             : ");    nutrient_terminal.print(xx);   nutrient_terminal.println("gr");      
  }

  nutrient_terminal.println("");  
  nutrient_terminal.println("SOLUZIONE C"); 
    
  if(ricetta.acido_nitrico>0){
    int xx = (ricetta.acido_nitrico/10)*(tank_liters/100);
    nutrient_terminal.print("Acido Nitrico       : ");    nutrient_terminal.print(xx);   nutrient_terminal.println("ml");      
  }
  if(ricetta.acido_fosforico>0){
    int xx = (ricetta.acido_fosforico/10)*(tank_liters/100);
    nutrient_terminal.print("Acido fosforico     : ");    nutrient_terminal.print(xx);   nutrient_terminal.println("ml");      
  }

  nutrient_terminal.println("");
  nutrient_terminal.println("---------------------");
  nutrient_terminal.println("SOLUZIONE NUTRITIVA");
  nutrient_terminal.print("N : "); nutrient_terminal.print(ricetta.n); nutrient_terminal.println("ppm");   
  nutrient_terminal.print("P : "); nutrient_terminal.print(ricetta.p); nutrient_terminal.println("ppm");   
  nutrient_terminal.print("K : "); nutrient_terminal.print(ricetta.k); nutrient_terminal.println("ppm");   
  nutrient_terminal.print("Ca: "); nutrient_terminal.print(ricetta.ca); nutrient_terminal.println("ppm");   
  nutrient_terminal.print("Mg: "); nutrient_terminal.print(ricetta.mg); nutrient_terminal.println("ppm");   
  nutrient_terminal.flush();
}

byte aeroponica_out_priority = 1;
bool aeroponica_timer_set = false;
bool aeroponica_reset = false;
bool aeroponica_out_automation = false;

void update_kw(){
  Blynk.virtualWrite(V49, KW);
  EEPROM.put(1, KW);
}
void aeroponica_rtc(){
  if(aeroponica.automation_status){
    if(minuti_now >= aeroponica.start_automation_time and minuti_now <= aeroponica.stop_automation_time){
      if(aeroponica_out_automation){
        Serial.println("[AEROPONICA OUT AUTOMATION] FINE AUTOMAZIONE NOTTURNA");
        aeroponica_out_automation = false; 
        aeroponica_timer_set = false;
        aeroponica_a.off();
        aeroponica_b.off();
        aeroponica_c.off();
        aeroponica_pump.off();
        digitalWrite(pompa_aeroponica, LOW);
        digitalWrite(settore_1, LOW);
        digitalWrite(settore_2, LOW);
        digitalWrite(settore_3, LOW);
      }
      switch(aeroponica_priority){
        case 1:   // SETTORE A
          if(aeroponica.a_time>0){
            if(aeroponica_timer_set == false){
              Serial.print("[AEROPONICA] Avvio A ");
              Serial.print(aeroponica.a_time);
              Serial.println(" s");
              a_aeroponica.set(aeroponica.a_time);
              aeroponica_timer_set = true;
              aeroponica_pump.on();
              aeroponica_a.on();
              digitalWrite(pompa_aeroponica, HIGH);
              digitalWrite(settore_1, HIGH);
            }else{
              if(!a_aeroponica.check()){
                aeroponica_pump.off();
                aeroponica_a.off();
                digitalWrite(pompa_aeroponica, LOW);
                digitalWrite(settore_1, LOW);
                aeroponica_priority = 2;
                aeroponica_timer_set = false;
              }
            }
          }else aeroponica_priority = 2;
          break;
        case 2:   // SETTORE B
          if(aeroponica.b_time>0){
            if(aeroponica_timer_set == false){
              Serial.print("[AEROPONICA] Avvio B ");
              Serial.print(aeroponica.b_time);
              Serial.println(" s");
              b_aeroponica.set(aeroponica.b_time);
              aeroponica_timer_set = true;
              aeroponica_pump.on();
              aeroponica_b.on();
              digitalWrite(pompa_aeroponica, HIGH);
              digitalWrite(settore_2, HIGH);
            }else{
              if(!b_aeroponica.check()){
                aeroponica_pump.off();
                aeroponica_b.off();
                digitalWrite(pompa_aeroponica, LOW);
                digitalWrite(settore_2, LOW);
                aeroponica_priority = 3;
                aeroponica_timer_set = false;
              }
            }
          }else aeroponica_priority = 3;
          break;
        case 3:   // SETTORE C
          if(aeroponica.c_time>0){
            if(aeroponica_timer_set == false){
              Serial.print("[AEROPONICA] Avvio C ");
              Serial.print(aeroponica.c_time);
              Serial.println(" s");
              c_aeroponica.set(aeroponica.c_time);
              aeroponica_timer_set = true;
              aeroponica_pump.on();
              aeroponica_c.on();
              digitalWrite(pompa_aeroponica, HIGH);
              digitalWrite(settore_3, HIGH);
            }else{
              if(!c_aeroponica.check()){
                aeroponica_pump.off();
                aeroponica_c.off();
                digitalWrite(pompa_aeroponica, LOW);
                digitalWrite(settore_3, LOW);
                aeroponica_priority = 4;
                aeroponica_timer_set = false;
              }
            }
          }else aeroponica_priority = 4;
          break;
        case 4:   // TEMPO RISPOSO
          if(aeroponica.waiting_time>0){
            if(aeroponica_timer_set == false){
              Serial.print("[AEROPONICA] Avvio WAITING ");
              Serial.print(aeroponica.waiting_time);
              Serial.println(" m");
              waiting_aeroponica.set(aeroponica.waiting_time*60);
              aeroponica_timer_set = true;
              aeroponica_pump.off();
              digitalWrite(pompa_aeroponica, LOW);
            }else{
              if(!waiting_aeroponica.check()){
                aeroponica_priority = 1;
                aeroponica_timer_set = false;
              }
            }
          }else aeroponica_priority = 1;
          break;
      }
    }else if(aeroponica_out_automation == false){
      Serial.println("[AEROPONICA OUT AUTOMATION] AVVIO AUTOMAZIONE NOTTURNA");
      aeroponica_priority = 1;
      aeroponica_timer_set = false;
      aeroponica_a.off();
      aeroponica_b.off();
      aeroponica_c.off();
      aeroponica_pump.off();
      digitalWrite(pompa_aeroponica, LOW);
      digitalWrite(settore_1, LOW);
      digitalWrite(settore_2, LOW);
      digitalWrite(settore_3, LOW);
      aeroponica_out_automation = true;
    }
    if(aeroponica_out_automation){
      switch(aeroponica_priority){
        case 1:   // SETTORE A
          if(aeroponica_timer_set == false){
            Serial.print("[AEROPONICA OUT AUTOMATION] Avvio A ");
            Serial.print(aeroponica.out_a_time);
            Serial.println(" s");
            a_aeroponica.set(aeroponica.out_a_time);
            aeroponica_timer_set = true;
          }
          if(a_aeroponica.check()){
            aeroponica_pump.on();
            aeroponica_a.on();
            digitalWrite(pompa_aeroponica, HIGH);
            digitalWrite(settore_1, HIGH);
          }else{
            aeroponica_pump.off();
            aeroponica_a.off();
            digitalWrite(pompa_aeroponica, LOW);
            digitalWrite(settore_1, LOW);
            aeroponica_priority = 2;
            aeroponica_timer_set = false;
          }
          break;
        case 2:   // SETTORE B
          if(aeroponica_timer_set == false){
            Serial.print("[AEROPONICA OUT AUTOMATION] Avvio B ");
            Serial.print(aeroponica.out_b_time);
            Serial.println(" s");
            a_aeroponica.set(aeroponica.out_b_time);
            aeroponica_timer_set = true;
          }
          if(a_aeroponica.check()){
            aeroponica_pump.on();
            aeroponica_b.on();
            digitalWrite(pompa_aeroponica, HIGH);
            digitalWrite(settore_2, HIGH);
          }else{
            aeroponica_pump.off();
            aeroponica_b.off();
            digitalWrite(pompa_aeroponica, LOW);
            digitalWrite(settore_2, LOW);
            aeroponica_priority = 3;
            aeroponica_timer_set = false;
          }
          break;
        case 3:   // SETTORE C
          if(aeroponica_timer_set == false){
            Serial.print("[AEROPONICA OUT AUTOMATION] Avvio C ");
            Serial.print(aeroponica.out_c_time);
            Serial.println(" s");
            a_aeroponica.set(aeroponica.out_c_time);
            aeroponica_timer_set = true;
          }
          if(a_aeroponica.check()){
            aeroponica_pump.on();
            aeroponica_c.on();
            digitalWrite(pompa_aeroponica, HIGH);
            digitalWrite(settore_3, HIGH);
          }else{
            aeroponica_pump.off();
            aeroponica_c.off();
            digitalWrite(pompa_aeroponica, LOW);
            digitalWrite(settore_3, LOW);
            aeroponica_priority = 4;
            aeroponica_timer_set = false;
          }
          break;
        case 4:   // TEMPO RISPOSO
          if(aeroponica_timer_set == false){
            Serial.print("[AEROPONICA OUT AUTOMATION] Avvio WAIT ");
            Serial.print(aeroponica.out_waiting_time);
            Serial.println(" m");
            a_aeroponica.set(aeroponica.out_waiting_time*60);
            aeroponica_timer_set = true;
          }
          if(a_aeroponica.check()){
            aeroponica_pump.off();
            digitalWrite(pompa_aeroponica, LOW);
          }else{
            aeroponica_priority = 1;
            aeroponica_timer_set = false;
          }
          break;
      }
    }
  }else if(aeroponica_priority>0){
    Serial.println("[AEROPONICA] AUTOMAZIONE INTERROTTA DA APP");
    aeroponica_kill();
  }
}
void aeroponica_kill(){
    aeroponica_priority = 0;
    aeroponica_timer_set = false;
    aeroponica_a.off();
    aeroponica_b.off();
    aeroponica_c.off();
    aeroponica_pump.off();
    digitalWrite(pompa_aeroponica, LOW);
    digitalWrite(settore_1, LOW);
    digitalWrite(settore_2, LOW);
    digitalWrite(settore_3, LOW);
    aeroponica_out_automation = false;
}

Serial monitor: LINK

Anyone know how I can fix this?

What’s in this file, and why are you using this instead of BlynkTimer?

Does this mean that you are using Blynk Legacy, if so, where is this coming from:

and why isn’t it at the top of your code?

What Blynk library version are you using?

How much memory is left when you compile this code?

When your device loses connection, do you need to do a physical reset to get it connected again?

Have you made any modifications to your Ethernet shield (added a capacitor across the reset button etc?

A summary of your serial monitor, showing the data around the disconnections and reconnections and some commentary would be useful (posted here, with triple backticks the same as when you posted code).

Pete.

You should always keep your void loop as clean as possible, check this out
https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

Hi, I rewrote the code as you said, but it keeps going offline. As I understand the problem is the blynk LEDs. The switching on and off of led blynk takes arduino offline. How is it possible?

New code:


#define BLYNK_AUTH_TOKEN    "ccc"
#define BLYNK_PRINT Serial  // Defines the object that is used for printing

#include <SPI.h>
#include <OneWire.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <WidgetRTC.h>              
#include <LiquidCrystal_I2C.h>      
#include <SimpleDHT.h>
#include <DallasTemperature.h>
 
char blynk_token[] = BLYNK_AUTH_TOKEN;
BlynkTimer timer; 

int ReCnctFlag;  // Reconnection Flag

#define BACK  3                     // Pulsante Indietro
#define UP    3                     // Pulsante Superiore
#define DOWN  3                     // Pulsante Inferiore
#define OK    3                     // Pulsante Invio
#define HOME  3                     // Pulsante Home

#define RTC_CLK             21
#define RTC_DAT             21
#define RTC_RST             21
#define DHT_PIN             39

#define temp_cisterna_aeroponica  40                // Sensore temperatura cisterna aeroponica

#define aeroponica_lv_HIGH   46       // Livello medio cisterna aeroponica 500 LITRI
#define aeroponica_lv_LOW    47       // Livello basso cisterna aeroponica 100 LITRI

#define contalitri          34       // Contalitri per registrare consumo aeroponica

#define settore_1           37       // Settore 1 aeroponica
#define settore_2           29       // Settore 3 aeroponica
#define settore_3           36       // Settore 3 aeroponica

#define pompa_aeroponica    35       
#define pompa_fuorisuolo    27
#define pompa_insalate      34

SimpleDHT11 dht11(DHT_PIN);
//LiquidCrystal_I2C lcd(0x27,20,4);   // Configurazione LCD
OneWire oneWire(temp_cisterna_aeroponica);
DallasTemperature cisterna_aeroponica(&oneWire);

WidgetRTC rtc_blynk;
WidgetTerminal nutrient_terminal(V97);
WidgetTerminal terminal(V99);
WidgetLED aeroponica_sleep(V100);
WidgetLED aeroponica_a(V101);
WidgetLED aeroponica_b(V102);
WidgetLED aeroponica_c(V103);
WidgetLED fuorisuolo_pump(V104);
WidgetLED fuorisuolo_riserva(V105);
WidgetLED aeroponica_riserva(V106);
WidgetLED tank_pump(V107);

byte aeroponica_out_priority = 1;
bool aeroponica_timer_set = false;
bool aeroponica_reset = false;
bool aeroponica_out_automation = false;

struct aeroponica{
  float water_use;           // V50 - litri
  int start_automation_time; // V10 - in minuti
  int stop_automation_time;  // V10 - in minuti
  int tank_liters_estimate;  // V51 - litri
  long a_time;               // V30 - in secondi
  long b_time;               // V31 - in secondi
  long c_time;               // V32 - in secondi
  long waiting_time;         // V33 - in minuti
  byte out_a_time = 30;
  byte out_b_time = 30;
  byte out_c_time = 30;
  byte out_waiting_time = 120;
  bool automation_status;    // V20 
  bool drain_status;         // V21
  bool a_status;          
  bool b_status;
  bool c_status;
  bool pump_status;

}aeroponica;
struct fuorisuolo{
  float liters_per_plant;    // V60 - in litri
  int start_automation_time; // V11 - in minuti
  int stop_automation_time;  // V11 - in minuti
  int tank_liters_estimate;  // V52 - in litri
  byte daily_cycles;         // V34
  byte minutes_per_cycle;    // V35 - in minuti
  byte number_plants;        // V36
  byte a_time;               
  byte waiting_time;
  bool automation_status;    // V22
  bool drain_status;         // V23
  bool a_status;
  bool pump_status;
}fuorisuolo;

int minuti_now;
byte aeroponica_priority = 0;
bool aeroponic_timer_run = false;
bool isFirstConnect = true;
int setts; 

BLYNK_CONNECTED() {
  if (isFirstConnect) {
    Blynk.syncAll();
    aeroponica_a.off();
    aeroponica_b.off();
    aeroponica_c.off();
    aeroponica_sleep.off();
    isFirstConnect = false;
    setts = 0;
    Blynk.virtualWrite(V9, setts);
  }
  rtc_blynk.begin();
}

BLYNK_WRITE(V10){    // AEROPONICA: ciclo irrigazion   
  TimeInputParam t(param);
  aeroponica.start_automation_time = (t.getStartHour() * 60) + t.getStartMinute();  
  aeroponica.stop_automation_time  = (t.getStopHour()  * 60) + t.getStopMinute();
  Serial.print("AEROPONICA START TIME : "); 
  Serial.print(t.getStartHour());
  Serial.print(":");
  Serial.println(t.getStartMinute());
  Serial.print("AEROPONICA STOP TIME  : "); 
  Serial.print(t.getStopHour());
  Serial.print(":");
  Serial.println(t.getStopMinute());
}
BLYNK_WRITE(V11){    // FUORISUOLO: ciclo irrigazione
  TimeInputParam t(param);
  fuorisuolo.start_automation_time = (t.getStartHour() * 60) + t.getStartMinute();  
  fuorisuolo.stop_automation_time  = (t.getStopHour()  * 60) + t.getStopMinute();
  Serial.print("FUORISUOLO START TIME : "); 
  Serial.print(t.getStartHour());
  Serial.print(":");
  Serial.println(t.getStartMinute());
  Serial.print("FUORISUOLO STOP TIME  : "); 
  Serial.print(t.getStopHour());
  Serial.print(":");
  Serial.println(t.getStopMinute());
}
BLYNK_WRITE(V20){    // AEROPONICA: automazione
  aeroponica.automation_status = param[0].asInt();
  Serial.print("Aeroponica AUTO   : "); 
  if(aeroponica.automation_status){
    Serial.println("ON");
    aeroponica_timer_set = false;
    aeroponica_priority = 1;
  }
  else{
    Serial.println("OFF"); 
    aeroponica_timer_set = true;  
    aeroponic_timer_run = false;
    digitalWrite(pompa_aeroponica, LOW);
    digitalWrite(settore_1, LOW);
    digitalWrite(settore_2, LOW);
    digitalWrite(settore_3, LOW);
  }
}
/*BLYNK_WRITE(V21){    // AEROPONICA: scarico
  aeroponica.drain_status = param[0].asInt();
  if(aeroponica.automation_status){
    Blynk.virtualWrite(V20, LOW);
    aeroponica.automation_status = LOW;
  }
  Serial.print("Aeroponica DRAIN  : "); 
  if(aeroponica.drain_status){
    Serial.println("ON");
    digitalWrite(pompa_aeroponica, LOW);  
  }
  else{
    Serial.println("OFF");  
    digitalWrite(pompa_aeroponica, HIGH);
  }
}
*/
BLYNK_WRITE(V22){    // FUORISUOLO: automazione
  fuorisuolo.automation_status = param[0].asInt();
  Serial.print("Fuorisuolo AUTO   : "); 
  if(fuorisuolo.automation_status) Serial.println("ON");  
  else Serial.println("OFF");
}
/*BLYNK_WRITE(V23){    // FUORISUOLO: scarico
  fuorisuolo.drain_status = param[0].asInt(); 
  if(fuorisuolo.automation_status){
    Blynk.virtualWrite(V22, LOW);
    fuorisuolo.automation_status = LOW;
  }
  Serial.print("Fuorisuolo DRAIN  : "); 
  if(fuorisuolo.drain_status) Serial.println("ON");  
  else Serial.println("OFF");  
}
*/

BLYNK_WRITE(V29){ // AEROPONICA: sleep notte
  aeroponica.out_waiting_time = param[0].asInt();
  aeroponica.out_waiting_time = aeroponica.out_waiting_time * 1000;
  Serial.print("Aereoponica sleep TIME    : "); Serial.print(aeroponica.out_waiting_time/1000); Serial.println("s");
}
BLYNK_WRITE(V30){ // AEROPONICA: Settore A in secondi
  aeroponica.a_time = param[0].asInt();
  aeroponica.a_time = aeroponica.a_time * 1000;
  Serial.print("Aereoponica A TIME    : "); Serial.print(aeroponica.a_time/1000); Serial.println("s");
}
BLYNK_WRITE(V31){ // AEROPONICA: Settore B in secondi
  aeroponica.b_time = param[0].asInt();
  aeroponica.b_time = aeroponica.b_time * 1000;
  Serial.print("Aereoponica B TIME    : "); Serial.print(aeroponica.b_time/1000); Serial.println("s");
}
BLYNK_WRITE(V32){ // AEROPONICA: Settore C in secondi
  aeroponica.c_time = param[0].asInt();
  aeroponica.c_time = aeroponica.c_time * 1000;
  Serial.print("Aereoponica C TIME    : "); Serial.print(aeroponica.c_time/1000); Serial.println("s");
}
BLYNK_WRITE(V33){ // AEROPONICA: Riposo in minuti
  aeroponica.waiting_time = param[0].asInt();
  aeroponica.waiting_time = (aeroponica.waiting_time*60)*1000;
  Serial.print("Aereoponica WAIT TIME : "); Serial.print(aeroponica.waiting_time/1000); Serial.println("s");
}
BLYNK_WRITE(V34){ // FUORISUOLO: Cicli giornalieri
  fuorisuolo.daily_cycles = param[0].asInt();
  Serial.print("Fuorisuolo CICLIxDAY  : "); Serial.println(fuorisuolo.daily_cycles); 
}
BLYNK_WRITE(V35){ // FUORISUOLO: Irrigazione per ciclo
  fuorisuolo.minutes_per_cycle = param[0].asInt();
  Serial.print("Fuorisuolo IRRIGAZIONE: "); Serial.print(fuorisuolo.minutes_per_cycle); Serial.println("m");
}
BLYNK_WRITE(V36){ // FUORISUOLO: Irrigazione per ciclo
  fuorisuolo.number_plants = param[0].asInt();
  Serial.print("Fuorisuolo PIANTE     : "); Serial.println(fuorisuolo.number_plants);
}


void setup(){
  Serial.begin(9600);                 // Inizializzazione seriale    
  cisterna_aeroponica.begin();
 /*
  pinMode(UP,   INPUT_PULLUP);
  pinMode(DOWN, INPUT_PULLUP);
  pinMode(OK,   INPUT_PULLUP);
  pinMode(BACK, INPUT_PULLUP);
  pinMode(HOME, INPUT_PULLUP);
*/
  pinMode(aeroponica_lv_LOW, INPUT);
  pinMode(aeroponica_lv_LOW, INPUT);

  pinMode(settore_1, OUTPUT);
  pinMode(settore_2, OUTPUT);
  pinMode(settore_3, OUTPUT);
  pinMode(pompa_aeroponica, OUTPUT);
  pinMode(pompa_fuorisuolo, OUTPUT);
  pinMode(pompa_insalate, OUTPUT);

  digitalWrite(settore_1, LOW);
  digitalWrite(settore_2, LOW);
  digitalWrite(settore_3, LOW);
  digitalWrite(pompa_aeroponica, LOW);
  digitalWrite(pompa_fuorisuolo, LOW);
  digitalWrite(pompa_insalate, LOW);

  Blynk.begin(blynk_token);
    
  aeroponica.tank_liters_estimate = 450;
  fuorisuolo.tank_liters_estimate = 110;

  check_aeroponica_lv();
  check_fuorisuolo_lv();
  check_dht();
  check_probes();

  aeroponica_sleep.off();
  aeroponica_a.off();
  aeroponica_b.off();
  aeroponica_c.off();
  
  // MAX 16 TIMER 
  timer.setInterval(600500L,  check_probes);
  timer.setInterval(301500L,  check_aeroponica_lv); // 60000L
  timer.setInterval(300500L,  check_fuorisuolo_lv);
  timer.setInterval(30000L,   reconnectBlynk);
  timer.setInterval(1000L,    aeroponica_rtc);
  
  Serial.println("[!] SETUP TERMINATO");
  Serial.println(""); Serial.println("");
}
void loop(){
  Blynk.run();
  timer.run();  
}

void check_aeroponica_lv(){
  Serial.println("CHECK AEROPONICA LV");
  if(digitalRead(aeroponica_lv_LOW) == HIGH){ // 28 digital
    aeroponica_riserva.on();
    Blynk.virtualWrite(90, HIGH);
  }else{
    aeroponica_riserva.off();
    Blynk.virtualWrite(90, LOW);
  }
  
  if(digitalRead(aeroponica_lv_LOW) == LOW and digitalRead(aeroponica_lv_HIGH) == LOW)        Blynk.virtualWrite(V49, 1000);
  else if(digitalRead(aeroponica_lv_LOW) == LOW and digitalRead(aeroponica_lv_HIGH) == HIGH)  Blynk.virtualWrite(V49, 500);
  else if(digitalRead(aeroponica_lv_LOW) == HIGH and digitalRead(aeroponica_lv_HIGH) == HIGH) Blynk.virtualWrite(V49, 100);
}

void check_fuorisuolo_lv(){
  fuorisuolo_riserva.off(); 
  /*
  if(digitalRead(fuorisuolo_lv_LOW) == HIGH){ // 30 digital
    fuorisuolo_riserva.on();
    Blynk.virtualWrite(91, HIGH);
  }else{
    fuorisuolo_riserva.off();     
    Blynk.virtualWrite(91, LOW);                                                                             
  }
  Blynk.virtualWrite(V52, fuorisuolo.tank_liters_estimate);
  */
}

void check_dht(){
}
void check_probes(){
  byte temperature = 0;
  byte humidity = 0;  
  int err = SimpleDHTErrSuccess;
  float temp;
  
  cisterna_aeroponica.requestTemperatures();
  temp = cisterna_aeroponica.getTempCByIndex(0);
  if(temp<50 && temp>-10){
    Serial.print("TEMP cisterna aeroponica: ");
    Serial.println(temp);
    Blynk.virtualWrite(V4, temp); // cisterna
  }
  if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
    Serial.print("Read DHT11 failed, err="); Serial.println(err);
  }else{
    Serial.print((int)temperature); Serial.print(" *C, "); 
    Serial.print((int)humidity);    Serial.println(" H");
    Blynk.virtualWrite(V1, (int)temperature);  
    Blynk.virtualWrite(V2, (int)humidity);    
  }
}


void rtcnow(){
  unsigned long ore = hour();
  unsigned long minuti = minute();
  unsigned long secondi = second();
  minuti_now = (ore*60)+(minuti);
}

void aeroponica_rtc(){
  if(aeroponica.automation_status){
    rtcnow();
    if(minuti_now >= aeroponica.start_automation_time and minuti_now <= aeroponica.stop_automation_time){
      switch(aeroponica_priority){
        case 1:
          if(!aeroponica_timer_set && aeroponica.a_time>0){
            Serial.print("[AEROPONIC] Start A for ");
            Serial.print(aeroponica.a_time/1000);
            Serial.println("s");
            digitalWrite(pompa_aeroponica, HIGH);
            digitalWrite(settore_1, HIGH);
            //aeroponica_a.on();
            //aeroponica_sleep.off();
            timer.setTimeout(aeroponica.a_time, [](){
              digitalWrite(pompa_aeroponica, LOW);
              digitalWrite(settore_1, LOW);
              //aeroponica_a.off();
              aeroponica_priority++;
              aeroponica_timer_set = false;
            });
            aeroponica_timer_set = true;
          }
        break;
        case 2:
          if(!aeroponica_timer_set && aeroponica.b_time>0){
            Serial.print("[AEROPONIC] Start B for ");
            Serial.print(aeroponica.b_time/1000);
            Serial.println("s");
            digitalWrite(pompa_aeroponica, HIGH);
            digitalWrite(settore_2, HIGH);
            //aeroponica_a.off();
            //aeroponica_b.on();
            timer.setTimeout(aeroponica.b_time, [](){
              digitalWrite(pompa_aeroponica, LOW);
              digitalWrite(settore_2, LOW);
              //aeroponica_b.off();
              aeroponica_priority++;
              aeroponica_timer_set = false;
            });
            aeroponica_timer_set = true;
          }
        break;
        case 3:
          if(!aeroponica_timer_set && aeroponica.c_time>0){
            Serial.print("[AEROPONIC] Start C for ");
            Serial.print(aeroponica.c_time/1000);
            Serial.println("s");
            digitalWrite(pompa_aeroponica, HIGH);
            digitalWrite(settore_3, HIGH);
            //aeroponica_a.off();
            //aeroponica_b.off();
            //aeroponica_c.on();
            timer.setTimeout(aeroponica.c_time, [](){
              digitalWrite(pompa_aeroponica, LOW);
              digitalWrite(settore_3, LOW);
              //aeroponica_c.off();
              aeroponica_priority++;
              aeroponica_timer_set = false;
            });
            aeroponica_timer_set = true;
          }
        break;
        case 4:
          if(!aeroponica_timer_set && aeroponica.waiting_time>0){
            Serial.print("[AEROPONIC] Start WAIT for ");
            Serial.print(aeroponica.waiting_time/1000);
            Serial.println("s");
            //aeroponica_a.off();
            //aeroponica_b.off();
            //aeroponica_c.off();
            aeroponica_sleep.on();
            digitalWrite(pompa_aeroponica, LOW);
            setts = aeroponica.waiting_time/1000;
            setts--;
            Blynk.virtualWrite(V9, setts);
            timer.setTimer(1000L, timer_flow, setts);
            timer.setTimeout(aeroponica.waiting_time, [](){
              aeroponica_priority = 1;
              aeroponica_timer_set = false;
              Blynk.virtualWrite(V9, 0);
              aeroponica_sleep.off();
            });
            aeroponica_timer_set = true;
          }
        break;
      }
    }
  }
}
void aeroponica_kill(){
    aeroponica_priority = 0;
    aeroponica_timer_set = false;
    aeroponica_a.off();
    aeroponica_b.off();
    aeroponica_c.off();
    aeroponica_sleep.off();
    digitalWrite(pompa_aeroponica, LOW);
    digitalWrite(settore_1, LOW);
    digitalWrite(settore_2, LOW);
    digitalWrite(settore_3, LOW);
    aeroponica_out_automation = false;
}
void reconnectBlynk() {
  if (!Blynk.connected()) {
    if(Blynk.connect()) {
     BLYNK_LOG("Reconnected");
    } else {
      BLYNK_LOG("Not reconnected");
    }
  }
}
void timer_flow(){
  if(aeroponica.automation_status){
    Blynk.virtualWrite(V9, setts--);
  }
}

I can’t help unless you answer my questions.

Pete.

With the new code, which I wrote in the old post, I deleted this file and used the blynk timers.

1.0.1

The sketch uses 39116 bytes (15%) of the space available for programs. The maximum is 253952 bytes.
Global variables use 1926 bytes (23%) of dynamic memory, leaving an additional 6266 bytes free for local variables

no, it resets itself automatically. I wrote the reconnect function

no

You haven’t answered this question.
You’ve deleted the template ID from your latest post, but I still don’t know if this is a Legacy or an IoT sketch.

You need to give me a clue about what exactly this means.

Pete.

No it was a mistake

I have commented on the lines that cause the problem in the aeroponic_rtc procedure.

For example:
aeroponica_a.on();
When this happens, arduino goes offline

So, once again, you haven’t stated clearly if you are using Blynk Legacy, or Blynk IoT.

I assume from this comment:

that you are using Blynk Legacy.
If that is the case then you should be using Blynk library version 0.6.1, not 1.0.1

If this is a long term project then I’d highly recommend that you migrate it to Blynk IoT before the Legacy product is decommissioned.

To be honest, the way that you’ve structured your code and the fact that you have lots of unused code and unnecessary libraries scatters across your two code versions make it extremely difficult to follow.

Personally, I’ve never liked the WidgetLED method.
I’ve always preferred to use the Blynk.Write(vPin,255) or Blynk.Write(vPin,0) methods of turning an LED widget on and off.

Pete.