ESP32 disconnect/reconnect randomly per day

Dear all, the ESP32S is driving me crazy, over 2 weeks the ESP32S i have connected it to some lighting and control them by Blynk & physical switch, but many times per day the ESP32 disconnect from internet and reconnect again which results in making all my light go from off to on, and i have to make them off again, am not able to maintain a reliable wifi connection.
By the way my sketch contain 2 modes as shown below, 1st mode to use blynk + physical switch in case of having internet connection & have actual feedback on mobile application from the physical button, and 2nd mode to use only physical switch in case of no internet connection.
i have been searching all over internet more than 2 weeks and i have done every thing but with out solution for example:

1- i have used different power source 2A with regulator 5V to VIN for powering the ESP32 and another source for my output relays with common ground
2- i have put capacitor 47mF on the EN to ground to smooth the volt 3.3 on the internal regulator and avoid any noise
3- i have changed all my GPIO & removed some outputs to avoid using ADC2 pins which is used during wifi as per the ESP32 data sheet
4- My router connection is very good as all my other devices PC and mobile working without any issue, even i was having internet calls on my mobile during the ESP32 wifi disconnect/reconnect issue happens without any problem in my mobile call + the ESP32 is very near to the router less than 5 meters without walls

please can u help me to find a solution for such frustrating issue really i do not know the reason for this…

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

#define BLYNK_PRINT Serial

#define DEBUG_SW 0

#define S1 23
#define R1 19  // old 15 

#define S2 17
#define R2 16

#define S3 34
#define R3 21   // old 4

#define S4 35
#define R4 22

#define S5 32
#define R5 18

#define S6 33
#define R6 5

int MODE = 0;

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

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxx";
char server[] = "blynk-cloud.com";  // URL for Blynk Cloud Server
int port = 8080;

BlynkTimer timer;

int switch_ON_Flag1_previous_I = 0;
int switch_ON_Flag2_previous_I = 0;
int switch_ON_Flag3_previous_I = 0;
int switch_ON_Flag4_previous_I = 0;
int switch_ON_Flag5_previous_I = 0;
int switch_ON_Flag6_previous_I = 0;

BLYNK_WRITE(V1)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  digitalWrite(R1, pinValue);
  // process received value
}

BLYNK_WRITE(V2)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V2 to a variable
  digitalWrite(R2, pinValue);
  // process received value
}

BLYNK_WRITE(V3)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V3 to a variable
  digitalWrite(R3, pinValue);
  // process received value
}

BLYNK_WRITE(V4)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V4 to a variable
  digitalWrite(R4, pinValue);
  // process received value
}

BLYNK_WRITE(V5)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V4 to a variable
  digitalWrite(R5, pinValue);
  // process received value
}

BLYNK_WRITE(V6)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V4 to a variable
  digitalWrite(R6, pinValue);
  // process received value
}

BLYNK_WRITE(V30)
{
  digitalWrite(R1, param.asInt()); // take the state of the Timer and send it to the pin
 Blynk.virtualWrite(V1, param.asInt()); // Send timer state back to button to update it.
 digitalWrite(R2, param.asInt()); // take the state of the Timer and send it to the pin
 Blynk.virtualWrite(V2, param.asInt()); // Send timer state back to button to update it.
 digitalWrite(R3, param.asInt()); // take the state of the Timer and send it to the pin
 Blynk.virtualWrite(V3, param.asInt()); // Send timer state back to button to update it.
 digitalWrite(R4, param.asInt()); // take the state of the Timer and send it to the pin
 Blynk.virtualWrite(V4, param.asInt()); // Send timer state back to button to update it.
 digitalWrite(R5, param.asInt()); // take the state of the Timer and send it to the pin
 Blynk.virtualWrite(V5, param.asInt()); // Send timer state back to button to update it.
 digitalWrite(R6, param.asInt()); // take the state of the Timer and send it to the pin
 Blynk.virtualWrite(V6, param.asInt()); // Send timer state back to button to update it.
 }
  

