ESP32 Restart When disconnected

Dear Blynk Community!

I need help with my simple code. I would like to implement an auto restart feature when my ESP is disconnected from the server for longer than let’s say 3 minutes. Normally I would just hardcode a restart using millis function regardless for the connection status. However, for this project each time pin 15 is pulled High (and for some reason it is on restart) the bypass relay latches on disconnecting my bedroom lights from the main circuit. For obvious reasons I don’t want my lights blinking every few minutes and therefore I would like to execute the restart function only when ESP is offline for longer than 3 minutes or so.

Thanks !

#define BLYNK_PRINT Serial


#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "XX";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "XX";
char pass[] = "XX";

int RlyPin = 15;
int RlyState = LOW;
uint8_t led1 = 17;
int PWM_LED1;
int PWM_Count = 70; // Digital value needed to latch the relay (0-4095)


void setup()
{
  pinMode(RlyPin, OUTPUT);
ledcAttachPin(led1, 1);
ledcSetup(1, 12000, 12);
  
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
 
}
BLYNK_WRITE(V0) //slider in Virtual Pin 0 (0...4095)
{
 PWM_LED1 = param.asInt();
 ledcWrite(1, PWM_LED1); // 0-4095
 float voltage1 = map(PWM_LED1, 0, 4095, 0, 330);
 //Blynk.virtualWrite(V?, voltage1/100); Future voltage read 
}

void loop()
{
  if (PWM_LED1 >= PWM_Count) {
    RlyState = HIGH;
    digitalWrite(RlyPin, RlyState);
    Blynk.virtualWrite(V3, 4095); //turn on the widget on V3
  } else {
    RlyState = LOW;
    digitalWrite(RlyPin, RlyState);
    Blynk.virtualWrite(V3, 0);
  }
    
  
  Blynk.run();
}

Your code breaks all the Blynk coding rules, by having a cluttered void loop and more importantly Blynk.virtualWrites in the void loop.

It’s not surprising that you’re getting frequent disconnections.

You should read this and fix your code structure…

Pete.

Thanks for the reply. I cleaned up the code, having nothing but Blynk.run() and timer.run(); in the loop.
Coming back to the original question; Is there a way to execute a hardware restart when the device is offline for longer then x seconds? I appreciate any feedback, thanks.

Yes, there are a number of ways to do that, but why restartthe device when you could simply reconnect?

If you search this forum you’ll find lots of topics regarding way to reconnect if a disconnection occurs.

Pete.