ESP8266 (as a shield) WiFi Recovery?

Hi,

I’m using an example arduino sketch to connect to Blynk via an ESP8366 (ESP-01).
It works great bit if the wifi connection is lost it does not reconnect. I can’t see any status function in the library to check the connection - has anyone come up with a solution?

My project will be difficult to access to I want to to reconnect to WiFi at all costs.

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>

#define EspSerial Serial1
#define ESP8266_BAUD 115200
ESP8266 wifi(&EspSerial);

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "code";

void setup()
{
  // Set console baud rate
  Serial.begin(115200);
  delay(10);
  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, "name", "pw");
}

void loop()
{
  Blynk.run();
}

Basics of what you need is covered in a post a few hours ago I need a FAIL SAFE

Great, thanks for your help.

One question about your sketch, Blynk.connect(3333) does not appear to reconnect for me, any idea why?

[13749] Connected to WiFi
[14018] Ready (ping: 12ms).
Connected to Blynk server
Not connected to Blynk server
Not connected to Blynk server
Not connected to Blynk server
Not connected to Blynk server
Not connected to Blynk server
Not connected to Blynk server

Code:

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <BlynkSimpleShieldEsp8266.h>
#include <SimpleTimer.h>

char auth[] = "xxx";
char ssid[] = "xxx";
char pass[] = "xxx";
bool Connected2Blynk = false;

#define ESP8266_BAUD 115200
#define EspSerial Serial1

ESP8266 wifi(&EspSerial);
SimpleTimer timer;

void CheckConnection(){
  Connected2Blynk = Blynk.connected();
  if(!Connected2Blynk){
    Serial.println("Not connected to Blynk server"); 
    Blynk.connect(3333);  // timeout set to 10 seconds and then continue without Blynk  
  }
  else{
    Serial.println("Connected to Blynk server");     
  }
}

void setup()
{
  Serial.begin(115200);
  delay(100);
  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(100);
  Blynk.begin(auth, wifi, ssid, pass);

  timer.setInterval(11000L, CheckConnection); // check if still connected every 11 seconds 
}

void loop()
{
  if(Connected2Blynk){
  Blynk.run();
  }
 timer.run();
}

@chinswain the sketch extract you are using was before Blynk changed Blynk.begin() to a blocking function.

Principles are the same and in our deluxe reconnection sketch (not public domain) there are 2 parts.

  1. Check if WiFi is available, if it’s not try again in X seconds i.e. router is down.

  2. If WiFi is available connect and try a connection to the server with Blynk.config().

  3. If server is not available check WiFi and then server in Y seconds.

The functions for WiFI and Server have timeouts using break etc.

Post your sketch when it’s done but not if it’s better than ours :slight_smile:

So I swapped out Blynk.connect(3333); for Blynk.begin(auth, wifi, ssid, pass); which works but seems a little drastic?

void CheckConnection(){
  Connected2Blynk = Blynk.connected();
  if(!Connected2Blynk){
    Serial.println("Not connected to Blynk server"); 
    Blynk.begin(auth, wifi, ssid, pass);
  }
  else{
    Serial.println("Connected to Blynk server");     
  }
}

I can’t see any other way of establishing a wifi connection before reconnecting to Blynk with this library:

@chinswain you should be using Blynk.config() not Blynk.begin().

no matching function for call to ‘BlynkWifi::config(char [33], ESP8266&, char [11], char [13])’

Just auth for Blynk.config(), the ssid and pwd are taken care of in the WiFi connection.

Hmm, not sure if I’m missing something, no function:

Blynk.config(auth);

Compiler:
no matching function for call to 'BlynkWifi::config(char [33])'

Function from BlynkSimpleShieldEsp8266.h:

void config(ESP8266&    esp8266,
            const char* auth,
            const char* domain = BLYNK_DEFAULT_DOMAIN,
            uint16_t    port   = BLYNK_DEFAULT_PORT)
{
    Base::begin(auth);
    wifi = &esp8266;
    this->conn.setEsp8266(wifi);
    this->conn.begin(domain, port);
}

void begin(const char* auth,
           ESP8266&    esp8266,
           const char* ssid,
           const char* pass,
           const char* domain = BLYNK_DEFAULT_DOMAIN,
           uint16_t    port   = BLYNK_DEFAULT_PORT)
{
    config(esp8266, auth, domain, port);
    connectWiFi(ssid, pass);
    while(this->connect() != true) {}
}

Make a regular WiFi connection before calling the Blynk.config().

Is it possible to do this without another library? I can’t see any way to establish a regular WiFi connection while using the ESP8266 as an AT shield.

Ah yes my sketch extracts you were using are for a proper ESP connection not a Shield connection.

In that case I would probably set the Arduino to reset if connection to Blynk is lost, but it depends on your application.

I guess as a shield you would just use

#include <WiFi.h>

Define ssid and pass and then in setup():

  while ( status != WL_CONNECTED) { 
    status = WiFi.begin(ssid, pass);
    delay(10000);
  }

then connect to Blynk with Blynk.config.

We actually have our WiFi connection and Blynk connection as two functions outside of setup().

They are called in setup() and whenever the link goes down etc.

As it was noted, the ESP8266_SoftSer is no longer available, you should update your Blynk library package.
Any solutions, that you get asking questions this way, may be obsolete with new library.

Sorry, that original example was incorrect - I have updated it.

Aside from calling Begin() again I think the only other option is to wait for someone more experienced to update BlynkSimpleShieldEsp8266 to match your changes to BlynkSimpleEsp8266 so I can manage the wifi connection outside of Blynk.

Slightly unrelated but I found an interesting library that emulates the arduino wifi library for esp8266 devices: https://github.com/bportaluri/WiFiEsp

Many thanks for your help Costas.