Check connection status in loop and reconect

Thanks @Costas . Yeah, definitely haven’t tested WiFi being down for hours. But if it is down for a few minutes the rest of the sketch keeps running and WiFi is auto-reconnected after being available using the regular Blynk.connect() and Blynk.run().

@DIYTerminator there shouldn’t be any difference between minutes and hours. If your sketch can handle minutes it will probably handle hours.

@Costas, i think you might be my savior. I am trying to make a double check connection. First time we will check if we have wifi.
If not wifi connection: we are going offline and continue to execute the code.
If we have wifi connection: we will try to connect to blynk. If not connected to blynk in 3 seconds we give up and continue with the code. The periodic check to see if the blynk server is available should be around 10 minutes. in all this time my code should still work.

At this time everything is perfect if we have both wifi and blynk connection. Also perfect if we do not have wifi connection. The code works as it should. But if i have wifi and NOT blynk connection everything halts. I have tried so many times with so many pieces of code that even i don’t know what i am doing… Can you please have a look and tell mee what i am doing wrong? At this point with wifi connected but without blynk server, the led blinks once every xxx seconds. many seconds…


#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>

const char* ssid          = "SSID";
const char* password      = "PASS";
const char* host          = "SERVER";
const char  auth[]        = "TOKEN";
const int   checkInterval = 1000;

SimpleTimer timer;


void setup()
{
  pinMode(BUILTIN_LED, OUTPUT);    //led

  Serial.begin(115200);
  WiFi.begin(ssid, password);
  Blynk.config(auth, host);
  timer.setInterval(checkInterval, blynkCheck);
  timer.setInterval(200, worktime);
}
void blynkCheck() {
  if (WiFi.status() == 3) {
    if (!Blynk.connected()) {
      Serial.println("WiFi OK, trying to connect to the server...");
      Blynk.connect();
    }
  }
  if (WiFi.status() == 1) {
    Serial.println("No WiFi connection, going offline.");
  }
}

void worktime() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(300);                       // wait
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(300); 
}

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

I used blynk.begin() and my board was rebooting. Found the truant line by commenting and trying out the sketch. I commented blynk.begin and replaced it with blynk.config() in setup and blynk.run in loop. Works well now.

Setup section part:

  //config blynk
  Blynk.config(settings.blynkToken, settings.blynkServer, atoi(settings.blynkPort));
 
  connectBlynk();
  //Blynk.begin(settings);

  //check if connected to Blynk server
  if (!wifiClient.connected()) {
    digitalWrite(BlynkLED, HIGH);
    digitalWrite(wifiLED, HIGH);
    Serial.print("\nBlynk server unreachable. Trying once more");
    connectBlynk();
    return;
  }

loop section

void loop()
{
  // Reconnect to Blynk Cloud
  if (!wifiClient.connected()) {
    digitalWrite(BlynkLED, HIGH);
    connectBlynk();
    return;
  }
  Blynk.run();  
}
1 Like

the code in the loop should be in a timer instead, otherwise it will block the rest of your sketch if there is no wifi / internet / blynk server available.

2 posts were split to a new topic: nodeMCU is not reconnecting after a wifi disruption

Please check code


#define BLYNK_PRINT Serial
#include 
#include 
#include 
#include 

char auth[] = “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”;

const int relayPin1 = 6;
const int relayPin2 = 7;

const int btnPin1 = 8;
const int btnPin2 = 9;

SimpleTimer timer;
void checkPhysicalButton1();
void checkPhysicalButton2();

int relayState1 = LOW;
int relayState2 = LOW;

int btnState1 = HIGH;
int btnState2 = HIGH;

BLYNK_CONNECTED() {
Blynk.syncVirtual(V0);
Blynk.syncVirtual(V1);
}
BLYNK_WRITE(V0) {
relayState1 = param.asInt();
digitalWrite(relayPin1, relayState1);
}
BLYNK_WRITE(V1) {
relayState2 = param.asInt();
digitalWrite(relayPin2, relayState2);
}

void checkPhysicalButton1()
{
if (digitalRead(btnPin1) == LOW) {
if (btnState1 != LOW) {
relayState1 = !relayState1;
digitalWrite(relayPin1, relayState1);
Blynk.virtualWrite(V0, relayState1);
}
btnState1 = LOW;
} else {
btnState1 = HIGH;
}
}
void checkPhysicalButton2()
{
if (digitalRead(btnPin2) == LOW) {
if (btnState2 != LOW) {
relayState2 = !relayState2;
digitalWrite(relayPin2, relayState2);
Blynk.virtualWrite(V1, relayState2);
}
btnState2 = LOW;
} else {
btnState2 = HIGH;
}
}

void setup()
{
Serial.begin(9600);
Blynk.begin(auth);

pinMode(relayPin1, OUTPUT);
pinMode(btnPin1, INPUT_PULLUP);
digitalWrite(relayPin1, relayState1);
timer.setInterval(100L, checkPhysicalButton1);

pinMode(relayPin2, OUTPUT);
pinMode(btnPin2, INPUT_PULLUP);
digitalWrite(relayPin2, relayState2);
timer.setInterval(100L, checkPhysicalButton2);
}

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

pls format your code. with the backticks and check for what? describe what goes wrong now, do you have any serial outputs ?

My code not working when without connect internet, all physical button is blocked! and not reconnect

ok. try changing/adding the below:
change:

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

add:

void setup() {
...
  timer.setInterval(60*1000, reconnectBlynk);
...

add:

void reconnectBlynk() {
  if (!Blynk.connected()) {
    Serial.println("Lost connection");
    if(Blynk.connect()) Serial.println("Reconnected");
    else Serial.println("Not reconnected");
  }
}
1 Like

Thanks, I am trying!

Hi, Every thing ok. One error: When restart but not connect internet, all button physical is not working.
But connect to internet, button is actived and working. Now reject cable internet, all button is working!

Please fix my error!

ah yes, on startup you get stuck on:

Blynk.begin(auth);

IRC there’s a way around this with blynk.config() and connect later on, but I’m not familiar with that method. Perhaps someone else can step in on this, or do some digging yourself here: http://docs.blynk.cc/

Thanks!

void setup()
  {
  Serial.begin(9600);
  WiFi.begin(ssid, pass);
  Blynk.config(auth);
  if (WiFi.status() == WL_CONNECTED) {
   Blynk.connect(5);
  }
.
.
.
void loop()
{
  Blynk.run();
}

This works just fine for me. While connected to net everything smooth. If not it just try to connect 5 times and if failed proceeds with rest of the code , while Blynk.run () in loop continues to check for wifi and internet.

Hope this will help.

Blynk.run() handles the reconnect, thus if one does not want to be on hold while network is lost, the Blynk.run()need to be within some conditional check, for example the way @wolph42 presented. The only difference would be the

Blynk.config(,,,);
if ( WiFi.status() == WL_CONNECTED) { 
   Blynk.connect();
}

instead the Blynk.begin(,);

i am using W5100 ethernet

arduino first start need connect internet, if not all code and button phisical is blocked!