Last active
July 24, 2022 11:57
-
-
Save Tech500/da87757a60af447fc8d59b8a8a6a2abd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Modification of ESP32, core 2.0.4 Wifi, WifiClientEvents.ino library example; same result as tutorial: | |
//https://raw.githubusercontent.com/RuiSantosdotme/Random-Nerd-Tutorials/master/Projects/ESP32/ESP32_Auto_Reconnect.ino | |
#include <WiFi.h> | |
const char* ssid = "Your WiFi name"; | |
const char* password = "Your WiFi password"; | |
void WiFiEvent(WiFiEvent_t event) | |
{ | |
Serial.printf("[WiFi-event] event: %d\n", event); | |
switch (event) { | |
case ARDUINO_EVENT_WIFI_STA_CONNECTED: | |
Serial.println("Connected to access point"); | |
break; | |
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: | |
Serial.println("Disconnected from WiFi access point"); | |
break; | |
case ARDUINO_EVENT_WIFI_STA_GOT_IP: | |
Serial.print("Obtained IP address: "); | |
Serial.println(WiFi.localIP()); | |
break; | |
default: break; | |
}} | |
void WiFiGotIP(WiFiEvent_t event, WiFiEventInfo_t info) | |
{ | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(IPAddress(info.got_ip.ip_info.ip.addr)); | |
} | |
void setup() | |
{ | |
Serial.begin(115200); | |
// delete old config | |
WiFi.disconnect(true); | |
delay(1000); | |
// Examples of different ways to register wifi events | |
WiFi.onEvent(WiFiEvent); | |
WiFi.onEvent(WiFiGotIP, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_GOT_IP); | |
WiFiEventId_t eventID = WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info){ | |
Serial.print("WiFi lost connection. Reason: "); | |
Serial.println(info.wifi_sta_disconnected.reason); | |
}, 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... "); | |
} | |
void loop() | |
{ | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment