Blynk takes 3-4 seconds to connect to server

Hello,

I think it’s not an error, but it’s a quite big issue on ultra low power projects.

I have Ultra Low Power Weather Logger project and I’ve started to integrate with Blynk.

My sketch waits 3-4 secs to connect Blynk server in “Blynk.connect()” function. I think SSL handshaking is happening here and causing this delay.

Is there anyway to connect to Blynk server faster?

Thank you!

Hello.

Did you try local server? In case this is network latency we can do nothing here. So first thing you need to know - how long it take to ping blynk servers from your hardware. What does serial output shows?

Yes I’ve tried local server and remote default Blynk server. Both are same.

I did some research, Blynk uses port 8442 for hardware connection as default. So, there is no SSL connection on this port.

“8442 - plain TCP connection for hardware (no security)”

Seems this issue is not related with SSL connection.

Ping time is 3-5ms from serial console:
“[5143] Ready (ping: 3ms).”

In that case please provide more details - what hardware, what libraries both blynk and connection related, your sketch, full serial output, etc.

Thank you.

Here is the details:
Arduino v1.6.9
Blynk v0.4.3
ESP8266 v2.3.0
Hardware: ESP8266 (ESP-03)

My sketch:

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

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

// Your WiFi credentials.
// Set password to "" for open networks.
const char* ssid = "ssid";
const char* password = "pass";

IPAddress ip(192, 168, 1, 77); // this 3 lines for a fix IP-address
IPAddress dns(8, 8, 8, 8);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);

IPAddress blynk_server_ip(77, 223, 144, 76);
uint8_t router_mac[6] = { 0x24, 0x69, 0xa5, 0x3b, 0x93, 0x1c }; // 24:69:a5:3b:93:1c

void setup()
{
  Serial.begin(115200);

  // Setup WiFi network
  WiFi.config(ip, gateway, subnet);
  WiFi.begin(ssid, password, 10, router_mac); // channel and mac address provided for fast connection to router.

  // Setup Blynk
  Blynk.config(auth, blynk_server_ip);
  while (Blynk.connect() == false) {
    Serial.print(".");
  }

  Serial.println("Connected to Blynk server");
}

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

Serial console output:

Thank you very much.

I see from your serial output that 5 seconds are wasted on connection to local network. And connection to Blynk server itself seems instant. You need to investigate what is going on when ESP connects to your local router.

@vshymanskyy @Costas am I right?

No. Actualy local network connection almost instant. Blynk connection takes 5secs.

Current setup sketch:

  Serial.println("Connecting to local network");
  
  // Setup WiFi network
  WiFi.config(ip, gateway, subnet, dns1, dns2);
  WiFi.begin(ssid, password, 10, router_mac);

  while (WiFi.status() != WL_CONNECTED) {
    delay(20);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  // Setup Blynk
  Blynk.config(auth, blynk_server_ip);
  while (Blynk.connect() == false) {
    Serial.print(".");
  }

  Serial.println("Connected to Blynk server");

And here is the debug output:

Blynk connection takes about 5 seconds.

Connecting to local network
........
Connected to YANIK2
IP address: 192.168.1.77
[425] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.4.3 on ESP8266

[431] Free RAM: 44272
[5001] Connecting to 77.223.144.76
[5058] <[02|00|01|00] APIKEY
[5119] >[00|00|01|00]�
[5119] Ready (ping: 2ms).
[5119] <[11|00|01|00]Gver[00]0.4.3[00]h-beat[00]10[00]buff-in[00]256[00]dev[00]ESP8266[00]build[00]Jan 15 2017 14:58:19[00]
Connected to Blynk server
[5181] >[00|00|01|00]�

This is where connection to server starts.

Yesterday I’ve tried the same with my esp12e and a low power weather station with bme280 and I have your same time results

1 Like

I think you are right but why 5 seconds from showing free RAM to trying to connect to the server?

I found the problem.

This line cause the 5secs delay:

If I change 5000UL to 1000UL delay reducing to 1sec.

5 Likes

@hamityanik nice hack, set mine to 500 so about 550ms to 650ms to connect to the server from reboot.

[301] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.4.4 on ESP8266

[501] Connecting to 192.168.10.229
[625] Ready (ping: 8ms).
Connected to Blynk server

I’ve added
lastLogin = 99999;

after this line, and problem gone. (at least for now :slight_smile: )

It will be like this:
this->authkey = auth; lastLogin = 99999;

5 Likes

Fantastic work investigating this @hamityanik

1 Like

@vshymanskyy please explain purpose of this 5000L delay.

1 Like

Yes we limit reconnection time - minimum 5 seconds delay.

for what purpose?

is it OK for Local Server user to bypass this delay?

Yes, you can modify the library as you want.

1 Like

@vshymanskyy,
Are there other tricks like this one for Local Server to do it even better?

Thanks!!