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.

Yes. I had same exact problem with my project. Problem is the range extender. Blynk doesn’t works well with range extender, even if it does it won’t reconnect due to packet routing from main router. Try connecting with main router and your problem will be solved.

That’s rather a sweeping statement!

Pete.

I don’t think so . . .

I have the following active projects around my house . . .
2 x ESP32
3 x Wemos D1R2 ESP8266
9 x Wemos D1 Mini ESP8266
4 x Sonoff ESP8266 with customs firmware
2 x Range Extenders . . .

All my devices running on Blynk . . . All but two of the devices are connected to one or other of the Range Extenders . . . ALL the devices have no problem connecting to the Range Extender, or re-connecting to the Range Extender.

Your particular model or configuration of Range Extender or WiFi configuration may be an issue for Blynk, but certainly not all . . . I’ve always found Blynk to a very stable platform, any issues I’ve had have been device or local WiFi related.

cul
billd

1 Like