I’m trying to find a way to connect to and maintain connection from an ESP8266 to a Blynk local server. Currently my device is connecting to the Blynk server fine, if it’s been without power a while. But in development, I’m having problems with connecting when making alterations to the code and reflashing the device. It seems to be related with the device still being present in the ASUS wifi router device list in some way. The result is that the ESP8266 gets stuck on “Connecting to [SSID]…”.

So I wondered if there is a recommended way to:

Attempt to connect to wifi at startup.

If wifi connection attempt fails, retry to connect to wifi.

If wifi connection is successful, connect to local Blynk server.

If Blynk local server connection attempt fails, retry to connect to Blynk server.

If Blynk local server connection is successful, start doing normal stuff (read sensors and push to Blynk local server, etc.).

While doing normal stuff, check that both wifi connection and Blynk server connection is OK. If one of them fails, attempt to reconnect.

It dawns on me when writing this that a connection handler with a state machine might not be a bad idea. Has this already been done perhaps?

Thanks a bunch for the tips. I have already searched this a while, and I struggled with finding a solid and updated way of doing this. Some of the threads I found had tips on how to handle reconnection, only to state later on that changes in either Blynk or ESP8266 code caused it to not work anymore.

So I ended with writing my own connection handler. I’ve tested it for a while now, and it seems to handle both wifi and Blynk server crashes well. I haven’t had any “stuck” situations so far, at least. If you see any issues with this, please let me know.

use a static IP for the Arduino or ESP
use a random MAC address. I.e. after reflashing the router sees a “new” device

that makes little sense to me, but most likely my limited knowledge:

I though MAC addresses were hard coded (as in cannot be changed)?

when using a static IP address you set this usually on the router, but with an ever changing MAC address that does not work! If it possible to set a static IP on the ESP side (no clue whether that is possible) then you run into the chance of IP address conflicts! (with other devices)

@ohjohnsen: thats a really neat routine, I might borrow that from you! The 0.1s timer though seems a bit much, I usually set it to 30s. Given the nature of your routine I understand that it should run more often but 10x per s…? Would 1s not be more than enough?

I do either one of these things, of course. Depending on what the device allows.
MAC on ESPs is fixed (ESP8266 Arduino core does not provide an API to change ESP8266’s WiFi MAC address. However, Espressif SDK offers an API to change the WiFi STA MAC address.).
For e.g. the Ethernet shield chaning MAC from Arduino core is possible …

@wolph42 Go right ahead… I plan on making the whole project open source eventually, just need to figure out how to mask ssid and password out of the committed code.

Not a bad idea increasing the interval to perhaps 500 ms. or 1 second. It would probably be a good idea then to move Blynk.run() to the loop() function, rather than having it in the connection handler.

@IBK Thanks for the tip. I already use a static IP. Don’t think I’ll be messing with the MAC address, though. After implementing the connection handler, I see that it takes at most a few retries before it reconnects with the wifi router. Guess the router needs to recognize the MAC address as disconnected before it allows reconnection. In any event, it’s not a problem for me anymore, since the ESP doesn’t get stuck now like it did earlier.

Attempt to connect to wifi at startup.
If wifi connection attempt fails, retry to connect to wifi.
If wifi connection is successful, connect to local Blynk server.
If Blynk local server connection attempt fails, retry to connect to Blynk server.
If Blynk local server connection is successful, start doing normal stuff (read sensors and push to Blynk local server, etc.).
While doing normal stuff, check that both wifi connection and Blynk server connection is OK. If one of them fails, attempt to reconnect.

I think I forwarded you my link of a simple example, that I belive does all this, already in another topic… but here it is again…

#9 Basic “Keeps running without WiFi or Blynk Server connection, but reconnects as soon as they are available” sketch
Simply does what the title says… Flashes the built in LED and sends UpTime data to both App and Serial monitor to show operation either way. Also has a lot of serial print commands to show general operation for troubleshooting… Remove them when you are comfortable that everything works for you.
http://docs.blynk.cc/#blynk-firmware-connection-management
#define BLYNK_PRINT Se…

Although the code is quite elegant, this looks like a very cumbersome way of handling the process. Look at all the code that has to be evaluated on each cycle of the void loop before the default condition (device connected, Blynk.run executed) is met.
This is going to significantly increase the liklehood of Blynk timeouts and therefore disconnections.

I don’t use Blynk in the same way as most people, but I always specify a static IP address for each individual device. I find that this makes it much easier to manage my devices and avoid having to mess around with router settings all the time.
You may also want to look at how the DHCP lease duration is configured in your router and see if reducing the lease time helps your situation.

@PeteKnight I agree when Blynk.run() resides in the else{} in the MAINTAIN_CONNECTIONS state. But I believe a better approach would be to check in loop() that state == MAINTAIN_CONNECTIONS, and then run Blynk.run(). In that case, you have only one interval delay between Blynk.connect() and Blynk.run() (given that the node connects to the Blynk server immediately).

The point was still to be able to solidly handle connection loss on both the Blynk server and the wifi connections, and then reconnect. Earlier I was plagued with the ESP locking up when connecting to wifi, and I’m not any more. That’s good enough for me.

Little you can do if you don’t have access to the device via remote. Perhaps, you can add a manual ON/OFF switch? or do something via code when internet fails. Got to see the circuit/code to better answer you.

Use a timer to do your work under a normal condition (when a connection to internet is established) and utilize the conditional already used in the loop
if (WiFi.status() != WL_CONNECTED) {//do things here}
when you have no connection.