Problems when the internet disconnects

I am using a nodemcu esp8266 and I connect it to blynk to control some outputs. These outputs are also controlled by external pushbuttons. When the internet is cut off the external pushbuttons do not respond instantly. It’s like two seconds answer and 2 seconds ignore. Something like that. Then when the internet connection comes back it works perfectly. Can someone help me about this? Thanks

This is my code:

#include <ESP8266WebServer.h>
#include <WiFiManager.h>      //https://github.com/tzapu/WiFiManager
#include <DNSServer.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "4147957xxxxxxxxxxxxxxxxxx";
char ssid[] = "Net"; // username or ssid of your WI-FI
char pass[] = "Ne"; // password of your Wi-Fi

// Your WiFi credentials.
// Set password to "" for open networks.

// Set your LED and physical button pins here
const int ledPin1 = 12;
const int ledPin2 = 13;
const int ledPin3 = 14;
const int ledPin4 = 2;
const int btnPin1 = 5;
const int btnPin2 = 4;
const int btnPin3 = 0;
const int btnPin4 = 1;

BlynkTimer timer;
void checkPhysicalButton();

int led1State = LOW;
int btn1State = HIGH;

int led2State = LOW;
int btn2State = HIGH;

int led3State = LOW;
int btn3State = HIGH;

int led4State = LOW;
int btn4State = HIGH;

// Every time we connect to the cloud...
BLYNK_CONNECTED() {
  // Request the latest state from the server
  Blynk.syncVirtual(V12);
  Blynk.syncVirtual(V13);
  Blynk.syncVirtual(V14);
  Blynk.syncVirtual(V15);

  // Alternatively, you could override server state using:
  //Blynk.virtualWrite(V12, led1State);
  //Blynk.virtualWrite(V13, led2State);
  //Blynk.virtualWrite(V14, led3State);
  //Blynk.virtualWrite(V15, led4State);

}

// When App button is pushed - switch the state
BLYNK_WRITE(V12) {
  led1State = param.asInt();
  digitalWrite(ledPin1, led1State);
}
  
 BLYNK_WRITE(V13) {
  led2State = param.asInt();
  digitalWrite(ledPin2, led2State);
 }
BLYNK_WRITE(V14) {
  led3State = param.asInt();
  digitalWrite(ledPin3, led3State);
}
BLYNK_WRITE(V15) {
  led4State = param.asInt();
  digitalWrite(ledPin4, led4State);
}

void checkPhysicalButton()
{
  if (digitalRead(btnPin1) == LOW) {
    // btn1State is used to avoid sequential toggles
    if (btn1State != LOW) {

      // Toggle LED state
      led1State = !led1State;
      digitalWrite(ledPin1, led1State);

      // Update Button Widget
      Blynk.virtualWrite(V12, led1State);
    }
    btn1State = LOW;
  } else {
    btn1State = HIGH;
  }

  if (digitalRead(btnPin2) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn2State != LOW) {

      // Toggle LED state
      led2State = !led2State;
      digitalWrite(ledPin2, led2State);

      // Update Button Widget
      Blynk.virtualWrite(V13, led2State);
    }
    btn2State = LOW;
  } else {
    btn2State = HIGH;
  }

  if (digitalRead(btnPin3) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn3State != LOW) {

      // Toggle LED state
      led3State = !led3State;
      digitalWrite(ledPin3, led3State);

      // Update Button Widget
      Blynk.virtualWrite(V14, led3State);
    }
    btn3State = LOW;
  } else {
    btn3State = HIGH;
  }

  if (digitalRead(btnPin4) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn4State != LOW) {

      // Toggle LED state
      led4State = !led4State;
      digitalWrite(ledPin4, led4State);

      // Update Button Widget
      Blynk.virtualWrite(V15, led4State);
    }
    btn4State = LOW;
  } else {
    btn4State = HIGH;
  }
}

