Help with bridge code ESP01

Hi friends, I’m using this code for in an esp01 to send commands to esp32
What happens is that it works for some time and then it stops, it stays connected to the wifi, but it doesn’t respond when pressing the buttons, then I need to restart it to work again…

ESP01 CODE


#include <BlynkSimpleEsp8266.h>
#include <ESP8266WiFi.h>
char ssid[] = "NET_VIRTUA_AP/43";
char pass[] = "ww";
char auth[] = "Nilk";
char auth1 [] = "fXfR";

WidgetBridge bridge1(0);


const int luz9 = V15; // COZINHA 1
const int btn9 = 2;


const int luz2 = V8; // COZINHA 2
const int btn2 = 0;

BlynkTimer timer;

void checkPhysicalButton();

int ledState9 = LOW;
int btnState9 = HIGH;

int ledState2 = LOW;
int btnState2 = HIGH;

BLYNK_CONNECTED() {
 
  Blynk.syncVirtual(V15);
  Blynk.syncVirtual(V8);
 
}

BLYNK_WRITE(V15) {
  ledState9 = param.asInt();
  bridge1.digitalWrite(luz9, ledState9);
}
BLYNK_WRITE(V8) {
  ledState2 = param.asInt();
  bridge1.digitalWrite(luz2, ledState2);
}

void checkPhysicalButton()

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

      // Toggle LED state
      ledState9 = !ledState9;
      bridge1.digitalWrite(luz9, ledState9);

      // Update Button Widget
      Blynk.virtualWrite(V15, ledState9);
    }
    btnState9 = LOW;
  } else {
    btnState9 = HIGH;
  }
  if (digitalRead(btn2) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btnState2 != LOW) {

      // Toggle LED state
      ledState2 = !ledState2;
      bridge1.digitalWrite(luz2, ledState2);

      // Update Button Widget
      Blynk.virtualWrite(V8, ledState2);
    }
    btnState2 = LOW;
  } else {
    btnState2 = HIGH;
  }
  }

