Why Blynk connection time varies between 1sec and 15sec?

Hello,

Running the same Arduino sketch the time it takes to make connection to Blynk varies between 1sec and 15sec.

Why is that?
Is there something I can do to get rid of those extra long connection times?

The time is calculated for this part of the sketch:

  Blynk.config(auth);
  while (Blynk.connect() == false) {
    delay(200);
    Serial.print("x");
   }

The time below is in milliseconds.

Blynk%20connection%20time

Thanks,
Tipo

Probably due to networking variances… you are connecting across the internet.

Do you have any Blynk debug info to go along with your timing chart?

Here is some debug info. It has some quick and some slower connections.

Between the connections ESP-12 goes to deep sleep so the time stamps are about the same always.

The only difference I can see between quick and slow connection is that in slow connection it tries to make the connection twice or even three times. The second try happens just 1ms after the first try.

[13960] 
___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
    /___/ v0.5.4 on ESP-12

[13966] Connecting to blynk-cloud.com:80
[14038] <[02|00|01|00] 3dc48a9ce1754f07a582071a7e89ab
[14086] >[00|00|01|00|C8]
[14087] Ready (ping: 48ms).
[14087] Free RAM: 45800
[14154] <[11|00|02|00]Gver[00]0.5.4[00]h-beat[00]10[00]buff-in[00]1024[00]dev[00]ESP-12[00]build[00]Feb 16 2019 07:53:16[00]

[13832] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.5.4 on ESP-12

[13838] Connecting to blynk-cloud.com:80
[18839] Connecting to blynk-cloud.com:80
x [23840] Connecting to blynk-cloud.com:80
[23885] <[02|00|01|00] 3dc48a9ce1754f07a582071a7e89ab
[23928] >[00|00|01|00|C8]
[23928] Ready (ping: 42ms).
[23928] Free RAM: 45296
[23995] <[11|00|02|00]Gver[00]0.5.4[00]h-beat[00]10[00]buff-in[00]1024[00]dev[00]ESP-12[00]build[00]Feb 16 2019 07:53:16[00]

[13801] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.5.4 on ESP-12

[13808] Connecting to blynk-cloud.com:80
[13878] <[02|00|01|00] 3dc48a9ce1754f07a582071a7e89ab
[13951] >[00|00|01|00|C8]
[13951] Ready (ping: 72ms).
[13951] Free RAM: 45800
[14018] <[11|00|02|00]Gver[00]0.5.4[00]h-beat[00]10[00]buff-in[00]1024[00]dev[00]ESP-12[00]build[00]Feb 16 2019 07:53:16[00]

[13828] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.5.4 on ESP-12

[13835] Connecting to blynk-cloud.com:80
[18836] Connecting to blynk-cloud.com:80
[18888] <[02|00|01|00] 3dc48a9ce1754f07a582071a7e89ab
[18932] >[00|00|01|00|C8]
[18932] Ready (ping: 43ms).
[18932] Free RAM: 45800
[18999] <[11|00|02|00]Gver[00]0.5.4[00]h-beat[00]10[00]buff-in[00]1024[00]dev[00]ESP-12[00]build[00]Feb 16 2019 07:53:16[00]

[13963] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.5.4 on ESP-12

[13970] Connecting to blynk-cloud.com:80
[14036] <[02|00|01|00] 3dc48a9ce1754f07a582071a7e89ab
[14092] >[00|00|01|00|C8]
[14092] Ready (ping: 55ms).
[14092] Free RAM: 45800
[14159] <[11|00|02|00]Gver[00]0.5.4[00]h-beat[00]10[00]buff-in[00]1024[00]dev[00]ESP-12[00]build[00]Feb 16 2019 07:53:16[00]

Okay, you didn’t mention the deep sleep thing before.
Have you allocated your ESP a static IP address in your router? If not then you should try doing that.

Pete.

How can I find out these IP addresses for my network to try static IP?

Below is example.
IPAddress ip( 192, 168, 0, 1 );
IPAddress gateway( 192, 168, 0, 254 );
IPAddress subnet( 255, 255, 255, 0 );

WiFi.localIP() responds my IP address is: 192.168.10.40

I’m making a few (educated) guesses, but I’m guessing that this is how things work at the moment. Apologies if I’ve pitched this at the wrong level for you, and just shout if there’s something you don’t understand…