void setup()
{
  Serial.begin(115200);
  WiFiManager wifiManager;
wifiManager.autoConnect("AutoConnectAP");
Serial.println("");
 Serial.println("wifi connected...yeey :");
Serial.println("IP address:");
Blynk.begin(auth, WiFi.SSID().c_str(), WiFi.psk().c_str());
 
  // Debug console
  Serial.begin(9600);

  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);

  pinMode(ledPin1, OUTPUT);
  pinMode(btnPin1, INPUT_PULLUP);
  digitalWrite(ledPin1, led1State);
  

  pinMode(ledPin2, OUTPUT);
  pinMode(btnPin2, INPUT_PULLUP);
  digitalWrite(ledPin2, led2State);
  

  pinMode(ledPin3, OUTPUT);
  pinMode(btnPin3, INPUT_PULLUP);
  digitalWrite(ledPin3, led3State);
  

  pinMode(ledPin4, OUTPUT);
  pinMode(btnPin4, INPUT_PULLUP);
  digitalWrite(ledPin4, led4State);

  // Setup a function to be called every 100 ms
  timer.setInterval(500L, checkPhysicalButton);
}

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

@matiascuchu please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Pete.

Ready Pete, can you help me? This problem drives me crazy.

I have the same problem and a additional problem also you can check my post also

Don’t you have the solution yet?

I’m guessing that your delay is caused by Blynk.run attempting to contact the Blynk server in the background each time the void loop is executed, but I’d have expected the delay to be longer.

You could try doing a Blynk.connected test before executing Blynk.run…

if(Blynk.connected())
{
    Blynk.run();
 }

But, the use of Blynk.begin in void setup will mean that if your device restarts with no WiFi then the buttons won’t work at all, as Blynk.begin is a blocking function.
You’d be better-off managing your own WiFi setup then using Blynk.config and Blynk.begin.

I’d also recommend not using WiFiManager unless you really need it, and if you do need it then take care to ensure you’ve configured it not to launch the captive portal in the event of a connection timeout.

Pete.

Thank you very much for answering Pete. I made some changes to my program thanks to you, but it’s still not completely efficient. Could you help me? I’ll tell you, I don’t know why but the Blynk.connect () function takes about 8 seconds to connect and influences the buttons and outputs. Could you help me or give me an alternative or solution to this?
Below I share the code:

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


char auth[] = "XXXXXXXXXXXXXXXXXXXXXX";
char ssid[] = "XXXXXXXXXXXX";
char pass[] = "XXXXXXXXXXXX";
char server[] = "blynk-cloud.com";
int port = 8080;


const int rele1 = 15; //D8
const int rele2 = 13; //D7
const int rele3 = 12; //D6
const int rele4 = 14; //D5
const int tecla1 = 1; //TX
const int tecla2 = 2; //D4
const int tecla3 = 4; //D2
const int tecla4 = 5; //D1


int bandera;  

BlynkTimer timer;


void variable_principal();

int rele1Est = LOW;
int tecla1Est = HIGH;

int rele2Est = LOW;
int tecla2Est = HIGH;

int rele3Est = LOW;
int tecla3Est = HIGH;

int rele4Est = LOW;
int tecla4Est = HIGH;

BLYNK_WRITE(V12) {
  rele1Est = param.asInt();
  digitalWrite(rele1, rele1Est);
}
  
 BLYNK_WRITE(V13) {
  rele2Est = param.asInt();
  digitalWrite(rele2, rele2Est);
  
}
BLYNK_WRITE(V14) {
  rele3Est = param.asInt();
  digitalWrite(rele3, rele3Est);
  
}
BLYNK_WRITE(V15) {
  rele4Est = param.asInt();
  digitalWrite(rele4, rele4Est);  //Lee los cambios que se realizan desde la app de Blynk y ejecuta salidas
}