void with_internet()
{
  if (digitalRead(S1) == LOW)
  {
    if (switch_ON_Flag1_previous_I == 0 )
    {
      digitalWrite(R1, LOW);
      if (DEBUG_SW) Serial.println("Relay1- ON");
      Blynk.virtualWrite(V1, 0);
      switch_ON_Flag1_previous_I = 1;
    }
    if (DEBUG_SW) Serial.println("Switch1 -ON");

  }
  if (digitalRead(S1) == HIGH )
  {
    if (switch_ON_Flag1_previous_I == 1)
    {
      digitalWrite(R1, HIGH);
      if (DEBUG_SW) Serial.println("Relay1 OFF");
      Blynk.virtualWrite(V1, 1);
      switch_ON_Flag1_previous_I = 0;
    }
    if (DEBUG_SW)Serial.println("Switch1 OFF");
  }


  if (digitalRead(S2) == LOW)
  {
    if (switch_ON_Flag2_previous_I == 0 )
    {
      digitalWrite(R2, LOW);
      if (DEBUG_SW)  Serial.println("Relay2- ON");
      Blynk.virtualWrite(V2, 0);
      switch_ON_Flag2_previous_I = 1;
    }
    if (DEBUG_SW) Serial.println("Switch2 -ON");

  }
  if (digitalRead(S2) == HIGH )
  {
    if (switch_ON_Flag2_previous_I == 1)
    {
      digitalWrite(R2, HIGH);
      if (DEBUG_SW) Serial.println("Relay2 OFF");
      Blynk.virtualWrite(V2, 1);
      switch_ON_Flag2_previous_I = 0;
    }
    if (DEBUG_SW)Serial.println("Switch2 OFF");
    //delay(200);
  }

  if (digitalRead(S3) == LOW)
  {
    if (switch_ON_Flag3_previous_I == 0 )
    {
      digitalWrite(R3, LOW);
      if (DEBUG_SW) Serial.println("Relay3- ON");
      Blynk.virtualWrite(V3, 0);
      switch_ON_Flag3_previous_I = 1;
    }
    if (DEBUG_SW) Serial.println("Switch3 -ON");

  }
  if (digitalRead(S3) == HIGH )
  {
    if (switch_ON_Flag3_previous_I == 1)
    {
      digitalWrite(R3, HIGH);
      if (DEBUG_SW) Serial.println("Relay3 OFF");
      Blynk.virtualWrite(V3, 1);
      switch_ON_Flag3_previous_I = 0;
    }
    if (DEBUG_SW)Serial.println("Switch3 OFF");
    //delay(200);
  }

  if (digitalRead(S4) == LOW)
  {
    if (switch_ON_Flag4_previous_I == 0 )
    {
      digitalWrite(R4, LOW);
      if (DEBUG_SW) Serial.println("Relay4- ON");
      Blynk.virtualWrite(V4, 0);
      switch_ON_Flag4_previous_I = 1;
    }
    if (DEBUG_SW) Serial.println("Switch4 -ON");

  }
  if (digitalRead(S4) == HIGH )
  {
    if (switch_ON_Flag4_previous_I == 1)
    {
      digitalWrite(R4, HIGH);
      if (DEBUG_SW) Serial.println("Relay4 OFF");
      Blynk.virtualWrite(V4, 1);
      switch_ON_Flag4_previous_I = 0;
    }
    if (DEBUG_SW)Serial.println("Switch4 OFF");
    //delay(200);
  }


  if (digitalRead(S5) == LOW)
  {
    if (switch_ON_Flag5_previous_I == 0 )
    {
      digitalWrite(R5, LOW);
      if (DEBUG_SW) Serial.println("Relay5- ON");
      Blynk.virtualWrite(V5, 0);
      switch_ON_Flag5_previous_I = 1;
    }
    if (DEBUG_SW) Serial.println("Switch5 -ON");

  }
  if (digitalRead(S5) == HIGH )
  {
    if (switch_ON_Flag5_previous_I == 1)
    {
      digitalWrite(R5, HIGH);
      if (DEBUG_SW) Serial.println("Relay5 OFF");
      Blynk.virtualWrite(V5, 1);
      switch_ON_Flag5_previous_I = 0;
    }
    if (DEBUG_SW)Serial.println("Switch5 OFF");
    //delay(200);
  }

   if (digitalRead(S6) == LOW)
  {
    if (switch_ON_Flag6_previous_I == 0 )
    {
      digitalWrite(R6, LOW);
      if (DEBUG_SW) Serial.println("Relay6- ON");
      Blynk.virtualWrite(V6, 0);
      switch_ON_Flag6_previous_I = 1;
    }
    if (DEBUG_SW) Serial.println("Switch6 -ON");

  }
  if (digitalRead(S6) == HIGH )
  {
    if (switch_ON_Flag6_previous_I == 1)
    {
      digitalWrite(R6, HIGH);
      if (DEBUG_SW) Serial.println("Relay6 OFF");
      Blynk.virtualWrite(V6, 1);
      switch_ON_Flag6_previous_I = 0;
    }
    if (DEBUG_SW)Serial.println("Switch6 OFF");
    //delay(200);
  }
  
}

void without_internet()
{

  digitalWrite(R1, digitalRead(S1));
  digitalWrite(R2, digitalRead(S2));
  digitalWrite(R3, digitalRead(S3));
  digitalWrite(R4, digitalRead(S4));
  digitalWrite(R5, digitalRead(S5));
  digitalWrite(R6, digitalRead(S6));
}


