Esp32 stop to work and send data after 2 or 3 hours

Hey guys
I´m have some trouble to fix a error
the code works fine but afer a few hours the esp 32 stop working and change the state of my relay
the code is:


#define BLYNK_PRINT Serial

#include <Adafruit_Sensor.h>
#include <SPI.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <ThingSpeak.h>
#include <DHT.h>
#include <DHT_U.h>
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <WidgetRTC.h>
#include <TimeLib.h>

//const char* server = "api.thingspeak.com";
BlynkTimer timer;
WiFiClient client;

#define CHANNEL_ID ########
#define CHANNEL_API_KEY "##########"

#define BLINK_ID "#######"//JOAO



char ssid[] = "#####";
char pass[] = "32734755";

int nivel = 25;
int valueNivel = 0;

const int oneWireBus = 4;  //Sensor de temperatura da agua
OneWire oneWire(oneWireBus);
DallasTemperature sensors(&oneWire);

int Photo = 33;  //Sensor de Luz canal digital
int valuePhoto = 0;

#define DHTTYPE DHT22  //Sensor Umidade do AR e Temperatura
#define DHTPIN 19
DHT dht(DHTPIN, DHT22);
float localHum = 0;
float localTemp = 0;

WidgetRTC rtc;
WidgetTerminal terminal(V4);
int switchState;

int rele1 = 32; //Luz Posiçao 2
int rele2 = 26; // Bomba Posiçao 3
int rele3 = 18; //Exautao Posiçao 4

int horas = hour();

int valor = LOW; // Ver. se luz esta apagada

BLYNK_CONNECTED() {
  // Synchronize time on connection
  rtc.begin();
}

BLYNK_WRITE(V6){
  switchState = param.asInt();
  getLUX();
}

void setup() {  
  
  Serial.begin(115200);
  ThingSpeak.begin(client);  // Initialize ThingSpeak
  
  pinMode(rele1, OUTPUT);                  // definições das portas IN1 e IN2 como portas de saidas 
  pinMode(rele2, OUTPUT);
  pinMode(rele3, OUTPUT);  
  pinMode(Photo, INPUT);
  pinMode(nivel, INPUT);  
  digitalWrite(rele1, valor);               // desativa porta IN1 
  digitalWrite(rele2, HIGH);
  digitalWrite(rele3, HIGH);
    
  dht.begin();

  Wire.begin(21,22);  //sda Roxo scl Cinza
  delay(3000);
  
  timer.setInterval(30100L, getDHT); 
  timer.setInterval(30200L, getTempagua);
  timer.setInterval(30300L, clockDisplay);
  timer.setInterval(30400L, getNivel);
  timer.setInterval(30500L, getPhoto);
  timer.setInterval(30600L, getLUX);
  timer.setInterval(21600000L, getRestart);// reseta em 12 horas

  
  terminal.clear();
  
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, pass);
  int wifi_ctr = 0;
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("WiFi connected");  
  Blynk.begin(BLINK_ID, ssid, pass);

}

void loop(){
  Blynk.run();
  timer.run();  
}
    
void getTempagua(){ // temp checks
   
  sensors.requestTemperatures();//Colete informações de temperatura  
  float tempAgua = sensors.getTempCByIndex(0);//Obtenha a temperatura em graus Celsius 
  Serial.print("Temp. agua: ");
  Serial.print(tempAgua);
  Serial.println(" "); 
  Blynk.virtualWrite(V0, tempAgua);//Atualize variaveis Blynk com os valores de temperatura

  ThingSpeak.setField(3,tempAgua);
  ThingSpeak.writeFields(CHANNEL_ID,CHANNEL_API_KEY);
  
}

void getPhoto(){ // check the light status with ldr sensor
  
  valuePhoto = digitalRead(Photo);
  if(valuePhoto == 1){ // say off
      Blynk.virtualWrite(V1, "Desligada"); 
  }
  else{ // say on
      Blynk.virtualWrite(V1, "Ligada"); //Luz desligada 
  }
  Serial.print("Luz (0 Ligada; 1 Desligada: )");
  Serial.print(valuePhoto);
  Serial.println(" ");
  terminal.print("Luz (0 Ligada; 1 Desligada: )");
  terminal.print(valuePhoto);
  terminal.println(" ");
  terminal.flush();
}

void getDHT(){ // temp and humi sensor . turn on and off the exhaust fan 
  float tempIni = localTemp;
  float humIni = localHum;
  
  localTemp = dht.readTemperature();                                                                      
  localHum = dht.readHumidity();
  
  Serial.print("TEMPERATURA: ");
  Serial.print(localTemp);
  Serial.println("C");
  Serial.print("UMIDADE: ");
  Serial.print(localHum);
  Serial.println("%");
  
  if(localTemp > 26 || localHum > 50){ // turn on if the temp is higher than 26 C and/or the hum is higher than 50%
      digitalWrite(rele3, HIGH);      
  }
  else{ // turn on if not
      digitalWrite(rele3, LOW);
  }
  
  Blynk.virtualWrite(V2, localTemp);  // write blynk
  Blynk.virtualWrite(V3, localHum); // write blynk

  ThingSpeak.setField(1,localHum); //set the channel 1 to say the hum value
  ThingSpeak.setField(2,localTemp); //set the channel 2 to say the tem value
  ThingSpeak.writeFields(CHANNEL_ID,CHANNEL_API_KEY); // write on thingspeak
}

