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