ESP32 WiFi Library – WiFi.getAutoReconnect()

Home / References / ESP32 Library / WiFi API / Station

Description

The getAutoReconnect method checks whether the ESP32 is configured to automatically reconnect to a Wi-Fi network if the connection is lost. This method is useful for determining the current auto-reconnect status of the device.


Syntax and Usage

This method returns a boolean value indicating whether auto-reconnect is enabled.

bool autoReconnect = WiFi.getAutoReconnect();

Argument(s)

This method does not accept any arguments.


Return Value

The getAutoReconnect method returns a bool value:

  • true if auto-reconnect is enabled.
  • false if auto-reconnect is disabled.

Example Codes

This code demonstrates how to use the ESP32 WiFi Library’s setAutoReconnect() and getAutoReconnect() methods to enable and verify the auto-reconnect feature. The auto-reconnect feature allows the ESP32 to automatically attempt reconnecting to the WiFi network if the connection is lost, ensuring continuous connectivity without manual intervention.

To use this code, replace “your_SSID” and “your_PASSWORD” with your WiFi network’s SSID and password. Upload the code to your ESP32 board and open the Serial Monitor at 115200 baud. The code will enable auto-reconnect, confirm its status, and connect to the WiFi network. If the connection drops, the ESP32 will automatically try to reconnect, making it ideal for applications requiring reliable network access.

/*
 * Author: Avant Maker
 * Date: February 7, 2025
 * Version: 1.0
 * Description: The example code demostrate how to use getAutoReconnect
 * method to check whether the ESP32 is configured to automatically
 * reconnect to a Wi-Fi 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/references/esp32-arduino-core-index/
 *
 * 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";
const char* password = "your_PASSWORD";

void setup() {
    Serial.begin(115200);

    // Enable auto-reconnect
    WiFi.setAutoReconnect(true);
    Serial.println("Enable auto-reconnect");

    // Check auto-reconnect status
    if (WiFi.getAutoReconnect()) {
        Serial.println("Auto-reconnect is enabled.");
    } else {
        Serial.println("Auto-reconnect is disabled.");
    }    

    // Connect to WiFi
    WiFi.begin(ssid, password);

    Serial.println("Connecting to WiFi...");
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println("\nConnected to WiFi!");
}

void loop() {
    // Your 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 Essential Guide – Network Section – WiFi Chapter

ESP32 Library Index

ESP32 Arduino Core Library


FAQ

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!

error: Content is protected !!