Home / References / ESP32 Library / WiFi API / Station
Description
The status
method in the ESP32 WiFi library is used to retrieve the current WiFi connection status of the ESP32. This method returns the WiFi connection state, which can be used to determine whether the device is connected to a network, attempting to connect, or disconnected. It is a key method for monitoring and debugging WiFi connectivity.
Syntax and Usage
The status
method can be used as follows:
WiFi.status();
Call the method without any argument to retrieve the current WiFi connection status.
For practical applications and examples of this method, please consult the “Example Code” section on this page. This section provides comprehensive guidance to help you better understand and apply the method effectively.
Argument
The status
method does not accept any argument.
Return Value
The status()
method returns a status code of the connection status. This following list describes the various status codes that indicate the current state of the WiFi connection. Understanding these statuses can help in debugging connection issues and ensuring smooth operation of your IoT projects.
- WL_IDLE_STATUS (0): The WiFi radio is in an idle state, not attempting to connect.
- WL_NO_SSID_AVAIL (1): The configured SSID is not available or cannot be found.
- WL_SCAN_COMPLETED (2): A WiFi network scan has been completed successfully.
- WL_CONNECTED (3): Successfully connected to the configured WiFi network.
- WL_CONNECT_FAILED (4): Failed to connect to the configured WiFi network, possibly due to incorrect credentials or other issues.
- WL_CONNECTION_LOST (5): The connection to the WiFi network was lost, indicating a problem maintaining the connection.
- WL_DISCONNECTED (6): The WiFi module is disconnected from any WiFi network.
Example Codes
Below are example codes demonstrating how to use the status()
method.
Example 1: Checking WiFi Connection Status
This example demonstrates how to connect to a WiFi network and periodically check the connection status using status()
method.
Note: Replace Your_SSID
and Your_Password
with your WiFi credentials. Monitor the Serial output to observe the current WiFi status.
/*
* Author: Avant Maker
* Date: March 26, 2025
* Version: 1.0
* Description:
* This code demonstrates how to continuously
* check ESP32's Wi-Fi connection status
* with WiFi.status().
*
* 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 innovative ideas to life.
*/
#include <WiFi.h>
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
void setup() {
Serial.begin(115200);
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() {
int status = WiFi.status();
if (status == WL_CONNECTED) {
Serial.println("WiFi is connected.");
} else {
Serial.println("WiFi is not connected.");
}
delay(2000);
}
Example 2: Handling Different Connection States
This example shows how to handle different WiFi connection states using a switch statement with WiFi.status()
.
Note: Replace Your_SSID
and Your_Password
with your WiFi credentials. Monitor the Serial output to observe the current WiFi status.
/*
* Author: Avant Maker
* Date: March 26, 2025
* Version: 1.0
* Description:
* This code demonstrates how to connect
* to a WiFi network and periodically check
* the connection status. It initializes the
* serial communication and attempts to connect
* to a specified WiFi network. In the main loop,
* it checks the WiFi connection status every 2
* seconds and prints the current status
* to the serial monitor.
*
* 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 innovative ideas to life.
*/
#include <WiFi.h>
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
void setup() {
Serial.begin(115200);
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() {
// Check WiFi status
int wifiStatus = WiFi.status();
switch (wifiStatus) {
case WL_CONNECTED:
Serial.println("WiFi is connected.");
break;
case WL_NO_SSID_AVAIL:
Serial.println("No SSID available.");
break;
case WL_CONNECT_FAILED:
Serial.println("WiFi connection failed.");
break;
case WL_IDLE_STATUS:
Serial.println("WiFi is idle.");
break;
case WL_DISCONNECTED:
Serial.println("WiFi is disconnected.");
break;
default:
Serial.println("Unknown WiFi status.");
break;
}
delay(5000); // Check status every 5 seconds
}
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!