void checkBlynk() { // called every 3 seconds by SimpleTimer

  bool isconnected = Blynk.connected();
  if (isconnected == false) {
    MODE = 1;
    
  }
  if (isconnected == true) {
    MODE = 0;
  
  }
}

void setup()
{
  // Debug console
  if (DEBUG_SW) Serial.begin(9600);
  pinMode(S1, INPUT);
  pinMode(R1, OUTPUT);

  pinMode(S2, INPUT);
  pinMode(R2, OUTPUT);

  pinMode(S3, INPUT);
  pinMode(R3, OUTPUT);

  pinMode(S4, INPUT);
  pinMode(R4, OUTPUT);

  pinMode(S5, INPUT);
  pinMode(R5, OUTPUT);

  pinMode(S6, INPUT);
  pinMode(R6, OUTPUT);

      digitalWrite(R1, HIGH);
      digitalWrite(R2, HIGH);
      digitalWrite(R3, HIGH);
      digitalWrite(R4, HIGH);
      digitalWrite(R5, HIGH);
      digitalWrite(R6, HIGH);
    
  //pinMode(MODE, INPUT);
  WiFi.begin(ssid, pass);
  timer.setInterval(3000L, checkBlynk); // check if connected to Blynk server every 3 seconds
  Blynk.config(auth);//, ssid, pass);
 }

void loop()
{
  if (WiFi.status() != WL_CONNECTED)
  {
    if (DEBUG_SW) Serial.println("Not Connected");
  }
  else
  {
    if (DEBUG_SW) Serial.println(" Connected");
    Blynk.run();
  }

  timer.run(); // Initiates SimpleTimer
  if (MODE == 0)
    with_internet();
    
  else
    without_internet();
 }

For now just keep void loop clean

void loop() {
Blynk.run
timer.run
}

Check if this works.

As soon as the internet goes down it switches to without_internet() and here you are just reading the button state(high or low). As your code says input pin is high, the relay turns ON.

In setup have pinMode(S1, INPUT); you need to have an resistor across input pin and GND. This way your pin won’t be floating and your lights wont turn on.

And most of all without_internet() you need have some code to read the previous state and only change if the button is pressed.

You might want to take a look at this
https://github.com/khoih-prog/ESP32TimerInterrupt/tree/master/examples/ISR_Timer_4_Switches

Awesome contribution by @khoih
This is ready to use with few minor changes. it uses interrupt to detect changes of the push button.

I am sure this will solve all your problem.

1 Like

digitalWrite is either high or low. If you want value to be written on esp32 its

ledcWrite(channel, dutycycle)

Thank you for your reply
Madhukesh, if i use the void loop with blynk and timer only, the physical buttons will not work.
second if i put resistor across the input pin and GND my pins still playing with light randomly on/off, i have avoided this issue by putting capacitor 1mF between input and GND and thus this issue was solved for random on/off

My main problem is not all of this, if the internet go down, i have no problem if the light changes according to switches, of course it will be more better to use interrupts as u mentioned to keep the same status, so i will check it, but my MAIN issue is the ESP32 wifi is down while the internet itself is ok with my other devices, i want to solve the wifi unreliable connections of ESP which happens randomly after 30 minutes or may be 2 hours of powering on my ESP.

If it only disconnect due to internet is down this is rarely happen as my internet connection is reliable and good, but it is only the ESP which lose connection,

I bought another ESP32S and i tried it today instead of the old one, but i have the same result.

They will of you change the code so that the physical buttons are being polled on a short timer, or use interrupts and debounce each button.

Your disconnections are almost certainly caused by your void loop and the with_internet function.
You can prove this by putting some Blynk.run statements in with_internet.

I wouldn’t recommend using capacitors in this way. 10k resistors are the correct approach.

If your supply is noisy then you probably want the capacitor across the 5v supply rails, not across the output from the voltage regulator.

Pete.

1 Like

To smooth out the ripples from your power supply you need to have an 10uf electrolytic cap and a 0.1uf ceramic cap across the 5v rail and after stepping it down to 3.3v again you need to have an 10uf electrolytic cap and a 0.1uf ceramic cap across 3.3v rail and an 0.1uf ceramic cap very near to the ESP VCC and GND. This setup will remove all the noise or ripples. Voltage spikes are the worst enemy to ESP.

Even i had this issue previously. This is due to the bad code,and also a bad power supply.

I would suggest you to use the ready sketch below.
https://github.com/khoih-prog/ESP32TimerInterrupt/tree/master/examples/ISR_Timer_4_Switches

1 Like