Check connection status in loop and reconect

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!

Not if you will drop the Blynk.begin()

Please say easy! Typing your code!

I will present again. I first started the circuit but I am not connected to the internet. My code is not working. If the second time, I launch that connection to the internet, the circuit works even if I pull the network out. At startup there must be a network, my code is enabled. If not all will stand still!

I’d need to recall, how to manage connection while using standard Arduino’s Ethernet library It’s been awhile :slight_smile:

My code:




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

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";


const int relayPin1 = 2;
const int relayPin2 = 3;
const int relayPin3 = 4;
const int relayPin4 = 5;

const int btnPin1 = 6;
const int btnPin2 = 7;
const int btnPin3 = 8;
const int btnPin4 = 9;


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

int relayState1 = LOW;
int relayState2 = LOW;
int relayState3 = LOW;
int relayState4 = LOW;


int btnState1 = HIGH;
int btnState2 = HIGH;
int btnState3 = HIGH;
int btnState4 = HIGH;

BLYNK_CONNECTED() {
Blynk.syncVirtual(V0);
Blynk.syncVirtual(V1);
Blynk.syncVirtual(V2);
Blynk.syncVirtual(V3);
}
BLYNK_WRITE(V0) {
  relayState1 = param.asInt();
  digitalWrite(relayPin1, relayState1);
}
BLYNK_WRITE(V1) {
  relayState2 = param.asInt();
  digitalWrite(relayPin2, relayState2);
}
BLYNK_WRITE(V2) {
  relayState3 = param.asInt();
  digitalWrite(relayPin3, relayState3);
}
BLYNK_WRITE(V3) {
  relayState4 = param.asInt();
  digitalWrite(relayPin4, relayState4);
}

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 checkPhysicalButton3()
{
  if (digitalRead(btnPin3) == LOW) {
    if (btnState3 != LOW) {
      relayState3 = !relayState3;
      digitalWrite(relayPin3, relayState3);
      Blynk.virtualWrite(V2, relayState3);
    }
    btnState3 = LOW;
  } else {
    btnState3 = HIGH;
  }
}
void checkPhysicalButton4()
{
  if (digitalRead(btnPin4) == LOW) {
    if (btnState4 != LOW) {
      relayState4 = !relayState4;
      digitalWrite(relayPin4, relayState4);
      Blynk.virtualWrite(V3, relayState4);
    }
    btnState4 = LOW;
  } else {
    btnState4 = HIGH;
  }
}

void reconnectBlynk() {
  if (!Blynk.connected()) {
    Serial.println("Lost connection");
    if(Blynk.connect()) Serial.println("Reconnected");
    else Serial.println("Not reconnected");
  }
}
void setup()
{
  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);

  pinMode(relayPin3, OUTPUT);
  pinMode(btnPin3, INPUT_PULLUP);
  digitalWrite(relayPin3, relayState3);
  timer.setInterval(100L, checkPhysicalButton3);

  pinMode(relayPin4, OUTPUT);
  pinMode(btnPin4, INPUT_PULLUP);
  digitalWrite(relayPin4, relayState4);
  timer.setInterval(100L, checkPhysicalButton4);
  
  Serial.begin(9600);
  Blynk.begin(auth);
  
  timer.setInterval(11000L, reconnectBlynk);
  
  int mytimeout = millis() / 1000;
  while (Blynk.connect() == false) { 
    if((millis() / 1000) > mytimeout + 8){  // try for less than 9 seconds
      break;
      }
    }
}

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


… is NOT properly formatted = hard to read. Please edit your post and format code

sorry, i have just edited