Hi I need to have my ESP32 reconnect after wifi has come back into range for use of the BLYNK App
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "+++++++++++++++";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "++++++";
char pass[] = "+++++++";
void setup()
{
// Debug console
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
}
void loop()
{
Blynk.run();
}
Gunner
August 26, 2017, 1:15am
2
Hello and welcome to the Blynk Forum
Your issue is common and there are many ways around it…
Look into alternate connection management methods… i.e. Blynk.config()
instead of Blynk.begin()
http://docs.blynk.cc/#blynk-firmware-configuration
http://docs.blynk.cc/#blynk-firmware-connection-management
And do some research on the forum about similar questions…
E.g. Blynk is blocking if internet is down
all sorted not sure how solid it will be only time will tell code below
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <SimpleTimer.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";
bool Connected2Blynk = false;
SimpleTimer timer;
void setup() {
Serial.begin(115200);
delay(10);
timer.setInterval(11000L, CheckConnection); // check if still connected every 11 seconds
Serial.println("\nStarted");
MyWiFi();
}
void MyWiFi(){
int mytimeout = millis() / 1000;
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
if((millis() / 1000) > mytimeout + 3){ // try for less than 4 seconds to connect to WiFi router
break;
}
}
if(WiFi.status() == WL_CONNECTED){
Serial.print("\nIP address: ");
Serial.println(WiFi.localIP());
}
else{
Serial.println("\nCheck Router ");
}
Blynk.config(auth);
Connected2Blynk = Blynk.connect(1000); // 1000 is a timeout of 3333 milliseconds
mytimeout = millis() / 1000;
while (Blynk.connect(1000) == false) {
if((millis() / 1000) > mytimeout + 3){ // try for less than 4 seconds
break;
}
}
}
void CheckConnection(){
Connected2Blynk = Blynk.connected();
if(!Connected2Blynk){
Serial.println("Not connected to Blynk server");
MyWiFi();
}
else{
Serial.println("Still connected to Blynk server");
}
}
void loop() {
if(Connected2Blynk){
Blynk.run(); // only process Blyk.run() function if we are connected to Blynk server
}
timer.run();
}
Gunner
August 26, 2017, 4:13am
4
Sounds good.
I fixed your posted code. For future posting of code, use the following triple backticks, fore and aft of the code, for proper display…
…as per the Welcome Topic that everyone forgets to read
Hey, welcome to Blynk Community.
This is the best place to ask questions, leave feedback and share your ideas.
If this is the first time you hear about Blynk, check out our website www.blynk.cc and come back!
As the community is big (and growing), let’s agree on some basic rules.
Asking for help
Before creating a new topic and asking for help, please check if your question was already answered:
Use forum search function
You may want to check out:
Blynk Documentation
Blynk Wiki
Blynk Exa…
PS With latest Blynk Libraries, SimpleTimer has been integrated into Blynk, so you don’t need that library anymore.