BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

ESP32 Tethering from mobile / won't reconnect auto


#1

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

#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


#3

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

#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 :wink:

PS With latest Blynk Libraries, SimpleTimer has been integrated into Blynk, so you don’t need that library anymore.

http://docs.blynk.cc/#blynk-firmware-blynktimer