Problems when the internet disconnects

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!!

If you don’t want to restructure your code to reduce the impact of this then you could always go for the local server option, or go for MQTT/Node-Red, with the Blynk plug-in for Node-Red.

Pete.

Yes, i want to restructure for reduce the impact of this. But i don’t find any code that can help with this. Can you write me some? Please. Thanks!

No thanks, I have other things to do with my time.

If you’ve taken on board what I’ve said already then you’ll have looked at Blynk.connect(timeout) and seen various strategies for mitigating the delay that this causes when no WiFi or internet connection is available.
Obviously attempting to connect to Blynk when no internet con is available makes no sense, so needs to be avoided.
Also, deciding on your priority ratio over re-connection versus responsiveness is a good move.
You can then structure your code so that one failed connection attempt prevents future attempts for x amount of time. A second failure could extend this if desired.
At the end of the day it’s about deciding on your priorities and structuring your code to suit your requirements.

Pete.

You were very rude to the first part, Pete. But now. In itself it is as you say, I do not know how to avoid the connection when the internet is not available. But I think you can’t help me