ESP32 timer gets disconnect all the time... Don't know what to do

Hi There.
I’m trying to make a timer to switch appliance on and off with specific time.
But unfortunally it disconnects all the time. Here’s the code, i hope you can guide me.

Thank You.
Marc.

Code:

#define BLYNK_PRINT Serial

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#include <SimpleTimer.h>

#include <ESPmDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include “Update.h”

SimpleTimer sendTimeDate;
SimpleTimer checkForTime;
SimpleTimer FeedTheDog;
SimpleTimer timer;

int t1;

WidgetRTC rtc;

unsigned long previousMillis = 0; // will store last time LED was updated

// constants won’t change :
const long interval = 200; // interval at which to blink (milliseconds)

boolean appareilEstAOff = false;

#define BLYNK_GREEN “#23C48E
#define BLYNK_BLUE “#04C0F8
#define BLYNK_YELLOW “#ED9D00
#define BLYNK_RED “#D3435C
#define BLYNK_DARK_BLUE “#5F7CD8

String CLOSE_LOCK = “\xF0\x9F\x94\x92”;
String OPEN_LOCK = “\xF0\x9F\x94\x93”;

String gaugeColor;

int prevState = -1;
int currState = -1;
long lastChangeTime = 0;

const int LED = GPIO_NUM_4;
const int SWITCH_PIN = GPIO_NUM_13;
const int RELAIS_PIN = GPIO_NUM_12;

const int PIN_TO_RESET = GPIO_NUM_23;

boolean hasStartTime, hasStopTime = false;
boolean enModeProgrammation = false;

char auth[] = “88138d69b16f4721aeec845421e3cbf6”;

const char* ssid = “VIDEOTRON2944”;
const char* pass = “K7E97YHEA9JAM”;

int heureDebut, minuteDebut;
int heureFin, minuteFin;

// Digital clock display of the time
void clockDisplay() {
// You can call hour(), minute(), … at any time
// Please see Time library examples for details

char bufferTime[15];
char bufferDate[10];
sprintf(bufferTime, “%02d:%02d%”, hour(), minute());
sprintf(bufferDate, “%02d/%02d/%d”, day(), month(), year());

Blynk.virtualWrite(V3, bufferTime);
Blynk.virtualWrite(V4, bufferDate);
Blynk.setProperty(V7, “label”, “\xE2\x8F\xB0”);
}

/*
void checkPin() {
// Invert state, since button is “Active LOW”
int state = !digitalRead(SWITCH_PIN);

// Debounce mechanism
long t = millis();
if (state != prevState) {
lastChangeTime = t;
}

if (t - lastChangeTime > 50) {
if (state != currState) {
currState = state;

  if (state == 0)
    led1.off();
  else
    led1.on();
}

}
prevState = state;
}
*/

/*
// This function sends Arduino’s up time every second to Virtual Pin (5).
// In the app, Widget’s reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendTemperature()
{
// Generate random temperature value 10.0 to 30.0 (for example)
float t = float(random(100, 300)) / 10;

// Format: 1 decimal place, add ℃
String str = String(t, 1) + “℃”;

// Send it to the server
Blynk.virtualWrite(V5, str);
}
*/

BLYNK_WRITE(V2) {
TimeInputParam t(param);

// Process start time
if (t.hasStartTime())
{
hasStartTime = true;

Serial.println(String("Start: ") +
               t.getStartHour() + ":" +
               t.getStartMinute() + ":" +
               t.getStartSecond());

heureDebut  = t.getStartHour();
minuteDebut = t.getStartMinute();

}
else
{
// Do nothing
}

// Process stop time
if (t.hasStopTime())
{
hasStopTime = true;

Serial.println(String("Stop: ") +
               t.getStopHour() + ":" +
               t.getStopMinute() + ":" +
               t.getStopSecond());

heureFin  = t.getStopHour();
minuteFin = t.getStopMinute();

}

char bufferStartTime[30];
char bufferStopTime[30];
sprintf(bufferStartTime, “Hr debut : %.02d:%.02d”, heureDebut, heureFin);
sprintf(bufferStopTime, “Hr fin : %.02d:%.02d”, heureFin, minuteFin);
Serial.println();
}

void checkApplicances() {
if (hasStartTime && hasStopTime) { // valeurs présentes…
if (hour() == heureDebut && minute() == minuteDebut && second() == 0) {
Blynk.notify(“Demarrage du timer!”);
digitalWrite(LED, HIGH);
digitalWrite(RELAIS_PIN, HIGH);
Blynk.virtualWrite(V8, 1);
Blynk.setProperty(V8, “label”, “OUVERT”);
Blynk.setProperty(V8, “color”, BLYNK_RED);
Blynk.virtualWrite(V10, OPEN_LOCK);
}

if (hour() == heureFin && minute() == minuteFin && second() == 0) {
  // if (appareilEstAOff) {
  Blynk.notify("Fin du timer!");
  digitalWrite(LED, LOW);
  digitalWrite(RELAIS_PIN, LOW);
  Blynk.virtualWrite(V8, 0);
  Blynk.setProperty(V8, "label", "FERME");
  Blynk.setProperty(V8, "color", BLYNK_GREEN);
  Blynk.virtualWrite(V10, CLOSE_LOCK);
  //}
}

}
}

boolean wasEnabled, timerIsDeleted;

