WiFi Reconnect with ESP8266ex + Arduino (SimpleShield ESP8266)

Hello! I have recently started to working with Blynk for IoT integration in my projects. After reading the documentation and a bit of research I have become familiar with the Blynk library. However there is an issue I often found and so far I did not found any topic about it when using Arduino + Esp8266.

My WiFi connection is not very stable and often my device desconnects from the WiFi. Once that happends the only way to re-establish the connection to the network and Blynk servers is unplugging the power or reseting the arduino manually. I have found quite a lot of information about this issue when using only ESP8266ex as MCU Like this one. However I did not manage to adapt the proposed solutions to my system.

I was wondering if is there any simple way to periodically check the connection status and in case the WiFi is disconnected re-connect to the network and connect to Blynk servers.

Details about my device:
• Arduino AtMega2560 connected via Serial1 ports to ESP8266ex (baud rate previously fixed at 9600).
• Control via Blynk app using an Android smartphone. I am using default settings so I guess I am running everything in the standard Blynk server.
• I am using the last Blynk Library 1.1.0.

So far I have attempeted checking the status of the connection with Blynk server in the void loop(). In case the connection is established it contnues with Blynk routine. In case the connection fails the devices should try connecting again.

void loop()
  {
    if(Blynk.connected()){
      Blynk.run();       // only do Blynk routines if connected to server
    }
    else {Blynk.begin(auth, wifi, ssid, pass);}
} 

I am aware that Blynk.begin() is not the ideal alternative and I have tried different alternatives within the else{} condition. However I cannot manage to use Blynk.connect() or Blynk.connectWiFi() . In other examples with the ESP8266ex alone they solved the issue using different specific WiFi libraries, however this does not seem possible with the Arduino + ESP8266 interface.

Any ideas about what could be the proper way to reconnect to the WiFi network + Blynk server?

Thanks in advance,

Jorge.

Let’s start by taking a step back.
What are your reasons for using the Mega + ESP-01 combination rather than a more modern and more powerful board with built-in WiFi capabilities such as the ESP32?

The communication process when using the Arduino + ESP-01 is via AT commands, and this is a very clunky approach. The BlynkSimpleShieldEsp8266 library is acting as a wrapper that converts the basic AT commands into a sort of WiFi protocol, but this is extremely limited compared the the propert WiFi libraries for the stand-alone ESP8266 and ESP32 boards, so your opti9ns are very limited.

Pete.

Hello Pete,

Thank you for your detailed and quick answer.

Yes, I am aware that using the Arduino + ESP8266 indeed is a tricky and rather limited platform for IoT applications. I am mainly using it because I already had an old project build on a AtMega2560 board + custom PCB and just wanted to add it the WiFi control and data logging functionalities as a way to get myself introduced to Blynk and more general IoT applications.

Next projects will surely be entirely done in ESP8266 alone or the more recent ESP32 alternatives if needed.

However I was wondering if even with the limitations of the BlynkSimpleShieldEsp8266 library in mind, would be possible to program a reconnection routine in case of WiFi outages. So far I did not found a way to make it work.

Mainly I did not manage to use Blynk.connect() or Blynk.connectWiFi() functions or other external libraries, and was thinking if maybe I am writting something wrong or just that those functions are not intended to work with my configuration.

Thanks for all the help.

These are both intended to be called once, in void setup, as different ways of initially connecting - but never together. Depending on your hardware and the WiFi library being used, at least one of these should then re-connect automatically. But, not with your hardware.

One (very messy) option is to reboot your Mega, via a pin that pulls the RST pin LOW, if your device goes offline for an extended period.

You could trawl back through old posts and look at stuff to do with re-connection routines for the Uno/Mega + ESP-01 hardware, but I’m not sure if you’ll find a reliable solution.

Pete.

Perfect Pete,

