ESP32 WiFi Library – WiFiMulti.APlistClean

Home / References / ESP32 Library / WiFi API / WiFiMulti

Description

The APlistClean method in the WiFiMulti class of the ESP32 WiFi Library is used to clear the current list of Access Points (APs) stored in memory. This method frees the dynamically allocated memory used to store the SSIDs and passphrases added via the addAP method, effectively resetting the list of WiFi networks that the ESP32 can connect to. It is particularly useful when you need to refresh or update the list of APs dynamically during runtime, ensuring no stale or unwanted network credentials remain in memory.


Syntax and Usage

The APlistClean method is straightforward and has only one usage scenario. Below is how to invoke it:

wifiMulti.APlistClean();
  • Without Arguments: This method is called on a WiFiMulti object without any parameters. It iterates through the stored list of APs, frees the memory allocated for each SSID and passphrase, and clears the internal list, preparing it for new AP entries if needed.

Note: There are no alternative usages with arguments, as this method is designed solely to reset the AP list.


Argument(s)

This method does not require any arguments.


Return Value

The APlistClean method does not return any value (i.e., its return type is void). Its purpose is to perform an action—clearing the AP list and freeing memory—rather than providing feedback through a return value.


Example Codes

This code demonstrates how to use the ESP32 WiFiMulti class’s APlistClean() method to clear all previously added access points (APs) from the list. This is useful when you want to reset the WiFiMulti configuration and replace it with a new set of networks. After clearing the list, new APs can be added using the addAP() method, allowing for dynamic updates to the network list during runtime.

To use this code, replace “Network1”, “password1”, and other placeholders with your actual WiFi credentials. Upload the code to your ESP32 and open the Serial Monitor at 115200 baud. The code first adds two networks, clears the list using APlistClean(), and then adds new networks. It attempts to connect to the strongest available network using run(). If successful, it prints the SSID and IP address; otherwise, it notifies of the failure. Ensure the ESP32 is within range of the newly added networks for a successful connection.

/*
 * Author: Avant Maker
 * Date: February 18, 2025
 * Version: 1.0
 * Description: This example code demonstrates how to 
 * use APlistClean to reset the WiFiMulti AP list after 
 * adding some networks. It’s useful when you want to start 
 * fresh with a new set of APs.
 *
 * 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>
#include <WiFiMulti.h>

WiFiMulti wifiMulti;

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

    // Add some access points
    wifiMulti.addAP("Network1", "password1");
    wifiMulti.addAP("Network2", "password2");

    Serial.println("APs added. Now clearing the list...");
    
    // Clear the AP list
    wifiMulti.APlistClean();
    
    Serial.println("AP list cleared. Adding new APs...");
    
    // Add new APs after clearing
    wifiMulti.addAP("AvantMaker", "newpassword");
    wifiMulti.addAP("OfficeWiFi", "officepass");
    

    // Attempt to connect to the strongest available network
    if (wifiMulti.run() == WL_CONNECTED) {
        Serial.println("Connected to WiFi!");
        Serial.print("SSID: ");
        Serial.println(WiFi.SSID());
        Serial.print("IP Address: ");
        Serial.println(WiFi.localIP());
    } else {
        Serial.println("Failed to connect.");
    }
}

void loop() {
    // Nothing to do 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 !!