BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

ESP32 Disconnects from WiFi after a while

Hi!
I’m using an ESP32 board to control a solid state relay.
The ESP32 connects to the router and by using the blynk mobile app, i can toggle the state of the relay.

But, after a while, when i look at the blynk app, it states that the device has been disconnected. Thus, i cannot toggle the relay.
When i turn off and turn on the ESP32, it works fine for a while and then it disconnects again.
Really need some help with this.
Below is my code for reference. Thank you

#define BLYNK_PRINT Serial

int pin        = 4;

#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[] = "jcPQzGc9dSAHlpVSHJImPJm0srRInQgp";

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

void setup() {  
  Serial.begin(9600);
  pinMode(pin, HIGH);
  pinMode(pin, OUTPUT); 
  Blynk.begin(auth, ssid, pass);
 
  
  Blynk.begin(auth, ssid, pass);
}



BLYNK_CONNECTED() {
    Blynk.syncAll();
}
void loop(){
    Blynk.run();
}

Your code seems to be a strange mix of direct pin manipulation (using digital pins) and pin declarations that you’d use if you were using virtual pins.
You’re also setting your ‘pin’ as HIGH before you’ve initialised it for output.

I think I’d add some connection checking and re-connection code in there if I was suffering from disconnections that didn’t re-connect on their own.

Pete.

Dear Sir,

Could you please tell me more about the connection checking and reconnection code

Also sir,

Does it depend on any particular configuration done on the router?

Are you using wireless range extender? Turn on Blynk DEBUG to get more insight on what could be happening.

Yes, I am using a range extender

would that have a problem?

Change this

 pinMode(pin, HIGH);

To

digitalWrite(pin, HIGH);

Instead of

Blynk.begin(auth);

Use

Blynk.config(auth);

Hi,
The following is what I experimented with, when I use a different router from a different ISP, the esp remains connected to the Blynk server forever.

When I switched to a different router from a different isp, the esp frequently disconnects from the Blynk cloud and reconnects back in a few seconds…

I’m really stuck as to why is this happening… please help me with this

I’d suggest that only your ISP can really help you with this.

Pete.