In fact I tried first with the rebooting option. However it is not suitable when you need to provide the system with Setup parameters to set in every boot (the project is an automatically controlled environmental chamber (temperature, light, humidity, CO2 concentration etc… ).

I will try checking old posts and replacing my Blynk.begin() call in the setup by the Blynk.connect() / Blynk.config or Blynk.connectWiFi(). If I found a way to keep it reconnecting I wills share it here.

Thank you for the help.

Jorge.

  Blynk.config(auth, "blynk-cloud.com", 8442);
  Blynk.connect();
instead of *Blynk.begin*

The simple solution to that is to store these values on the Blynk server and synchronise tge device with the values on connection/reconnection.

The other option is to store them 8n EEPROM, but this has a number of drawbacks when using the Uno/Mega.

For Blynk.config/begin to work you first of all need to have established an internet connection, which you can’t do with the simpleStream library in the same way that you can when using the ESP8266/ESP32 WiFi libraries.

As far as Blynk.connectWiFi() is concerned, have you read the connection management documentation about what this does, and how?

Pete.

Hello Pete,

So far I manage to solve the connection issues by implementing the following this strategy.

The simple solution to that is to store these values on the Blynk server and synchronise tge device with the values on connection/reconnection.

Maybe this makeshift approach could work for some others:

  1. Defining a boolean variable (false by default) stored in Blynk server that sets to true when offline time is longer than 10 minutes. The variable value is updated to the server and the system then resets, digitally.
 void(* resetFunc) (void) = 0;// reset function declaration, address 0

[...] 

resetFunc();  
  1. On device boot after connecting to blynk variables are synched with the server. The boolean gets updated to True, disabling the manual setup in the device and the last setup parameters stored within Blynk server are updated.

  2. Normal operation until the offline time exceeds > 10 minutes, then everything re-starts again.

It is not ideal when you deal with timers that needs to be acting on an accurate way (eg. controlling light / dark cycles and similar) , but it works for the general sensing + control purposes.

For Blunk.config/begin to work you first of all need to have established an internet connection, which you can’t do with the simpleStream library in the same way that you can when using the ESP8266/ESP32 WiFi libraries.
As far as Blynk.connectWiFi() is concerned, have you read the connection management documentation about what this does, and how?

And about this… yes, I read trhough all the connection management and WiFi provisioning documentation. Although I still finding some issues to understand which ones will work properly on the clunky Arduino + Esp8266 configuration. I am looking now for alternative libraries that allow establishing the WiFi connection and then using Blynk.config() and Blynk.connect() functions.

Concerning blynk.connectWiFi(), I never manage to make it work in the Arduino+ESP8266. After reading the docs I am unsure about why but to me it seems that simply they are not designed to work under this hardware combination.

Anycase any furhter clarification will be great.

I will continue working on this.

And as always, thank you for the clarifying answers.

Best,

Jorge.

I think you’d be better looking at different hardware, or re-visiting the reboot option.

My first excursions into home automation, before I discovered Blynk and the ESP8266, used a Mega and Ethernet shield in a remote location.
Every now and again the Ethernet shield would hang-up and make the device inaccessible.
My solution was to do a ping of an external website every minute and if it was inaccessible I would pull a pin LOW, which was connected to RST and would reboot the device, resetting the Ethernet shield in the process.

This is what I was suggesting for you.

I don’t really understand your makeshift approach to your parameters. The normal way is to hard-code default variable values into your sketch, then use BLYNK_CONNECTED and Blynk.syncVirtual(vPin) to retrieve updated values for these variables from the Blynk server - ones that are normally set via a parameters screen that you create within the app.

Pete.

Oh! That first project indeed sounds very similar to this mine very first beginner steps hehe. It is encouraging realizing how far is all the IoT world reaching.

And about the makeshift approach. My fault, probably I didn’t explained myself properly. In fact I followed the approach you mentioned ( BLYNK_CONNECTED followed by Blynk.syncVirtual(vPin) ).

All the boolean variable mess was mainly because the device also have Physichal buttons for manual setup in case it needs to be set fully offline. So, when there is no WiFi connection an initial setup code blocks the execution and connection to Blynk. But this is easier to replace or adapt.

Anycase, for the future stages of this and other projects I will surely move forward to more versatile and recent IoT-Specific boards.

Once again, thank you for all your help Pete.

Best wishes,

Jorge.