Home / References / ESP32 Library / WiFi API / AP
Description
The softAPdisconnect
method is used to disconnect clients or stop the Soft Access Point (AP) functionality provided by the ESP32’s WiFi library. When called, it either disconnects all currently connected clients or completely shuts down the soft AP, depending on the parameters used.
This method was introduced in ESP32 Arduino version 1.0.1 and is part of the core WiFi library functionality for managing the soft AP mode. It allows developers to control the network state of the ESP32 when operating as an AP, which is useful for scenarios like conserving resources or switching network modes dynamically.
Syntax and Usage
– Force Disconnection and Disable Wi-Fi:
WiFi.softAPdisconnect(wifioff);
When the wifioff
parameter is set to true
, the method forcefully disconnects all clients and disables the soft AP immediately. Additionally, the Wi-Fi radio is turned off, which prevents any further network activity. This can be useful for scenarios where a clean shutdown of the network is required without allowing clients to reconnect or using the Wi-Fi resources.
– Force Disconnection Without Turning Off Wi-Fi:
WiFi.softAPdisconnect();
When called without parameters (or with false
for the wifioff
parameter), this method will disconnect all clients from the soft AP normally. Clients may attempt to reconnect to it but connected devices are no longer allowed to communicate through it.
Argument(s)
wifioff
– [Optional, Boolean] Specifies whether to turn off the Wi-Fi radio after disconnecting clients. The default value isfalse
. When set totrue
, the Wi-Fi radio is disabled, entering a lower power state and preventing any future network activity until re-enabled.
Return Value
The method returns a boolean value. Returns true
if the disconnection and any associated configuration changes (such as turning the Wi-Fi radio off) were completed successfully. Returns false
if an error occurred during the operation, such as invalid parameters or a failure to execute the requested action.
Example Codes
Example 1: Normal Client Disconnect
This code demonstrates how to use the ESP32 AP (Access Point) class’s softAPdisconnect()
method to disconnect all clients connected to the soft AP while keeping the WiFi radio active. The method ensures that connected devices are no longer allowed to communicate through the Access Point, effectively isolating them from the network. Clients may attempt to reconnect to it, but connected devices are no longer allowed to communicate through it. This functionality is useful in scenarios where you need to temporarily disable client access without turning off the entire WiFi radio, such as during maintenance or reconfiguration.
To use this code, upload it to your ESP32 and open the Serial Monitor at 115200 baud. The ESP32 starts a soft AP named “AvantMaker-ESP32-AP” and waits for 30 seconds to allow clients to connect. Afterward, the softAPdisconnect()
method is called to disable client connections while keeping the WiFi radio active. Observe the Serial Monitor for confirmation messages about the soft AP’s status. Modify the delay or AP name if needed, and ensure no critical devices rely on the connection during testing.
/*
* Author: Avant Maker
* Date: February 7, 2025
* Version: 1.0
* Description: This example shows how to use ESP32 WiFi Library's softAPdisconnect method
* to disconnect clients from the soft AP normally. The AP remains active (Wi-Fi radio on),
* but connected devices are no longer allowed to communicate through it.
*
* 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>
void setup() {
Serial.begin(115200);
// Start the softAP
bool softAPStart = WiFi.softAP("AvantMaker-ESP32-AP");
if (softAPStart) {
Serial.println("softAP started successfully.");
} else {
Serial.println("Failed to start softAP mode.");
}
// Wait a bit
delay(30000);
// Disable the softAP
bool result = WiFi.softAPdisconnect();
if (result) {
Serial.println("softAP mode disabled successfully.");
} else {
Serial.println("Failed to disable softAP mode.");
}
}
void loop() {
// Empty loop
}
Example 2: Force AP Shutdown and Turn Wi-Fi Off
This code demonstrates how to use the ESP32 AP (Access Point) class’s softAP()
and softAPdisconnect()
methods to start and fully disable a soft AP. Initially, the ESP32 creates a WiFi Access Point named “AvantMaker-ESP32-AP” using the softAP()
method. After a 30-second delay to allow clients to connect, the softAPdisconnect(true)
method is called to not only disconnect all clients but also turn off the WiFi radio entirely. This ensures that the ESP32 stops functioning as an Access Point and disables all WiFi functionality, saving power or preparing for other tasks.
To use this code, upload it to your ESP32 and open the Serial Monitor at 115200 baud. The ESP32 will start the soft AP and print a confirmation message if successful. After 30 seconds, it disables the soft AP and turns off the WiFi radio, with the results displayed in the Serial Monitor. Modify the AP name or delay as needed for your application. Ensure no devices rely on the WiFi connection during testing, as the network will be completely disabled after the process.
/*
* Author: Avant Maker
* Date: February 7, 2025
* Version: 1.0
* Description: This example shows how to use ESP32 WiFi Library's softAPdisconnect method
* to completely shut down soft AP, and turn off Wi-Fi radio after disconnecting all clients.
*
* 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>
void setup() {
Serial.begin(115200);
// Start the softAP
bool softAPStart = WiFi.softAP("AvantMaker-ESP32-AP");
if (softAPStart) {
Serial.println("softAP started successfully.");
} else {
Serial.println("Failed to start softAP mode.");
}
// Wait a bit
delay(30000);
// Disable the softAP
bool result = WiFi.softAPdisconnect(true);
if (result) {
Serial.println("softAP mode and WiFi disabled successfully.");
} else {
Serial.println("Failed to disable softAP mode.");
}
}
void loop() {
// Empty loop
}
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!