// _____________________________________
// / __ __ /
// / ___ / / ____ _/ /_ ____ /
// / / _ \/ / / __ `/ __ \/_ / /
// / / __/ /___/ /_/ / /_/ / / /_ /
// / \___/_____/\__,_/_.___/ /___/ /
// /____________________________________/
// ~ Embedded Labz :: eLabz.Net ~
//
#define Project "ESP32-WiFi.ino"
//
// Author: Designed by Andre Santana [eLabz.Net/Santana]
//
#define Sketch "ESP32-WiFi.ino"
#define CodeURL "[github.com/e-Labz/ESP32-WiFi]"
//
// Description: Simple implementation of persistent wifi
// connection for ESP8266 & ESP32
//
#define Version "0.10"
#define RelDate "2023-04-09"
//
#define License "GPL-3.0-or-later"
//
// This is free software, please consider supporting to
// help keep our coffee or beer @ [eLabz.Net/donate] ;-)
//
// This program comes with ABSOLUTELY NO WARRANTY. This is
// free software, and you are welcome to redistribute it
// under certain conditions.
//
// See [www.gnu.org/licenses] for more details.
//
////////////////////////////////////////////////////////////////////
#include <WiFi.h>
#define pshBtn 5 // set push button pin
const char* ssid = "Wokwi-GUEST";
const char* password = "";
volatile bool pshBtnFlag = false;
void ICACHE_RAM_ATTR pshISR();
void WiFiStationConnected(WiFiEvent_t event, WiFiEventInfo_t info){
Serial.println("Connected to AP successfully!");
}
void WiFiGotIP(WiFiEvent_t event, WiFiEventInfo_t info){
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void WiFiStationDisconnected(WiFiEvent_t event, WiFiEventInfo_t info){
Serial.println("Disconnected from WiFi access point");
Serial.print("WiFi lost connection. Reason: ");
Serial.println(info.wifi_sta_disconnected.reason);
Serial.println("Trying to Reconnect");
WiFi.disconnect(false);
delay(3000);
WiFi.begin(ssid, password);
delay(3000);
WiFi.reconnect();
}
void setup(){
Serial.begin(115200);
WiFi.disconnect(true); // delete old config
delay(1000);
WiFi.onEvent(WiFiStationConnected, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_CONNECTED);
WiFi.onEvent(WiFiGotIP, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_GOT_IP);
WiFi.onEvent(WiFiStationDisconnected, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_DISCONNECTED);
/* Remove WiFi event
Serial.print("WiFi Event ID: ");
Serial.println(eventID);
WiFi.removeEvent(eventID);*/
WiFi.begin(ssid, password);
Serial.println();
Serial.println();
Serial.println("Wait for WiFi... ");
pinMode(pshBtn, INPUT_PULLUP); // Initialize hardware GPIOs
attachInterrupt( // attach ISR
digitalPinToInterrupt(pshBtn), // to pshBtn pin
pshISR, // and pshBtnISR function
RISING);
}
void loop(){
if(pshBtnFlag == true) {
Serial.println("Disconnecting . . .");
WiFi.disconnect(true);
pshBtnFlag = false;
}
delay(1000);
}
/* -------------------- ISR Attached Function ------------------- */
void ICACHE_RAM_ATTR pshISR() { // PshISR attached int
static unsigned long lastDebounceTime = 0; // initalize debounce vars
const unsigned long debounceDelay = 200; // set debounce interval
if ((millis() - lastDebounceTime) > debounceDelay) { // debounce algorithm
pshBtnFlag = true; // set notify flag
}
lastDebounceTime = millis(); // set debounce millis
}
////////////////////////////////////////////////////////////
// This is free software, please consider supporting to //
// help keep our coffee or beer @ [eLabz.Net/donate] ;-) //
////////////////////////////////////////////////////////////
Loading
esp32-c3-devkitm-1
esp32-c3-devkitm-1