Reconectar blynk

Hola, alguien sabe como puedo reconectar mi ESP32 al servidor de blynk luego de que se corte el internet? ya que cuando el internet vuelve no se reconecta, probé usar la función Blynk.connect() cuando se detecte que se perdió la conexión pero no me funcionó.

Hi, (I used Google Translate so read your post.

Blynk should automatically reconnect when your Internet connection is restored.

First - post your code so we can see what you have done.

billd

Resulta que después de estar unas horas desconectado, blynk no vuelve a reconectar.
Pero conseguí resolver el problema con este código:

int lastConnected;
BlynkTimer timer;

void reconnectBlynk() {
  if (!Blynk.connected()) {
    Serial.println("Lost connection");
    if(Blynk.connect()) Serial.println("Reconnected");
    else Serial.println("Not reconnected");
  }
}
void setup()
{
  WiFi.begin(ssid, pass);
  Blynk.config(auth);
  Serial.begin(9600);
  timer.setInterval(60*1000, reconnectBlynk);
}

void loop()
{
  if(Blynk.connected()){
  Blynk.run();
  lastConnected = millis();
  }  
  else{
    if (millis() > (lastConnected+120000)) {      // if it has been 2 minutes or more since the last connection or last attempt to connect to Blynk Server
      Blynk.connectWiFi(ssid, pass);
      if (WiFi.status() == WL_CONNECTED){
        Blynk.config(auth);
        Blynk.connect();
  }
    }
}
timer.run();
}

If you include Blynk.begin(auth, ssid, pass); in setup, and Blynk.run(); in the loop, you should not need all that other code.

Billd

Intenté hacer eso, fue mi primera opción, pero no conseguí hacerlo funcionar, el problema lo tenía cuando internet estaba conectado pero en algún momento dejaba de funcionar, cuando volvía no se reconectaba mi ESP32, tenía que ir a reiniciar la placa.

Strange . . . Blynk.run(); should take care of the reconnect. Do you have the latest Blynk library installed?

Tengo instalada la versión 0.6.1

That’s the latest. What about your ESP32? Latest library for your specific board?

Para ESP32 tengo instalada la de espressif systems 1.0.4, según el gestor de tarjetas es la última versión.

That’s the latest. Do you have anything else running in your setup)( or loop() ?

Solamente tengo
Timer.run();
Timer2.run();
Y en el setup es lo que publique en el código de arriba.

Can you post the FULL code that does NOT work (the one with Blynk.begin()?

Este es el codigo que intente usar pero no se reconectaba:

#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
bool Estado;
bool funcionando = false;
#define EXTRACTOR 13
char auth[] = "******";
char ssid[] = "*******";
char pass[] = "******";

BlynkTimer timer;
WidgetLED led1(V4); 
BLYNK_CONNECTED(){
Blynk.syncAll();
}

BLYNK_WRITE(V5) 
  {
    Estado = param.asInt();
    if(Estado == true && funcionando == false){
     timer.setTimeout(1000L, Confirmar);
    }
  }

void Confirmar(){
  if(Estado == true){ 
    digitalWrite(EXTRACTOR, HIGH);
    led1.on();
    funcionando = true;
    timer.setTimeout(10000L, RelayOff); 
  }
}

void RelayOff(){
  digitalWrite(EXTRACTOR, LOW);
  led1.off();
  funcionando = false;
}

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(EXTRACTOR, OUTPUT);
}

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

The only thing I see is that you are using

In two of your functions - effectively delays . . . Blynk doesn’t like deles, this may be causing Blynk.run() to stop . . . have youe tried a basic example (LED and button) without any timers?

Pero ese tipo de timer no está basado en la librería SimpleTimer.h? Según tengo entendido la función timer.setTimeout usa millis().

Possibly (likely) - I’m just trying to eliminate possibilities.

De igual forma ese timer se activa desde la app, así que solo funciona cuando yo lo activo, no creo que le cause algún problema a blynk.run()

I’m stumped . . . I’ve got about 20 ESP8266/32 devices connected on my network, and they always re-connect when I have interruptions.

I can’t think of anything else other than try a very basic sketch, then keep adding code elements to see what is stopping Blunk.run() from working properly.

Mañana voy a intentar lo que decis y ver que resultados obtengo.
Puede ser algún problema en mi ESP32? Tengo la versión de 38 pines

It could be . . . there are many, many boards, some are low quality . . . when I get strange issues like these I always go back to basic sketch, and try with another device - it helps to eliminate unknown issues.

Good Luck.

billd