Settime blynk no conect

Hello, I need help with a project that I made for a friend and I just need to solve the disconnection problem, what happens is that it connects to the WiFi network but in this network it does not have internet and the Blynk tries to connect and the rest of the functions do not work, I have 4 buttons and 4 relays these should also work without internet, but since the Blynk tries to connect the button does not send the signal to the relay so that it turns on here I leave part of my code thanks. PD: sorry for my bad english use google translator

void setup() {
timer.setInterval(200L, pruebabton)
setup_wifi();
Blynk.config(token);
}

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

void pruebabton(){
int boton1 = pe.digitalRead(4);
if (boton1 == LOW)
{
if(pe.digitalRead(0) == 0){
pe.digitalWrite(0, 1);
delay(50);
Blynk.virtualWrite(V1, 0);
rele1 = ‘2’;
Serial.println(“rele 1 on”);
}else{
pe.digitalWrite(0, 0);
delay(50);
Blynk.virtualWrite(V1, 1);
rele1 = ‘1’;
Serial.println(“rele 1 off”);
}
}
}

You need pinMode statements in your void setup.

In future, when you post code it would help if you posted the full sketch, and format the code in the correct way, which is to put triple backticks at the beginning and end of your code.
Triple backticks look like this:
```

Pete.

sorry for misplacing the part of the code, as I said earlier, when the Blynk server loses the connection the buttons do not function to turn the relay on and off, if you can help me thanks

2- remove the setinterval part because it doesn’t work in any way D:

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <EEPROM.h>
#define BLYNK_PRINT Serial 
#include <BlynkSimpleEsp8266.h>
#include <PortExpander_I2C.h>
#include "DHT.h"

PortExpander_I2C pe(0x20);
char tecla;
int conected_blynk = 0;
WidgetLCD lcd(V5);

//sensor
#define DHTPIN 0 // D3
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

//-------------------VARIABLES GLOBALES--------------------------
int contconexion = 0;
unsigned long previousMillis = 0;

char ssid[50];      
char pass[50];
char token[50];

const char *ssidConf = "Configurar";
const char *passConf = "12345678";

String mensaje = "";

//-----------CODIGO HTML PAGINA DE CONFIGURACION---------------
String pagina = "<!DOCTYPE html>"
"<html lang='es'>"
"<head>"
  "<meta charset='UTF-8'>"
  "<meta name='viewport' content='width=device-width, initial-scale=1.0'>"
  "<meta http-equiv='X-UA-Compatible' content='ie=edge'>"
  "<title>Configuracion</title>"
  "<style type='text/css'>"
    "*{margin:0;padding:0;font-family:sans-serif;box-sizing:border-box}body{background:#dedede;display:flex;min-height:100vh}a{text-decoration:none}.formato{margin:auto;width:50%;max-width:500px;background:#f3f3f3;padding:30px;border:1px solid rgba(0,0,0,2)}h2{text-align:center;margin-bottom:20px;color:rgba(0,0,0,.5)}input{display:block;padding:10px;width:100%;margin:30px 0;font-size:20px}input[type=submit]{background:linear-gradient(#ffda64,#ffb940);border:0;color:brown;opacity:.8;cursor:pointer;border-radius:10px;margin-bottom:0}input[type=button]{background:linear-gradient(#64c1ff,#4073ff);border:0;color:#000102;opacity:.8;cursor:pointer;border-radius:10px;margin-bottom:0;margin-top:0}h3{color:rgba(0,0,0,.5)}ul{display:block;padding:10px;width:100%;margin-left:10px;color:rgba(0,0,0,.6)}li{margin-bottom:7px}@media (max-width:768px){.formato{width:75%}}@media (max-width:480px){.formato{width:95%}}"
  "</style>"
"</head>"
"<body>"
  "<div class='formato'>";

String configuracion = 
    "<form action='guardar_conf' method='get'>"
      "<h2>Configuracion Wifi</h2>"
      "<input type='text' placeholder='Nombre de la Wifi' name='ssid' id='ssid' required>"
      "<input type='text' placeholder='Contraseña de la Wifi' name='pass' required>"
      "<input type='text' placeholder='Token de seguridad Blynk' name='token' required>"
      "<input class='boton' type='submit' value='Guardar'/><br><br> "
    "</form>"
    "<a href='escanear'><input type='button' value='Buscar Wifi'></a>";

String guardado = "<h2>Importante</h2>"
                  "<p>Configuracion guarda....</p><br>"
                  "<p>Por favor, espere que el dispositivo se configure.</p><br>"
                  "<p>Si el dispositivo no llegase a conectar</p>"
                  "<p>a la WiFi o al programa Blynk</p>"
                  "<p>tendra que reniciar el dispositivo eh</p>"
                  "<p>ingresar todos los datos correctamente.</p>";
    
String paginafin = "</div></body></html>";

void(* resetFunc) (void) = 0; 

//------------------------SETUP WIFI-----------------------------
void setup_wifi() {
// Conexión WIFI
  WiFi.mode(WIFI_STA); //para que no inicie el SoftAP en el modo normal
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED and contconexion < 50) { //Cuenta hasta 50 si no se puede conectar lo cancela
    ++contconexion;
    delay(250);
    Serial.print(".");
    digitalWrite(2,HIGH);
    delay(250);
    digitalWrite(2,LOW);
    renicioBoton();
  }
  if(contconexion < 50){
    Serial.println("");
    Serial.println("WiFi conectado");
    Serial.println(WiFi.localIP());
    digitalWrite(2, LOW);
  }else{
    Serial.println("");
    Serial.println("Error de conexion");
    digitalWrite(2, HIGH);
    for (int i = 0; i < 100; ++i){
      digitalWrite(2, !digitalRead(2));
      renicioBoton();
      delay(100);
    }
    delay(50);
    //ESP.reset();
  }
}


//--------------------------------------------------------------
WiFiClient espClient;
ESP8266WebServer server(80);
//--------------------------------------------------------------

//-------------------PAGINA DE CONFIGURACION--------------------
void paginaconf() {
  server.send(200, "text/html", pagina + configuracion + mensaje + paginafin); 
}

void paginaguardado() {
  server.send(200, "text/html", pagina + guardado + paginafin); 
}

//--------------------MODO_CONFIGURACION------------------------
void modoconf() {
   
  delay(100);
  digitalWrite(2,HIGH);
  delay(100);
  digitalWrite(2, LOW);
  delay(100);
  digitalWrite(2,HIGH);
  delay(100);
  digitalWrite(2, LOW);

  WiFi.softAP(ssidConf, passConf);
  IPAddress myIP = WiFi.softAPIP(); 
  Serial.print("IP del acces point: ");
  Serial.println(myIP);
  Serial.println("WebServer iniciado...");

  server.on("/", paginaconf); //esta es la pagina de configuracion

  server.on("/guardar_conf", guardar_conf); //Graba en la eeprom la configuracion

  server.on("/escanear", escanear); //Escanean las redes wifi disponibles

  server.on("/guardado", paginaguardado); //mensaje de guardado
  
  server.begin();

  while (true) {
    server.handleClient();
  }
}

//---------------------GUARDAR CONFIGURACION-------------------------
void guardar_conf() {
  
  Serial.println(server.arg("ssid"));//Recibimos los valores que envia por GET el formulario web
  grabar(0,server.arg("ssid"));
  Serial.println(server.arg("pass"));
  grabar(50,server.arg("pass"));
  Serial.println(server.arg("token"));
  grabar(100,server.arg("token"));

  paginaguardado();
  delay(3000);
  ESP.reset();
}

//----------------Función para grabar en la EEPROM-------------------
void grabar(int addr, String a) {
  int tamano = a.length(); 
  char inchar[50]; 
  a.toCharArray(inchar, tamano+1);
  for (int i = 0; i < tamano; i++) {
    EEPROM.write(addr+i, inchar[i]);
  }
  for (int i = tamano; i < 50; i++) {
    EEPROM.write(addr+i, 255);
  }
  EEPROM.commit();
}

//-----------------Función para leer la EEPROM------------------------
String leer(int addr) {
   byte lectura;
   String strlectura;
   for (int i = addr; i < addr+50; i++) {
      lectura = EEPROM.read(i);
      if (lectura != 255) {
        strlectura += (char)lectura;
      }
   }
   return strlectura;
}

//---------------------------ESCANEAR----------------------------
void escanear() {  
  int n = WiFi.scanNetworks(); //devuelve el número de redes encontradas
  Serial.println("escaneo terminado");
  if (n == 0) { //si no encuentra ninguna red
    Serial.println("no se encontraron redes");
    mensaje = "<br><p>no se encontraron redes</p>";
  }  
  else
  {
    Serial.print(n);
    Serial.println(" redes encontradas");
    mensaje = "";
    mensaje = "<br>"
              "<h3>Wifi Encontradas - Seleccione una.</h3>"
              "<ul id='list'>";
              
    for (int i = 0; i < n; ++i)
    {
      // agrega al STRING "mensaje" la información de las redes encontradas 
      mensaje = (mensaje) + "<li><a onclick='ssid" + i + "()'>" + WiFi.SSID(i) + "</a></li>"
                            "<script> function ssid" + i + "(){"
                              "document.getElementById('ssid').value = '" + WiFi.SSID(i) + "'"
                            "} </script>";
      Serial.println(WiFi.SSID(i));
      delay(10);
    }

    mensaje = (mensaje) + "</ul>";
    paginaconf();
  }
}

void renicioBoton(){
  if ( Serial.available() ){ tecla = Serial.read(); }
  if( tecla == '1' ){ modoconf(); }
}

char rele1 = '0';
char rele2 = '0';
char rele3 = '0';
char rele4 = '0';

BLYNK_CONNECTED() {  
  if(rele1 == '0'){ Blynk.syncVirtual(V1); Serial.println("es 0"); }
  else if(rele1 == '1'){ Blynk.virtualWrite(V1, 1); Serial.println("es 1");}
  else if(rele1 == '2'){ Blynk.virtualWrite(V1, 0); Serial.println("es 2");}
  //Blynk.syncVirtual(V1);
  delay(50);
  if(rele2 == '0'){ Blynk.syncVirtual(V2); Serial.println("es 0"); }
  else if(rele2 == '1'){ Blynk.virtualWrite(V2, 1); Serial.println("es 1");}
  else if(rele2 == '2'){ Blynk.virtualWrite(V2, 0); Serial.println("es 2");}
  //Blynk.syncVirtual(V2);
  delay(50);
  if(rele3 == '0'){ Blynk.syncVirtual(V3); Serial.println("es 0"); }
  else if(rele3 == '1'){ Blynk.virtualWrite(V3, 1); Serial.println("es 1");}
  else if(rele3 == '2'){ Blynk.virtualWrite(V3, 0); Serial.println("es 2");}
  //Blynk.syncVirtual(V3);
  delay(50);
  if(rele4 == '0'){ Blynk.syncVirtual(V4); Serial.println("es 0"); }
  else if(rele4 == '1'){ Blynk.virtualWrite(V4, 1); Serial.println("es 1");}
  else if(rele4 == '2'){ Blynk.virtualWrite(V4, 0); Serial.println("es 2");}
  //Blynk.syncVirtual(V4);
  
}

void botonrele(int boton, int rele, char blynk){
  int boton1 = pe.digitalRead(boton);
  if (boton1 == LOW)
  {
   if(pe.digitalRead(rele) == 0){
      pe.digitalWrite(rele, 1);
      delay(50);
      Blynk.virtualWrite(blynk, 0);
      switch (blynk) {
        case V1:
          rele1 = '2';
          Serial.println("rele 1 on");
          break;
        case V2:
          rele2 = '2';
          break;
        case V3:
          rele3 = '2';
          break;
        case V4:
          rele4 = '2';
          break;
      }
    }else{
      pe.digitalWrite(rele, 0);
      delay(50);
      Blynk.virtualWrite(blynk, 1);
      switch (blynk) {
        case V1:
          rele1 = '1';
          Serial.println("rele 1 off");
          break;
        case V2:
          rele2 = '1';
          break;
        case V3:
          rele3 = '1';
          break;
        case V4:
          rele4 = '1';
          break;
      }
    }
    delay(300);
  }
}

BLYNK_WRITE(V1)  // Boton Virtual BLYNK V1
{
  int i = param.asInt();
  if(i == 1){
      pe.digitalWrite(0, 0);
      lcd.clear(); //Use it to clear the LCD Widget
      lcd.print(2, 0, "Sector cuatro"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
      lcd.print(3, 1, "Prendidas");
  }
  else{
    pe.digitalWrite(0, 1);
    lcd.clear(); //Use it to clear the LCD Widget
    lcd.print(2, 0, "Sector cuatro"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
    lcd.print(4, 1, "Apagado");
  }
}

BLYNK_WRITE(V2)  // Boton Virtual BLYNK V1
{
  int i = param.asInt();
  if(i == 1){
      pe.digitalWrite(1, 0);
      lcd.clear(); //Use it to clear the LCD Widget
      lcd.print(2, 0, "Sector cuatro"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
      lcd.print(3, 1, "Prendidas");
  }
  else{
    pe.digitalWrite(1, 1);
    lcd.clear(); //Use it to clear the LCD Widget
    lcd.print(2, 0, "Sector cuatro"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
    lcd.print(4, 1, "Apagado");
  }
}

BLYNK_WRITE(V3)  // Boton Virtual BLYNK V1
{
  int i = param.asInt();
  if(i == 1){
      pe.digitalWrite(2, 0);
      lcd.clear(); //Use it to clear the LCD Widget
      lcd.print(2, 0, "Sector cuatro"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
      lcd.print(3, 1, "Prendidas");
  }
  else{
    pe.digitalWrite(2, 1);
    lcd.clear(); //Use it to clear the LCD Widget
    lcd.print(2, 0, "Sector cuatro"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
    lcd.print(4, 1, "Apagado");
  }
}

BLYNK_WRITE(V4)  // Boton Virtual BLYNK V1
{
  int i = param.asInt();
  if(i == 1){
      pe.digitalWrite(3, 0);
      lcd.clear(); //Use it to clear the LCD Widget
      lcd.print(2, 0, "Sector cuatro"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
      lcd.print(3, 1, "Prendidas");
  }
  else{
    pe.digitalWrite(3, 1);
    lcd.clear(); //Use it to clear the LCD Widget
    lcd.print(2, 0, "Sector cuatro"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
    lcd.print(4, 1, "Apagado");
  }
}

BLYNK_READ(V6)  // Boton Virtual BLYNK V6
{
  float temp =  dht.readTemperature();
  Blynk.virtualWrite(V6, temp);
}

BLYNK_READ(V7)  // Boton Virtual BLYNK V6
{
  float humedad  =  dht.readHumidity();
  Blynk.virtualWrite(V7, humedad);
}

//------------------------SETUP-----------------------------
void setup() {

  pe.init();
  dht.begin();
  
  pinMode(2, OUTPUT);

  pe.pinMode(0, OUTPUT);
  pe.pinMode(1, OUTPUT);
  pe.pinMode(2, OUTPUT);
  pe.pinMode(3, OUTPUT);
  pe.digitalWrite(0, HIGH);
  pe.digitalWrite(1, HIGH);
  pe.digitalWrite(2, HIGH);
  pe.digitalWrite(3, HIGH);
  
  pe.pinMode(4, INPUT);
  pe.pinMode(5, INPUT);
  pe.pinMode(6, INPUT);
  pe.pinMode(7, INPUT);
  

  
  // Inicia Serial
  Serial.begin(115200);
  Serial.println("");

  EEPROM.begin(512);

  pinMode(14, INPUT_PULLUP);  //D5
  if (digitalRead(14) == 0) {
    modoconf();
  }

  leer(0).toCharArray(ssid, 50);
  leer(50).toCharArray(pass, 50);
  leer(100).toCharArray(token, 50);
  delay(10);
  
  setup_wifi();
  Blynk.config(token);
  if(Blynk.connect()){
    delay(200);
    Serial.println("conectado a blynk");
    digitalWrite(2, LOW);
    conected_blynk = 1;
  }else{
    delay(200);
    Serial.println("no conectado a blynk");
    conected_blynk = 0;
  }

}

//--------------------------LOOP--------------------------------
void loop() { 
//  unsigned long currentMillis = millis();
  Blynk.run();
  if(conected_blynk == 0){
    if(Blynk.connect()){
      Serial.println("conectado a blynk 2");
      digitalWrite(2, LOW);
      conected_blynk = 1;
    }
  }
  
  renicioBoton();

  botonrele(4, 0, V1);
  botonrele(5, 1, V2);
  botonrele(6, 2, V3);
  botonrele(7, 3, V4);
}

I already solved my problem, I found it in another post, thanks anyway.

#define INTERVAL_RECONNECT 60000L

timer.setInterval(INTERVAL_RECONNECT, timerReconnect);

void loop()
{
	if (Blynk.connected())
	{
		Blynk.run(); // Initiates Blynk Server  
	}
	
	timer.run(); // Initiates BlynkTimer	
	readKey();	
}

void timerReconnect()
{
	if (!Blynk.connected())
	{
		Blynk.connect();
	}
}
1 Like