Home / References / ESP32 Library / WiFi API / Station
Description
The reconnect
method in the ESP32 WiFi library is used to re-establish the WiFi connection if it has been disconnected. It attempts to reconnect to the previously configured WiFi network. This method is particularly useful in scenarios where the connection is lost due to network issues or other interruptions.
Syntax and Usage
The reconnect
method can be used as follows:
Call the method without arguments to attempt reconnecting to the saved WiFi network using the previously configured SSID and password.
WiFi.reconnect();
Argument(s)
The reconnect
method does not accept any arguments.
Return Value
The reconnect
method does not return any value. It initiates the reconnection process and relies on the WiFi event system to notify whether the reconnection was successful or not.
Example Codes
Below are examples demonstrating how to use the reconnect
method:
Example 1: Reconnect to WiFi
This example demonstrates how to use the reconnect
method to re-establish the WiFi connection if it gets disconnected:
Note: Replace YourSSID
and YourPassword
with your WiFi credentials. This example continuously monitors the WiFi connection and attempts to reconnect if disconnected.
/*
* Author: Avant Maker
* Date: February 7, 2025
* Version: 1.0
* Description: This example code demonstrates how to use the reconnect
* method to re-establish the ESP32's WiFi connection if it gets disconnected.
*
* 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 nnovative ideas to life.
*/
#include <WiFi.h> // Include the ESP32 WiFi library
// Define the WiFi credentials
const char* ssid = "your_SSID"; // Replace with your WiFi network's SSID
const char* password = "your_PASSWORD"; // Replace with your WiFi network's password
void setup() {
// Initialize Serial communication for debugging
Serial.begin(115200);
// Start connecting to the WiFi network using SSID and password
Serial.println("Connecting to WiFi...");
WiFi.begin(ssid, password);
// Wait until connected
while (WiFi.status() != WL_CONNECTED) {
delay(1000); // Wait for 1 second
Serial.println("Attempting to connect...");
}
// Once connected, print the IP address
Serial.println("WiFi connected!");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP()); // Print the local IP address assigned to the ESP32
// Simulate a WiFi disconnection after 2 seconds
delay(2000);
Serial.println("Disconnecting from WiFi...");
// Disconnect from the WiFi network
WiFi.disconnect();
Serial.println("WiFi disconnected.");
// Wait for 5 seconds before reconnecting
delay(5000);
// Attempt to reconnect to the WiFi network
Serial.println("Attempting to reconnect to WiFi...");
WiFi.reconnect(); // This will attempt to reconnect to the last connected WiFi network
// Wait until reconnected
while (WiFi.status() != WL_CONNECTED) {
delay(1000); // Wait for 1 second
Serial.println("Attempting to reconnect...");
}
// Once reconnected, print the new IP address
Serial.println("WiFi reconnected!");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP()); // Print the local IP address assigned to the ESP32
}
void loop() {
// Main loop does nothing in this example, but you can add your own code here
}
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!