Check connection status in loop and reconect

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

Do you see my error?

remove the Blynk.begin() and try:

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };  //you can edit it to your needs

if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
  }
else
 Blynk.config(auth);
 Blynk.connect();
}

The rest should be fine as is

What error?

Perfect thankyou very very much. what your name telegram? or facebook?

I’m facebook-free :smile:

Am I too old already? What is that?

I want to make friends and exchange more about electronics and programming.

telegram is like kik, or facebook messenger
:wink:

Fine… but I do remember telegrams more like that:
176057329-612x612

And I do have some still somewhere deep in my drawer (Well, they were there “a few” years ago)

3 Likes

I tried to put in the webhook, but I got an issue
:joy::joy:

A post was merged into an existing topic: (Make) ESP8266 automatically connect to the wifi and blynk servers (after disconnection)