Blynk Reconnect when wifi connection is lost

Hello,

I made a simple Blynk program, but the connection to my internet is not great. sometimes he is losing connection, but it doesn’t reconnect to my wifi.
how do I intergrade a reconnect function to the wifi?
I hope you can help me with this.
Thank you for your help!
(im using a Wemos d1 mini)

#define BLYNK_TEMPLATE_ID "XXXX"
#define BLYNK_DEVICE_NAME "XXXXX"
#define BLYNK_AUTH_TOKEN "XXXXXXXXX"


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = BLYNK_AUTH_TOKEN;

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "XXXXXXX";
char pass[] = "XXXXXXX";


int Open  = 13;
int Dicht = 12;

BLYNK_WRITE(V0) {
  int pinvalue = param.asInt ();
  digitalWrite(Open,pinvalue);
}
BLYNK_WRITE(V1) {
  int pinvalue = param.asInt ();
  digitalWrite(Dicht,pinvalue);
}


void setup()
{
  Serial.begin(115200);
  delay(100);
pinMode(Open,OUTPUT);
pinMode(Dicht,OUTPUT);

Blynk.begin(auth, ssid, pass);
}

void loop() {
  Blynk.run();
  
}
void connectionstatus() {
  
  if ((WiFi.status() != WL_CONNECTED) ) {
    Serial.println("Reconnecting to WiFi...");
    WiFi.disconnect();
    WiFi.begin(ssid, pass);
    while (WiFi.status() != WL_CONNECTED) {
      delay(100);
      Serial.print(".");
    }
    Serial.println();
    Serial.println(WiFi.localIP());
    //Alternatively, you can restart your board
    //ESP.restart();
  } else {
    Serial.println("wifi OK");
  }

  if (!Blynk.connected() ) {
    Serial.println("Lost Blynk server connection");
    Blynk.connect();
  } else {
    Serial.println("Blynk OK");
  }
}
2 Likes

If the controller controls something (a relay), then the use of “while” is highly undesirable.

You can add a count flag and exit the while loop at the desired time (more than 10 seconds) else there’s no way to check and reconnect to wifi.

Im sorry, I Should mensioned that. There are 2 relays on it. It is for opening and closing a gate 2 times a day. Hé only has to check its connection every 5 minutes or so.

So you can use timer to call connectionstatus() every 5 minutes .

void setup {


timer.setInterval(5*60*1000, connectionstatus);  // check every 5 minutes
1 Like