Home / References / ESP32 Library / WiFi API / Station
Description
The setAutoReconnect
method enables or disables automatic reconnection of the ESP32 to a Wi-Fi network. When auto-reconnect is enabled, the ESP32 will attempt to reconnect to the previously connected network if the connection is lost.
Syntax and Usage
This method takes a boolean argument to enable or disable auto-reconnect.
– Enable auto-reconnect:
WiFi.setAutoReconnect(true);
– Disable auto-reconnect:
WiFi.setAutoReconnect(false);
This method can be called at any time to modify the auto-reconnect behavior of the ESP32.
Argument(s)
- enable: A boolean value indicating whether auto-reconnect should be enabled (
true
) or disabled (false
).
Return Value
The setAutoReconnect
method does not return a value. It simply configures the auto-reconnect feature of the ESP32.
Example Codes
Example 1: Enable auto-reconnect
Note: Replace your_SSID
and your_PASSWORD
with your Wi-Fi network credentials. The code enables auto-reconnect, connects to the network, and the ESP32 will automatically attempt to reconnect if the network connection is lost.
/*
* Author: Avant Maker
* Date: February 7, 2025
* Version: 1.0
* Description: The example code demostrate how to use setAutoReconnect
* method to enable automatic reconnection of the ESP32 to a
* Wi-Fi network. When auto-reconnect is enabled, the ESP32
* will attempt to reconnect to the previously connected network
* if the connection is lost.
*
* License: MIT
*
* Code Source:
* This example code is sourced from the Comprehensive Guide
* to the ESP32 Arduino Core Library, accessible on AvantMaker.com.
* For additional code examples and in-depth documentation related to
* the ESP32 Arduino Core Library, please visit:
*
* https://avantmaker.com/home/all-about-esp32-arduino-core-library/
*
* AvantMaker.com, your premier destination for all things
* DIY, AI, IoT, Smart Home, and STEM projects. We are dedicated
* to empowering makers, learners, and enthusiasts with
* the resources they need to bring their innovative ideas to life.
*/
#include <WiFi.h>
const char* ssid = "your-SSID"; // Replace with your Wi-Fi SSID
const char* password = "your-PASSWORD"; // Replace with your Wi-Fi password
void setup() {
Serial.begin(115200);
// Set Auto Reconnect to True
WiFi.setAutoReconnect(true);
Serial.println("Auto-reconnect enabled");
// Connect WiFi
WiFi.begin(ssid, password);
Serial.println("Connecting to WiFi...");
// Wait
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected!");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
}
void loop() {
// Check WiFi Status
if (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi connection lost. Auto-reconnect should handle this.");
} else {
Serial.println("WiFi is still connected.");
}
delay(1000);
}
AvantMaker Tutorial Suggestion
If you want to learn more about the ESP32’s WiFi capabilities, we’ve created a beginner-friendly guide that explains them in detail. Just click the link below, and you’ll be teleported to that page. It will clear up any confusion you may have while connecting your ESP32 to WiFi or setting it up as a WiFi access point (AP).
ESP32 Library Index
- ESP32 WiFiClient Library
- ESP32 HTTPClient Library
- ESP32 WiFiClientSecure Library
- ESP32 WebServer Library
- ESP32 WiFi Library
- Which ESP32 Boards are Recommended for Learners
- How to Copy Codes from AvantMaker.com
- What is SPIFFS and how to upload files to it?
- What is LIttleFS and how to upload files to it?
Ready to experiment and explore more about ESP32? Visit our website’s All About ESP32 Resources Hub, packed with tutorials, guides, and tools to inspire your maker journey. Experiment, explore, and elevate your skills with everything you need to master this powerful microcontroller platform!