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

ESP 32 / ESP8266 different timeout!

Hello Guys,

And first thank you for all the precious answers i got from here.
I’m running some esp8266 and ESP32 device on a local Wifi network and i connected a local Blynk server on Raspberry 4 on it.
I don’t need Blynk to run all the time ( just for few special control of my devices) , and i’m trying to find a way to bypass blynk when there is no local server on the network, and then continue the void setup() without blynk.

I checked solutions to go through Blynk.connect(timeout) , with a timer , but whatever i do, its working really well on ESP8266 ( i checked on a wemos D1 mini pro ) with less than 3s of timeout before telling me if Blynk local server is connected or not.But when i’m trying on an ESP32 ( Lolin D32) , its just like the device is not considering the timeout from the Blynk.connect(), and i have more than 18s before having an answer from the arduino, and its too much for my system. It’s exactly the same code , just switch

Esp8266Wifi> and to <Wifi.h> and

Any idea ?

This is my Esp8266 code , with the 2 libraries changed for ESP32.

> #include <ESP8266WiFi.h> 
> #include <BlynkSimpleEsp8266.h>
> #define BLYNK_DEBUG
> #define BLYNK_PRINT Serial
>
> char auth[] = "IG_gS43Sshb8dOqVdVSI3GUgIlCszlln";
> char ssid[] = "Wifiled";
> char pass[] = "**";
> char server[] = "192.168.0.3";
> 
> IPAddress local_ip(192, 168,0,69);
> IPAddress gateway(192, 168,0, 1);
> IPAddress subnet(255, 255, 255, 0);
> 
> boolean ConnectWifi(void)
> {
>   boolean state = true;
>   int i = 0;
> 
> WiFi.config(local_ip, gateway, subnet);
>   WiFi.begin(ssid, pass);
>   Serial.println("");
>   Serial.println("Connecting to WiFi");
> 
>   // Wait for connection
> 
>   Serial.print("Connecting");
>   //while (status != WL_CONNECTED) {
>   while (WiFi.status() != WL_CONNECTED) {
>     delay(500);
>     Serial.print(".");
> 
>     if (i > 20) {
>       state = false;
>       break;
>     }
>     i++;
>   }
>   if (state) {
>     Serial.println("");
>     Serial.print("Connected to ");
>     Serial.println(ssid);
>     Serial.print("IP address: ");
>     Serial.println(WiFi.localIP());
>   } else {
>     Serial.println("");
>     Serial.println("Connection failed.");
>   }
>   return state;
> }
> void setup()
> {
>   Serial.begin(9600);
>
> ConnectWifi();
>   Serial.println("Wifi OK , now blink");
>   Blynk.config(auth, server, 8080);
> int mytimeout = millis() / 1000;
>Blynk.connect(3000);
>   if (Blynk.connected() == true ){
>   Serial.print ("Blynk connected");
>   }else 
>   {
>   Serial.print ("Disconnected");
>   }
> 
>  }
> void loop()
> {
>   if(Blynk.connected() == true){
>     Blynk.run();
>   }
> }
1 Like

Hello , and thank you ! I already saw this topic , and as i said , my code is working fine with ESP8266.But when i try to modify it for ESP32 ( replacing Esp8266WiFi.h and BlynkSimpleESP8266.h by WiFi.h and BlynkSimpleEsp32.h ) , my Blynk.connect() command doesnt work with my assignated timeout , and timeout after more than 15s , and i dont understand why.Any clue? thank you !

@RomainL, I stared looking at your code earlier to see if I could replicate the issue.
However, I was somewhat put-off by the poor coding. Things like this concerned me:

The variable timeout is declared, but never used again. Instead you use a variable called mytimeout

You declare a global IP address, which presumably is going to be used as the static IP address for your device:

You the re-declare it again within void setup (making it local), but this time with a different IP:

Then you don’t use this IP address (or the corresponding gateway and subnet) when you create your WiFi connection.

In a similar way, you declare a character array to hold the IP address of your local Blynk server:

then hard code the IP address (at least it’s the same this time) in your Blynk.config command:

Despite these issues, I cleaned-up the code and tried to get it running on an ESP32 (after changing the WiFi and Blynk libraries), connecting to the Blynk cloud server with one of my auth codes.
I wasn’t able to get the sketch to connect to Blynk at all, despite increasing the timeout and the allowed time. After about half an hour of messing around I gave up and went back to my own work.
I’m skeptical about using a timeout in Blynk.connect command AND using a time limit counter within a while loop.

Anyway, the point I’m making is that if you want people to look at your sample code, than it’s good practice to clean it up first, and it’s also preferable if you post the code correctly, using triple backticks instead of blockquotes, so that it’s possible to copy and paste it into the IDE without losing the formatting and having to do a find a replace on all of the opening and closing quotes.

I’d take a look at GTT’s approach, it’s a much cleaner starting point than yours.

Pete.

Hi Pete, and thank you for you answer !

