WiFi problems when I want to connet it

Hi, I had a problem with the ESP32 connecting it to the Internet and disconnecting it to the Internet, but I solved it, however i don't understand why one code works and the other one doesen't, when in both codes of the connection and the disconnection are the same.

My problem was: As I said, I want to connect and disconnect the ESP32 to the Internet.
My idea was with a button, If I press it the ESP32 connects to Internet, and the next time it disconnects.
BUT the issue is that the ESP32 reboot, all the time i press the button
I don't understan why

The code is:

#include <WiFi.h>
#define boton_BT 2 // pin where i have my button connected

// Definicion conexion wifi
const char* ssid = ""; // fil with the name of your wifi connection
const char* password = ""; // fill with the password
// FIN Definicion conexion wifi

bool BT = false;


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

  pinMode(boton_BT,INPUT_PULLDOWN);
  attachInterrupt(boton_BT,Fun_Bottom,RISING); //RISING

}

void Fun_Bottom(){
  delay(100);
  
  if(digitalRead(boton_BT) == HIGH){

    if(BT == false){
      BT = true;
  
      Serial.println("Conectando a la red WiFi...");
      WiFi.begin(ssid, password);

      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print("conetcando...");
      }

      Serial.println("WiFi conectado");
      Serial.println("Dirección IP: ");
      Serial.println(WiFi.localIP());

      
    }else{
      BT = false;
      Serial.println("Desconectando de la red WiFi...");
      WiFi.disconnect();
    }

  } // fin if(digitalRead(boton_BT) == HIGH) ("fin" means "end", I am from spain)
}// FIN Fun_Bottom

void loop() {

} // fin void loop()

My second option and the one that works:
It is excaly the same as before, but with a variable between, If I press the button, it changes that bool variable from false to true, and the next time from true to false.
And then in the void loop, if that bool is true -> the ESP32 connects to Interet, if it is false -> it disconnects, (also i used another bool in the void loop, because ones it is connected or disconnected, I don't want to do it infinitely because the void loop runs over and over, therefore I only allow the code to connect or disconnet the Internet ones every time the button is pressed)

#include <WiFi.h>
#define boton_BT 2 // pin where i have my button connected

// Definicion conexion wifi
const char* ssid = ""; // fil with the name of your wifi connection
const char* password = ""; // fill with the password
// FIN Definicion conexion wifi

bool Primera_BT = false;
bool BT = false;


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

  pinMode(boton_BT,INPUT_PULLDOWN);
  attachInterrupt(boton_BT,Fun_Bottom,RISING); //RISING

}

void Fun_Bottom(){
  delay(100);
  
  if(digitalRead(boton_BT) == HIGH){

    if(BT == false){
      BT = true;
      Primera_BT = true;
      Serial.println("Variable BT = true"); // to know the value on the Serial Monitor
    }else{
      BT = false;
      Primera_BT = true;
      Serial.println("Variable BT = false");
    }

  } // fin if(digitalRead(boton_BT) == HIGH) ("fin" means "end", I am from spain)
}// FIN Fun_Bottom

void loop() {

  if(BT == true){
    if(Primera_BT == true){
      Primera_BT = false;      
      Serial.println("Conectando a la red WiFi...");
      WiFi.begin(ssid, password);

      while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print("conetcando...");
      }

      Serial.println("WiFi conectado");
      Serial.println("Dirección IP: ");
      Serial.println(WiFi.localIP());
    }
  }

  if(BT == false){
    if(Primera_BT == true){
      Primera_BT = false;
    Serial.println("Desconectando de la red WiFi...");
    WiFi.disconnect();
    delay(1000);
    }
  }

} // fin void loop()

Does anyone know the answer why the first one reboots and the other one not? The code it is the same, the only difference it's that bool that I use in the second one.

Thank you so much for your time

Don't use interrupts, if you don't know what you are doing.

An overview of interrupts on an AVR, but most of it holds true for an ESP32 environment.

Your first sketch is a good example how not to use an ISR.
The second one is not better.

Poll the button, don't use interrupts for that functionality.

Thank you for answer to my question

But right now I have another question

If i want to turn it on and off with a buttom, do not I need an interruption to know when the button is pressed? Because I don't want to be cheking it all the time if it is pressed
I want the button to "tell me" if someone touched it, thas why I chose to do it in that way

Moderador
Movido a la categoría que mejor se ajusta a su idioma.

You could use an interrupt, but it would be slower than polling,
because the ISR would only set a flag, which would be polled,
triggering all the stuff that will not run inside an ISR.

The cleaner, less knowledge intensive approach is using polling of the key directly.
You could use a button library, I like Bounce2.

if you are dealing with a time critical control function, e.g. switch on a pump to drain a tank which is liable to flood, one would use an interrupt routine which would be short, e.g. a few comands to switch on a pump and sound an alarm. A follow on task such as sending an email or SMS text to alert local operators would be carried out in the main program loop.
your application appears to be switching off a WiFi connection which does not seem to be particularly time critical

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.