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();
}
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();
}
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