// MENU
BLYNK_WRITE(V7) {
switch (param.asInt())
{
case 1: // Item 1
Serial.println(“MODE PROGRAMMATION : ON”);
checkForTime.enable(t1);
break;

case 2: // Item 2
  Serial.println("MODE PROGRAMMATION : OFF");
  checkForTime.disable(t1);
  break;

/*
    case 3: // Item 3
      Serial.println("STOP TIMER!");
      wasEnabled = checkForTime.isEnabled(t1);
      if (wasEnabled) {
        checkForTime.deleteTimer(t1);
        timerIsDeleted = true;
      }
      break;
*/
default:
  Serial.println("Unknown item selected");
  break;

}
}

// setup
void setup() {
Serial.begin(115200);
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
pinMode(RELAIS_PIN, OUTPUT);
digitalWrite(RELAIS_PIN, LOW);
pinMode(PIN_TO_RESET, OUTPUT);
digitalWrite(PIN_TO_RESET, LOW);

//setup_wifi1();
Blynk.begin(auth, ssid, pass);

/*
while (Blynk.connect() == false) {
// Wait until connected
}
*/

sendTimeDate.setInterval(60000L, clockDisplay); // Update time each minute…

t1 = checkForTime.setInterval(1000L, checkApplicances);

FeedTheDog.setInterval(60000L, feed_da_dog); // 60 000 = 1 minutes * 2 = 2 minutes = 120000L(ong)
timer.enable(t1);

rtc.begin();

// Port defaults to 3232
ArduinoOTA.setPort(3232);

// Hostname defaults to esp3232-[MAC]
ArduinoOTA.setHostname(“ESP32_TIMER_MPE”);

// No authentication by default
ArduinoOTA.setPassword(“MPE”);

// Password can be set with it’s md5 value as well
// MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
// ArduinoOTA.setPasswordHash(“21232f297a57a5a743894a0e4a801fc3”);

ArduinoOTA.onStart( {
String type;
if (ArduinoOTA.getCommand() == U_FLASH)
type = “sketch”;
else // U_SPIFFS
type = “filesystem”;

// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
Serial.println("Start updating " + type);

});
ArduinoOTA.onEnd( {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf(“Progress: %u%%\r”, (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println(“Auth Failed”);
else if (error == OTA_BEGIN_ERROR) Serial.println(“Begin Failed”);
else if (error == OTA_CONNECT_ERROR) Serial.println(“Connect Failed”);
else if (error == OTA_RECEIVE_ERROR) Serial.println(“Receive Failed”);
else if (error == OTA_END_ERROR) Serial.println(“End Failed”);
});

ArduinoOTA.begin();
Serial.println(“Ready”);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());

// Tweet immediately on startup
// Blynk.tweet(“My Arduino project is tweeting using @blynk_app and it’s awesome!\n #arduino #IoT #blynk”);
// Blynk.tweet(“My Arduino project is tweeting using @blynk_app and it’s awesome!\n #arduino #IoT #blynk”);
// attachInterrupt(digitalPinToInterrupt(SWITCH_PIN), checkPin, CHANGE);
}

void feed_da_dog() {
digitalWrite(PIN_TO_RESET, HIGH);
delay(20);
digitalWrite(PIN_TO_RESET, LOW);
}

// loop
void loop() {
Blynk.run();

ArduinoOTA.handle();
///////////////////////////
feed_da_dog();
sendTimeDate.run();
checkForTime.run();
///////////////////////////
}

BLYNK_APP_DISCONNECTED() {
Serial.println(“App Disconnected.”);
}

/*
boolean isFirstConnected = true;
BLYNK_CONNECTED() {
if (isFirstConnected) {
// digitalWrite(LED, LOW);
// digitalWrite(RELAIS_PIN, LOW);
Blynk.syncAll();
isFirstConnected = false;
}
}
*/

/*
// This is called when Smartphone App is opened
BLYNK_APP_CONNECTED() {
Serial.println(“App Connected.”);
}

// This is called when Smartphone App is closed
BLYNK_APP_DISCONNECTED() {
Serial.println(“App Disconnected.”);
}
*/

/*
// PROXIMITY SENSOR
BLYNK_WRITE(V9) {
int state = param.asInt();

if (state == 0) {
Serial.println(“FAR”);
}
else if (state == 1) {
Serial.println(“NEAR”);
}
}
*/

/*

BLYNK_WRITE(V8) {
BLYNK_LOG(“Got a value: %s”, param.asStr());

int value = param.asInt();
if (value == 0) {
Serial.println(“OFF”);
appareilEstAOff = true;
Blynk.setProperty(V8, “label”, “FERME”);
Blynk.setProperty(V8, “color”, BLYNK_GREEN);
Blynk.virtualWrite(V10, CLOSE_LOCK);
digitalWrite(LED, LOW);
digitalWrite(RELAIS_PIN, LOW);
}
else if (value == 1) {
Serial.println(“ON”);
appareilEstAOff = false;
Blynk.setProperty(V8, “label”, “OUVERT”);
Blynk.setProperty(V8, “color”, BLYNK_RED);
Blynk.virtualWrite(V10, OPEN_LOCK);

digitalWrite(LED, HIGH);
digitalWrite(RELAIS_PIN, HIGH);

}
}

I would say Welcome to the Forum, but it appears you have been here awhile :stuck_out_tongue_winking_eye: so I don’t suppose we could start with you editing you post and formatting your pasted code as per the Welcome Topic that you might have missed reading along the way? :wink:

Blynk - FTFC

Then, once we can see your code clearer, we can work on using timers instead of running your functions directly in the void loop() as they want to run hundreds of times a second there and can cause those disconnections.