Yes the code was badly cleaned, sorry for that.I did it.

As said before , its working on ESP8266 without any problem ,Blynk.connect() react to timeout.But no way to make it work on an ESP32, i tried every solution i found with timers / while loop etc without success, i still have a > 15s timeout.

Works for me when combining the connection test with the correct codes for ESP32 (including OTA).

LED on pin 2 keeps on blinking and when the server comes back online, the uptime is still going.

#define BLYNK_PRINT Serial // This prints to Serial Monitor
// #define BLYNK_DEBUG  // Optional, this enables more detailed prints
#include <WiFi.h>  // for ESP32
#include <WiFiClient.h>  // for ESP32
#include <BlynkSimpleEsp32.h>  // for ESP32
#include <ESPmDNS.h>  // For OTA w/ ESP32
#include <WiFiUdp.h>  // For OTA
#include <ArduinoOTA.h>  // For OTA

char auth[] = "xxxxxxxxxx";
char ssid[] = "xxxxxxxxxx";
char pass[] = "xxxxxxxxxx";
char server[] = "xxx.xxx.xxx.xxx";  // IP for Local Cloud Server
//char server[] = "blynk-cloud.com";  // URL for Blynk Cloud Server
int port = 8080;

int DeviceLED = 2;    // LED on GPIO2
int ReCnctFlag;       // Reconnection Flag
int ReCnctCount = 0;  // Reconnection counter

BlynkTimer timer;



void setup() {
  Serial.begin(115200);
  pinMode(2, OUTPUT);

  WiFi.begin(ssid, pass);  // Non-blocking if no WiFi available
  Blynk.config(auth, server, port);
  Blynk.connect();

  timer.setInterval(1000L, UpTime);

  ArduinoOTA.setHostname("ESP Loss of connection test");  // For OTA
  ArduinoOTA.begin();  // For OTA
}



BLYNK_CONNECTED() {
  Serial.println("Cconnected");
  ReCnctCount = 0;
}




void UpTime() {
  Blynk.virtualWrite(V0, millis() / 1000);  // Send UpTime seconds to App
  Serial.print("UpTime: ");
  Serial.println(millis() / 1000);  // Send UpTime seconds to Serial
  digitalWrite(DeviceLED, !digitalRead(DeviceLED));  // Blink onboard LED
}



void loop() {
  timer.run();
  ArduinoOTA.handle();  // For OTA

  if (Blynk.connected()) {  // If connected run as normal
    Blynk.run();
  } else if (ReCnctFlag == 0) {  // If NOT connected and not already trying to reconnect, set timer to try to reconnect in 30 seconds
    ReCnctFlag = 1;  // Set reconnection Flag
    Serial.println("Starting reconnection timer in 30 seconds...");
    timer.setTimeout(30000L, []() {  // Lambda Reconnection Timer Function
      ReCnctFlag = 0;  // Reset reconnection Flag
      ReCnctCount++;  // Increment reconnection Counter
      Serial.print("Attempting reconnection #");
      Serial.println(ReCnctCount);
      Blynk.connect();  // Try to reconnect to the server
    });  // END Timer Function
  }
}

Thanks GTT ! I’ll try this !

Hey GTT , thanks for your code, didnt succeed to reduce the timeout time for my first connection…My goal was to , whenever i have Blynk local server connected or not , start and run my device in less than 5s…I ended up recoding BlynkProtocol.h but without success…The last solution i tried and for me the best one / faster one , was to ping the local server before connecting , and connecting only if i get some response from the fixed ip of the server.Not the best solution i admit, but now blynk initialisation is not blocking my code more than 3s,in both way…If anyone need this code or have the same needs ,send me a message !

Thank you for everything !

Did you try to adjust the default timeout of Blynk.connect(), which is 30 seconds, to anything shorter (within reason)?

yes of course , in two ways : blynk.connect(timeout) ; or #define BLYNK_TIMEOUT_MS .but in ESP32 system, its sort of bypassing it and the timeout its around 18s whatever i put.When i tried to modify the code in the Blynkprotocol.h library , i succeed in creating a shorter timeout when server was disconnected, but that created some problem with connection when the local server was connected.

Its working perfectly on ESP8266 architecture, but as i rode on several topics, ESP32 architecture create some issues with blynk timeout and i was sort of stuck in this for few days.Ping is the only reliable and fast solution ( 3s maximum to connect / bypass if there is no local server available) i found !

Hello to all. I have the same problem as RomainL…When you execute Blynk.connect() on ESP32 and there is no internet connection. There is delay for 18 seconds. It doesnt matter what number you put in Blynk.connect(), every time is 18seconds (like 6000*3). The ESP32 freeze for 18 seconds.
On nodemcu ESP8266 it works with no problems.
Is there any solution instead of ping?

I also have the same problem n the trailing post with esp32

Hi, RomainL. I have the same problem with “BLYNK_TIMEOUT” for esp32. Would you please send me your code with ping or you have already better solution. Best regards.