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.
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.
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âŚ