void variable_principal(){
  if (digitalRead(tecla1) == LOW) {
    if (tecla1Est != LOW) {
      
    rele1Est = !rele1Est;
    digitalWrite(rele1, rele1Est);

    Blynk.virtualWrite(V12, rele1Est);
    }
    tecla1Est = LOW;
  }
  else {
  tecla1Est = HIGH;
  }
  

  if (digitalRead(tecla2) == LOW) {
    if (tecla2Est != LOW) {
      
    rele2Est = !rele2Est;
    digitalWrite(rele2, rele2Est);

    Blynk.virtualWrite(V13, rele2Est);
    }
    tecla2Est = LOW;
  }
  else {
  tecla2Est = HIGH;
  }
    

  if (digitalRead(tecla3) == LOW) {
    if (tecla3Est != LOW) {
      
    rele3Est = !rele3Est;
    digitalWrite(rele3, rele3Est);

    Blynk.virtualWrite(V14, rele3Est);
    }
    tecla3Est = LOW;
  }
  else {
  tecla3Est = HIGH;
  }


  if (digitalRead(tecla4) == LOW) {
    if (tecla4Est != LOW) {
      
    rele4Est = !rele4Est;
    digitalWrite(rele4, rele4Est);

    Blynk.virtualWrite(V15, rele4Est);
    }
    tecla4Est = LOW;
  }
  else {
  tecla4Est = HIGH;
  }
}

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

  WiFi.begin(ssid, pass);
  Blynk.config(auth, server, port);
  Blynk.connect();

  Blynk.syncVirtual(V12);
  Blynk.syncVirtual(V13);
  Blynk.syncVirtual(V14);
  Blynk.syncVirtual(V15);
  
  pinMode(rele1, OUTPUT);
  pinMode(tecla1, INPUT_PULLUP);
  digitalWrite(rele1, rele1Est);
  

  pinMode(rele2, OUTPUT);
  pinMode(tecla2, INPUT_PULLUP);
  digitalWrite(rele2, rele2Est);
  

  pinMode(rele3, OUTPUT);
  pinMode(tecla3, INPUT_PULLUP);
  digitalWrite(rele3, rele3Est);
  

  pinMode(rele4, OUTPUT);
  pinMode(tecla4, INPUT_PULLUP);
  digitalWrite(rele4, rele4Est);


  timer.setInterval(100L, variable_principal);
}

void loop(){

  if (Blynk.connected()) { 
    Blynk.run();
  } else if (bandera == 0) {  
    bandera = 1; 
    timer.setTimeout(60000L, []() { 
      bandera = 0;
      WiFi.begin(ssid, pass);
      Blynk.config(auth, server, port);
      Blynk.connect();
    }); 
  }
  timer.run();
}

That’s a very odd way to connect to WiFi, and a very messy way to manage reconnection within the void loop.

Blynk.connect has an optional timeout parameter that can be specified, but how that parameter is used is quite complex. You’d need to search for the discussions on it to understand the details.

Also, isn’t connecting the Tx pin to one of the relays and still using serial debugging a problem?

Pete.

hi, can someone help me with my project? what happens to me is that I use Blynk.connect () as many of you saw them use, to reconnect when the internet goes down. My problem is that this function usually takes 9 seconds in the event that the connection is not made. and those 9 seconds for my program completely, neither inputs nor outputs work. below I share a part of my programming:

void loop(){

  if (Blynk.connected()) { 
    Blynk.run();
  } else if (bandera == 0) {  
    bandera = 1; 
    timer.setTimeout(30000L, []() { 
      bandera = 0;
      Blynk.connect();
    }); 
  }
  timer.run();
}

I’ve moved this question from the 2 year old topic into this one, mostly because of the badly structured void loop in your code.
Let’s keep the discussion in one place.

Pete.

hi, can someone help me with my project? what happens to me is that I use Blynk.connect () as many of you saw them use, to reconnect when the internet goes down. My problem is that this function usually takes 9 seconds in the event that the connection is not made. and those 9 seconds for my program completely, neither inputs nor outputs work. below I share a part of my programming:

void loop(){

  if (Blynk.connected()) { 
    Blynk.run();
  } else if (bandera == 0) {  
    bandera = 1; 
    timer.setTimeout(30000L, []() { 
      bandera = 0;
      Blynk.connect();
    }); 
  }
  timer.run();
}

Ok, but can you help me please?

Read my earlier comments.

Pete.

You will not get an ultimate solution. It happened with me also.i am also finding the solution. Did you look at my code?

I saw your post but I practically see that there is no solution

R you controlling relays in output?

Yes, are relays

Can you help me with the delay that Blynk.connect() makes in my code?

Ok, not problem. Happy Christmas!!