At the moment, you router is probably using DHCP to dynamically hand-out IP addresses to devices that want to connect to your network. With the IP address range that you’re using at the moment (I’ll come back to that later) there are 255 unique IP addresses available. Actually there are slightly less than that, as your router takes-up one of those, and another one is reserved for some networking stuff.

It’s only the last 3 digits of the IP address that can change, and every device on the network must have a unique IP address.

To try to maintain some sort of order, the router attempts to allocate the same IP address to a device whenever it appears on the network. The device is identified by its MAC address, and the router says to itself “Okay, I’ve seen you before, last time I gave you address 123, that address is currently available, so I’ll give you 123 again”.
It then creates an entry in a routing table that says any traffic for that MAC address should be routed to this IP address. As this routing table could get very messy very quickly, the router monitors each of the MAC addresses on its network and if one of them disappears it deletes that entry from the routing table. It does that quite quickly, usually withing half a minute or less of the device not responding to a broadcast message.

I think that this is where your delays are coming from. The routing table entry is being deleted each time your ESP goes to sleep, and the router/ESP have to re-negotiate which IP address is going to be dynamically allocated to the device, and a routing table entry created.

What you want to do is to get the router to create a permanent routing table entry for your device, so as soon as the device connects to Wi-Fi it can start sending data packets without the whole IP address negotiation / routing table entry creation process having to take place.
Think of it like having an allocated parking space, rather than having to drive around the car park trying to find an empty space.

How you you get your router to do this will obviously depend on the make/model/firmware version of your router.
With my router, it’s an option called “Address Reservation” that lives in the Advanced/LAN IP Setup menu and looks like this:
image

With my router, clicking the Add button will show a list of currently connected devices and you just select which one you want to use and give it a nickname.

Now back to your question…

In your post you said:

This is a way of allocating a fixed IP address to your ESP within your sketch. However, this wouldn’t help your situation, as the router still wouldn’t retain a routing table entry for that IP address.
Also, these IP addresses aren’t compatible with your current network address range, as they have “0” for the third part of the address (in bold above) rather than the “10” that’s used by your device below. This would put your ESP on a different subnet and it would be invisible to your router, so impossible for it to connect to the network.

So this is the IP address allocated to your ESP by your router using DHCP. When you log in to your router and want to add a static routing entry for your ESP, this is the IP address that you’ll be looking for (assuming it hasn’t changed since). You might want to disable deep sleep on your ESP device while you do this on your router, otherwise the device will keep disappearing from the list of potential devices to add a fixed IP/static routing entry for.

Hope this makes some sort of sense, and that you can manage to log in to your router and figure-out how to add a static router for your device. Shout if not and we’ll see what we can do to help.

Pete.

1 Like

Thanks Pete.

I think I got what you are saying and I’m trying to get fixed IP connection to work. I was able to fix ESP IP in router, trying to get the firmware right.Currently it says Wifi connection is made, but it doesn’t connect to Blynk at all. I need to continue debugging that…

Just one question:
I thought it doesn’t matter to Blynk how and with what IP address the wifi connection is made. Wifi connection is made first and when it’s done, Blynk.connect() wouldn’t know anything about how the Wifi connection was made.
But this is not how it works?

You shouldn’t need to change your sketch at all, it should connect to your router using the same code as before, the router will just take care of handling the routing table entry as being persistent.

That’s true, but from what I’ve observed doing my own tests, I think that what’s happing is that the Wi-Fi connection between the ESP and the router gets set-up, but the device can’t actuallt talk to the outside world until the router has finished doing the routing table bit of the process. In the meantime, Blynk is trying and failing to send data packets out to the Blynk server.
You see a similar effect sometimes when connecting your phone to a Wi-Fi hotspot. You’ll see that the phone is connected to the Wi-Fi, but you get a ‘no internet connectivity’ message for a few seconds, then this disappears.

The Blynk library used to have a delay built in to it where it would wait 5 seconds after the Wi-Fi connection was established before attempting authentication with the server, but this seems to have been removed now. I guess this delay was to allow the router to sort itself out before attempting a Blynk connection.

Pete.

Setting fixed IP for ESP MAC address in the router didn’t help.

Changing deep sleep time from 60sec to 10min fixed the issue: now the Blynk connection time has been steadily under 800ms.

I used 60sec deep sleep time only for debugging purposes. I’m actually using 10min deep sleep in my project.So, as this solution works for me now I’m not digging deeper in what really is the issue with shorter deep sleep time. Need to move on with other parts of the project…

1 Like