void clockDisplay(){

  String currentTime = String(hour()) + ":" + minute() + ":" + second();
  String currentDate = String(day()) + " " + month() + " " + year();
  
  Serial.print("Hora e data: ");
  Serial.print(currentTime);
  Serial.print(" ");
  Serial.print(currentDate);
  Serial.println();
  terminal.print("Hora e data: ");
  terminal.print(currentTime);
  terminal.print(" ");
  terminal.println(currentDate);
  terminal.flush();

  Blynk.virtualWrite(V4, currentTime); // write blynk
}

void getLUX() { // Lê a condiçao da selecao de 4 opcoes no app  
  Serial.print("verificar horas : ");
  Serial.print(hour());
  Serial.println();
  if (switchState == 1){ // select working hours 
     
     if(hour() == 6 && hour()<= 18 ){// working hours 6 am to 6 pm; 12 hours cycle
        valor = LOW;
        digitalWrite(rele1, valor); 
     }
     else{
        valor = HIGH;
        digitalWrite(rele1, valor);
     }
  }
       
  if (switchState == 2){  // select working hours
     
     clockDisplay();
     
     if(hour() == 5 && hour() <= 23 ){//working hours 5 am to 11 pm; 18 hours cycle
        valor = LOW;
        digitalWrite(rele1, valor);        
     }
     else{
        valor = HIGH;
        digitalWrite(rele1, valor);
     }
   }
    
  if (switchState == 3){ // turn off the light
     
     valor = HIGH;
     digitalWrite(rele1, valor);
  }
  
  if (switchState == 4){ // turn on the light
     
     valor = LOW;
     digitalWrite(rele1, valor);
   }
     
  ThingSpeak.setField(4,valor); //set the channel 4 to say the relay status
  ThingSpeak.writeFields(CHANNEL_ID,CHANNEL_API_KEY);// write on thingspeak
  Serial.print("Verificar estado do Rele da luz (0 Ligada; 1 Desligada) : ");
  Serial.print(valor);
  Serial.println();
  terminal.print("Verificar estado do Rele da luz;;/; (0 Ligada; 1 Desligada) : ");
  terminal.print(valor);
  terminal.println(" ");

  terminal.flush();
     
}

void getNivel(){ //turn off the pump if water is low
  
  valueNivel = digitalRead(nivel);
  if(valueNivel == 0){
    digitalWrite(rele2, HIGH);
    Blynk.virtualWrite(V7, "Ligada");
  }
  else{ 
    digitalWrite(rele2, LOW);
    Blynk.virtualWrite(V7, "Desligada");
  }
  Serial.print("Agua (0 Ligada; 1 Desligada) : ");
  Serial.print(valueNivel); 
  Serial.println();
  terminal.print("Agua (0 Ligada; 1 Desligada) : ");
  terminal.print(valueNivel); 
  terminal.println(" ");

  terminal.flush();
}

void getRestart(){ // restart esp32
  Serial.println("Restarting ");
  ESP.restart();
}

the trouble are when the segment state are set in the app and suddenly change the relay state.
Look like this
i turn on the esp 32 and the switch state are press 1 on the app. Work fine for 2 or 3 hours and suddenly the light turn off when should be on.
the operating period is 12 hours on and 12 hours off on the case 1.
I’m not sure what i did wrong.


void getLUX() { // Lê a condiçao da selecao de 4 opcoes no app  
  Serial.print("verificar horas : ");
  Serial.print(hour());
  Serial.println();
  if (switchState == 1){ // select working hours 
     
     if(hour() == 6 && hour()<= 18 ){// working hours 6 am to 6 pm; 12 hours cycle
        valor = LOW;
        digitalWrite(rele1, valor); 
     }
     else{
        valor = HIGH;
        digitalWrite(rele1, valor);
     }
  }
       
  if (switchState == 2){  // select working hours
     
     clockDisplay();
     
     if(hour() == 5 && hour() <= 23 ){//working hours 5 am to 11 pm; 18 hours cycle
        valor = LOW;
        digitalWrite(rele1, valor);        
     }
     else{
        valor = HIGH;
        digitalWrite(rele1, valor);
     }
   }
    
  if (switchState == 3){ // turn off the light
     
     valor = HIGH;
     digitalWrite(rele1, valor);
  }
  
  if (switchState == 4){ // turn on the light
     
     valor = LOW;
     digitalWrite(rele1, valor);
   }
     
  ThingSpeak.setField(4,valor); //set the channel 4 to say the relay status
  ThingSpeak.writeFields(CHANNEL_ID,CHANNEL_API_KEY);// write on thingspeak
  Serial.print("Verificar estado do Rele da luz (0 Ligada; 1 Desligada) : ");
  Serial.print(valor);
  Serial.println();
  terminal.print("Verificar estado do Rele da luz;;/; (0 Ligada; 1 Desligada) : ");
  terminal.print(valor);
  terminal.println(" ");

  terminal.flush();
     
}

can u guys help me?

Is the 100ms difference between functions really enough to get everything done before going onto next?

You are probably running into some overlapping issues as time goes on.