Home / References / ESP32 Library / WiFi API / WiFiMulti
Description
The WiFiMulti.addAP
method is used to add multiple Access Points (APs). This allows the ESP32 to connect to different WiFi networks sequentially, providing a fallback mechanism if the primary network is unavailable. This method is particularly useful in IoT applications where reliable connectivity is crucial.
Syntax and Usage
The WiFiMulti.addAP
method can be used in the following way:
bool addAP(const char* ssid, const char* passphrase = NULL);
Adds an AP with the specified SSID and an optional passphrase. If the network is open (no password), the passphrase can be omitted or set to NULL
.
Arguments
ssid
: The SSID (name) of the Wi-Fi network. This is a required parameter.passphrase
: The password for the Wi-Fi network. This is an optional parameter; if omitted or set toNULL
, the network is assumed to be open.
Return Value
The method returns a boolean value:
true
: The AP was successfully added to the list.false
: The AP could not be added (e.g., due to invalid parameters).
Example Codes
This code demonstrates how to use the ESP32 WiFiMulti class’s addAP()
method to connect to multiple access points (APs). The addAP()
method allows you to specify different WiFi networks with their SSIDs and passwords. The ESP32 will automatically try to connect to the strongest available network from the list, making it ideal for environments with varying WiFi coverage.
To use this code, replace “SSID1”, “password1”, and other placeholders with your actual WiFi network credentials. Upload the code to your ESP32 and open the Serial Monitor at 115200 baud. The code adds multiple networks using addAP()
, connects to the best available one, and prints the assigned IP address once connected. Ensure the ESP32 is within range of at least one of the specified networks.
/*
* Author: Avant Maker
* Date: February 18, 2025
* Version: 1.0
* Description: This example code demonstrates how to
* use WiFiMulti.addAP method to add multiple APs and
* connect to the strongest available network.
*
* 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);
// Add multiple APs
wifiMulti.addAP("SSID1", "password1");
wifiMulti.addAP("SSID2", "password2");
wifiMulti.addAP("SSID3"); // Open network
Serial.println("Connecting...");
while (wifiMulti.run() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("\nConnected to WiFi!");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
}
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 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!