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.
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:
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.
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.
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…