void setup()
{
  Blynk.begin(auth, ssid, pass);

  if (Blynk.connect()) {
    bridge1.setAuthToken(auth1);
    //bridge2.setAuthToken(auth2);
  }

  pinMode(luz9, OUTPUT);
  pinMode(luz2, OUTPUT);
 
  digitalWrite(luz9, ledState9);
  digitalWrite(luz2, ledState2);

  pinMode(btn9, INPUT_PULLUP);
  pinMode(btn2, INPUT_PULLUP);
  
  // Setup a function to be called every 100 ms
  timer.setInterval(100L, checkPhysicalButton);

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


ESP32 CODE


#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <ESPmDNS.h>  // For OTA - ESP32
#include <WiFiUdp.h>  // For OTA
#include <ArduinoOTA.h>  // For OTA
BlynkTimer timer;

// define the GPIO connected with Relays and switches
#define RelayPin1 23  //D23 cozinha 1
#define RelayPin2 22  //D22 quarto 1
#define RelayPin3 21  //D21 sala 6
#define RelayPin4 19  //D19 sala 2
#define RelayPin5 18  //D18 sala 1
#define RelayPin6 5   //D5 quarto 2
#define RelayPin7 25  //D25 corredor
#define RelayPin8 26  //D26 cozinha 2
#define RelayPin9 16  //D sacada 1
#define RelayPin10 17 //D sacada 2

#define SwitchPin1 13  //D13
#define SwitchPin2 12  //D12
#define SwitchPin3 14  //D14
#define SwitchPin4 27  //D27
#define SwitchPin5 33  //D33
#define SwitchPin6 32  //D32
#define SwitchPin7 15  //D15
#define SwitchPin8 4   //D4
#define SwitchPin9 1  //D15
#define SwitchPin10 2 //D4

#define VPIN_BUTTON_1    V15 cz1
#define VPIN_BUTTON_2    V2
#define VPIN_BUTTON_3    V3 
#define VPIN_BUTTON_4    V4
#define VPIN_BUTTON_5    V5 
#define VPIN_BUTTON_6    V6
#define VPIN_BUTTON_7    V7 
#define VPIN_BUTTON_8    V8 cz 2
#define VPIN_BUTTON_9    V9 
#define VPIN_BUTTON_10    V10


int lastConnectionAttempt = millis();
int connectionDelay = 5000; // try to reconnect every 5 seconds


int toggleState_1 = 1; //Define integer to remember the toggle state for relay 1
int toggleState_2 = 1; //Define integer to remember the toggle state for relay 2
int toggleState_3 = 1; //Define integer to remember the toggle state for relay 3
int toggleState_4 = 1; //Define integer to remember the toggle state for relay 4
int toggleState_5 = 1; //Define integer to remember the toggle state for relay 5
int toggleState_6 = 1; //Define integer to remember the toggle state for relay 6
int toggleState_7 = 1; //Define integer to remember the toggle state for relay 7
int toggleState_8 = 1; //Define integer to remember the toggle state for relay 8
int toggleState_9 = 1; //Define integer to remember the toggle state for relay 7
int toggleState_10 = 1;


char auth[] = "fXfRF";
char ssid[] = "NET_VIRTUA_AP/43";
char pass[] = "ww";
char server[] = "blynk-cloud.com";  
int port = 8080;

WidgetTerminal terminal(V11);
WidgetLCD lcd(V13);

void relayOnOff(int relay){

    switch(relay){
      case 1: 
             if(toggleState_1 == 1){
              digitalWrite(RelayPin1, LOW); // turn on relay 1
              toggleState_1 = 0;
              Serial.println("Device1 ON");
              }
             else{
              digitalWrite(RelayPin1, HIGH); // turn off relay 1
              toggleState_1 = 1;
              Serial.println("Device1 OFF");
              }
             delay(500);
      break;
      case 2: 
             if(toggleState_2 == 1){
              digitalWrite(RelayPin2, LOW); // turn on relay 2
              toggleState_2 = 0;
              Serial.println("Device2 ON");
              }
             else{
              digitalWrite(RelayPin2, HIGH); // turn off relay 2
              toggleState_2 = 1;
              Serial.println("Device2 OFF");
              }
             delay(500);
      break;
      case 3: 
             if(toggleState_3 == 1){
              digitalWrite(RelayPin3, LOW); // turn on relay 3
              toggleState_3 = 0;
              Serial.println("Device3 ON");
              }
             else{
              digitalWrite(RelayPin3, HIGH); // turn off relay 3
              toggleState_3 = 1;
              Serial.println("Device3 OFF");
              }
             delay(500);
      break;
      case 4: 
             if(toggleState_4 == 1){
              digitalWrite(RelayPin4, LOW); // turn on relay 4
              toggleState_4 = 0;
              Serial.println("Device4 ON");
              }
             else{
              digitalWrite(RelayPin4, HIGH); // turn off relay 4
              toggleState_4 = 1;
              Serial.println("Device4 OFF");
              }
             delay(500);
      break;
      case 5: 
             if(toggleState_5 == 1){
              digitalWrite(RelayPin5, LOW); // turn on relay 5
              toggleState_5 = 0;
              Serial.println("Device5 ON");
          terminal.print("on");
              }
             else{
              digitalWrite(RelayPin5, HIGH); // turn off relay 5
              toggleState_5 = 1;
              Serial.println("Device5 OFF");
              terminal.print("off");
              }
             delay(500);
      break;
      case 6: 
             if(toggleState_6 == 1){
              digitalWrite(RelayPin6, LOW); // turn on relay 6
              toggleState_6 = 0;
              terminal.println("Device6 ON");
  delay(2000);
  lcd.clear(); //Use it to clear the LCD Widget
  lcd.print(4, 0, "ligado"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
  lcd.print(4, 1, "----"); 

              }
             else{
              digitalWrite(RelayPin6, HIGH); // turn off relay 6
              toggleState_6 = 1;
              terminal.println("Device6 OFF");
  delay(2000);
  lcd.clear(); //Use it to clear the LCD Widget
  lcd.print(4, 0, "desligado"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
  lcd.print(4, 1, "----"); 

              
              }
             delay(500);
      break;
      case 7: 
             if(toggleState_7 == 1){
              digitalWrite(RelayPin7, LOW); // turn on relay 7
              toggleState_7 = 0;
              Serial.println("Device7 ON");
              }
             else{
              digitalWrite(RelayPin7, HIGH); // turn off relay 7
              toggleState_7 = 1;
              Serial.println("Device7 OFF");
              }
             delay(500);
      break;
      case 8: 
             if(toggleState_8 == 1){
              digitalWrite(RelayPin8, LOW); // turn on relay 8
              toggleState_8 = 0;
              Serial.println("Device8 ON");
              }
             else{
              digitalWrite(RelayPin8, HIGH); // turn off relay 8
              toggleState_8 = 1;
              Serial.println("Device8 OFF");
              }
             delay(500);
      break;
            case 9: 
             if(toggleState_9 == 1){
              digitalWrite(RelayPin9, LOW); // turn on relay 8
              toggleState_9 = 0;
              Serial.println("Device9 ON");
              }
             else{
              digitalWrite(RelayPin9, HIGH); // turn off relay 8
              toggleState_9 = 1;
              Serial.println("Device9 OFF");
              }
             delay(500);
      break;
            case 10: 
             if(toggleState_10 == 1){
              digitalWrite(RelayPin10, LOW); // turn on relay 8
              toggleState_10 = 0;
              Serial.println("Device10 ON");
              }
             else{
              digitalWrite(RelayPin10, HIGH); // turn off relay 8
              toggleState_10 = 1;
              Serial.println("Device10 OFF");
              }
             delay(500);
      break;
      default : break;      
      }  
}

void with_internet(){
    //Manual Switch Control
    if (digitalRead(SwitchPin1) == LOW){
      delay(200);
      relayOnOff(1); 
      Blynk.virtualWrite(VPIN_BUTTON_1, toggleState_1);   // Update Button Widget  
    }
    else if (digitalRead(SwitchPin2) == LOW){
      delay(200);
      relayOnOff(2);      
      Blynk.virtualWrite(VPIN_BUTTON_2, toggleState_2);   // Update Button Widget
    }
    else if (digitalRead(SwitchPin3) == LOW){
      delay(200);
      relayOnOff(3);
      Blynk.virtualWrite(VPIN_BUTTON_3, toggleState_3);   // Update Button Widget
    }
    else if (digitalRead(SwitchPin4) == LOW){
      delay(200);
      relayOnOff(4);
      Blynk.virtualWrite(VPIN_BUTTON_4, toggleState_4);   // Update Button Widget
    }
    else if (digitalRead(SwitchPin5) == LOW){
      delay(200);
      relayOnOff(5); 
      Blynk.virtualWrite(VPIN_BUTTON_5, toggleState_5);   // Update Button Widget  
    }
    else if (digitalRead(SwitchPin6) == LOW){
      delay(200);
      relayOnOff(6);      
      Blynk.virtualWrite(VPIN_BUTTON_6, toggleState_6);   // Update Button Widget
    }
    else if (digitalRead(SwitchPin7) == LOW){
      delay(200);
      relayOnOff(7);
      Blynk.virtualWrite(VPIN_BUTTON_7, toggleState_7);   // Update Button Widget
    }
    else if (digitalRead(SwitchPin8) == LOW){
      delay(200);
      relayOnOff(8);
      Blynk.virtualWrite(VPIN_BUTTON_8, toggleState_8);   // Update Button Widget
    }
    else if (digitalRead(SwitchPin9) == LOW){
      delay(200);
      relayOnOff(9);
      Blynk.virtualWrite(VPIN_BUTTON_9, toggleState_9);   // Update Button Widget
    }
    else if (digitalRead(SwitchPin10) == LOW){
      delay(200);
      relayOnOff(10);
      Blynk.virtualWrite(VPIN_BUTTON_10, toggleState_10);   // Update Button Widget
    }
}
void without_internet(){
    //Manual Switch Control
    if (digitalRead(SwitchPin1) == LOW){
      delay(200);
      relayOnOff(1);      
    }
    else if (digitalRead(SwitchPin2) == LOW){
      delay(200);
      relayOnOff(2);
    }
    else if (digitalRead(SwitchPin3) == LOW){
      delay(200);
      relayOnOff(3);
    }
    else if (digitalRead(SwitchPin4) == LOW){
      delay(200);
      relayOnOff(4);
    }
    else if (digitalRead(SwitchPin5) == LOW){
      delay(200);
      relayOnOff(5);      
    }
    else if (digitalRead(SwitchPin6) == LOW){
      delay(200);
      relayOnOff(6);
    }
    else if (digitalRead(SwitchPin7) == LOW){
      delay(200);
      relayOnOff(7);
    }
    else if (digitalRead(SwitchPin8) == LOW){
      delay(200);
      relayOnOff(8);
    }
    else if (digitalRead(SwitchPin9) == LOW){
      delay(200);
      relayOnOff(9);
    }
    else if (digitalRead(SwitchPin9) == LOW){
      delay(200);
      relayOnOff(9);
    }
}

BLYNK_CONNECTED() {
  // Request the latest state from the server
  Blynk.syncVirtual(VPIN_BUTTON_1);
  Blynk.syncVirtual(VPIN_BUTTON_2);
  Blynk.syncVirtual(VPIN_BUTTON_3);
  Blynk.syncVirtual(VPIN_BUTTON_4);
  Blynk.syncVirtual(VPIN_BUTTON_5);
  Blynk.syncVirtual(VPIN_BUTTON_6);
  Blynk.syncVirtual(VPIN_BUTTON_7);
  Blynk.syncVirtual(VPIN_BUTTON_8);
  Blynk.syncVirtual(VPIN_BUTTON_9);
  Blynk.syncVirtual(VPIN_BUTTON_10);
}

// When App button is pushed - switch the state
BLYNK_WRITE(V12){
  int pinValue = param.asInt();
  if (pinValue == 1) 
  terminal.println("ola teste");
}

BLYNK_WRITE(VPIN_BUTTON_1) {
  toggleState_1 = param.asInt();
  digitalWrite(RelayPin1, toggleState_1);
}

BLYNK_WRITE(VPIN_BUTTON_2) {
  toggleState_2 = param.asInt();
  digitalWrite(RelayPin2, toggleState_2);
}

BLYNK_WRITE(VPIN_BUTTON_3) {
  toggleState_3 = param.asInt();
  digitalWrite(RelayPin3, toggleState_3);
}

BLYNK_WRITE(VPIN_BUTTON_4) {
  toggleState_4 = param.asInt();
  digitalWrite(RelayPin4, toggleState_4);
}

BLYNK_WRITE(VPIN_BUTTON_5) {
  toggleState_5 = param.asInt();
  digitalWrite(RelayPin5, toggleState_5);
}

BLYNK_WRITE(VPIN_BUTTON_6) {
  toggleState_6 = param.asInt();
  digitalWrite(RelayPin6, toggleState_6);
}

BLYNK_WRITE(VPIN_BUTTON_7) {
  toggleState_7 = param.asInt();
  digitalWrite(RelayPin7, toggleState_7);
}

BLYNK_WRITE(VPIN_BUTTON_8) {
  toggleState_8 = param.asInt();
  digitalWrite(RelayPin8, toggleState_8);
}
BLYNK_WRITE(VPIN_BUTTON_9) {
  toggleState_9 = param.asInt();
  digitalWrite(RelayPin9, toggleState_9);
}

BLYNK_WRITE(VPIN_BUTTON_10) {
  toggleState_10 = param.asInt();
  digitalWrite(RelayPin10, toggleState_10);
}



void reconnectBlynk() {
  if (!Blynk.connected()) {
   terminal.print("Perdeu a conexão");
    if(Blynk.connect()) terminal.print("Reconectado...");
    else terminal.print("Não reconectado");
  }
}

void setup()
{
 WiFi.begin(ssid, pass);
  Blynk.config(auth, server, port);
  Blynk.connect();
Serial.begin(9600);
  ArduinoOTA.setHostname("ESP32");  // For OTA - Use your own device identifying name
  ArduinoOTA.begin();  // For OTA
  
  timer.setInterval(60*1000, reconnectBlynk);
  pinMode(RelayPin1, OUTPUT);
  pinMode(RelayPin2, OUTPUT);
  pinMode(RelayPin3, OUTPUT);
  pinMode(RelayPin4, OUTPUT);
  pinMode(RelayPin5, OUTPUT);
  pinMode(RelayPin6, OUTPUT);
  pinMode(RelayPin7, OUTPUT);
  pinMode(RelayPin8, OUTPUT);
  pinMode(RelayPin9, OUTPUT);
  pinMode(RelayPin10, OUTPUT);


  pinMode(SwitchPin1, INPUT_PULLUP);
  pinMode(SwitchPin2, INPUT_PULLUP);
  pinMode(SwitchPin3, INPUT_PULLUP);
  pinMode(SwitchPin4, INPUT_PULLUP);
  pinMode(SwitchPin5, INPUT_PULLUP);
  pinMode(SwitchPin6, INPUT_PULLUP);
  pinMode(SwitchPin7, INPUT_PULLUP);
  pinMode(SwitchPin8, INPUT_PULLUP);
  pinMode(SwitchPin9, INPUT_PULLUP);
  pinMode(SwitchPin10, INPUT_PULLUP);
  
  //During Starting all Relays should TURN OFF
  digitalWrite(RelayPin1, toggleState_1);
  digitalWrite(RelayPin2, toggleState_2);
  digitalWrite(RelayPin3, toggleState_3);
  digitalWrite(RelayPin4, toggleState_4);
  digitalWrite(RelayPin5, toggleState_5);
  digitalWrite(RelayPin6, toggleState_6);
  digitalWrite(RelayPin7, toggleState_7);
  digitalWrite(RelayPin8, toggleState_8);
  digitalWrite(RelayPin9, toggleState_9);
  digitalWrite(RelayPin10, toggleState_10);
  
  
  lcd.clear(); //Use it to clear the LCD Widget
  lcd.print(4, 0, "Hello"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
  lcd.print(4, 1, "World");  

}

void loop()
{  
  // check WiFi connection:
  if (WiFi.status() != WL_CONNECTED)
  {
    // (optional) "offline" part of code

    // check delay:
    if (millis() - lastConnectionAttempt >= connectionDelay)
    {
      lastConnectionAttempt = millis();

      // attempt to connect to Wifi network:
      if (pass && strlen(pass))
      {
        WiFi.begin((char*)ssid, (char*)pass);
      }
      else
      {
        WiFi.begin((char*)ssid);
        terminal.print("reconectando ao roteador");
      }
    }
  }
  else
  {
    Blynk.run();
  }

  
 ArduinoOTA.handle();
  timer.run(); // Initiates SimpleTimer
}

You have an awful lot of delays in your esp32 code and as a general rule delays are bad news.
Your loop also should probably be a bit cleaner and only have blynk.run and timer.run in it.

I am aware of this, but esp32 is working fine,
I already used another code in esp32 thinking that the problem was him.
but esp01 has always had this flaw.

This is astringent way to do this. The bridge assignment should really be done in BLYNK_CONNECTED()

What library version are you using, and what ESP8266 Core version?

Pete.

hi Pete

lib blynk 1.0.0-beta.1
esp8266 commuinty 3.0.1

You should be using 0.6.1

It might also be worth downgrading your ESP Core as the 3.x versions are very new and may still be a little buggy.

Pete.

better to downgrade everything then?
and the code is not so bad?

I already pointed-out an issue.

Pete.

thank you, i will test