Home / References / ESP32 Library / WiFi API / Station
Description
The disconnect method in the ESP32 WiFi library is used to terminate the current Wi-Fi connection. It provides options to control additional behaviors such as turning off the Wi-Fi radio, clearing saved access point configurations, and setting a custom timeout for the disconnection process.
Syntax and Usage
The disconnect method can be used in the following ways:
– Without argument:
WiFi.disconnect()
This disconnects from the current WiFi network using default settings.
– With arguments:
WiFi.disconnect(wifioff, eraseap, timeoutLength)
This allows you to specify whether to turn off the WiFi radio, erase network credentials, and set a custom timeout.
Arguments
- wifioff (optional): A boolean value
- If true, the WiFi radio will be turned off after disconnecting.
- If false or not specified, the WiFi radio remains on after disconnecting.
- Default: false
- eraseap (optional): A boolean value
- If true, the stored AP configuration (SSID and password) will be erased.
- If false or not specified, the stored AP configuration remains intact.
- Default: false
- timeoutLength (optional): An unsigned long value
- Specifies the timeout for the disconnection process in milliseconds.
- Default: 100 milliseconds
Return Value
The disconnect method returns a boolean value:
- true: If the disconnection was successful
- false: If the disconnection failed or timed out
Example Code
/*
* Author: Avant Maker
* Date: February 7, 2025
* Version: 1.0
* Description: This code demonstrates how to connect and
* disconnect from a WiFi network using the ESP32's WiFi library.
* It includes examples of demonstrating two methods of disconnecting
* from the WiFi network - one with default settings and another
* with custom settings that include turning off the radio,
* erasing AP configuration, and applying a timeout.
*
* 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.
*/
const char* ssid = "your-SSID"; // Replace with your Wi-Fi SSID
const char* password = "your-PASSWORD"; // Replace with your Wi-Fi password
#include <WiFi.h>
void setup() {
Serial.begin(115200);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Wait for 5 seconds
delay(5000);
// Example 1: Disconnect with default settings
if (WiFi.disconnect()) {
Serial.println("Disconnected from WiFi with default settings");
} else {
Serial.println("Failed to disconnect from WiFi");
}
delay(5000);
// Reconnect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Reconnecting to WiFi...");
}
Serial.println("Reconnected to WiFi");
// Example 2: Disconnect with custom settings
if (WiFi.disconnect(true, true, 500)) {
Serial.println("Disconnected, radio off, AP config erased, with 500ms timeout");
} else {
Serial.println("Failed to disconnect with custom settings");
}
}
void loop() {
// Your code here
}
Usage Notes
- Use WiFi.disconnect() for a quick disconnection with default settings.
- Use WiFi.disconnect(true, false, timeout) to disconnect and turn off WiFi radio with a custom timeout.
- Use WiFi.disconnect(true, true, timeout) to disconnect, turn off WiFi, clear saved network credentials, and set a custom timeout.
- After disconnecting, you may need to call WiFi.begin() again to reconnect to a network.
- The timeoutLength argument can be useful in scenarios where you need more (or less) time for the disconnection process to complete.
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!