Inquire: Call 0086-755-23203480, or reach out via the form below/your sales contact to discuss our design, manufacturing, and assembly capabilities.
Quote: Email your PCB files to Sales@pcbsync.com (Preferred for large files) or submit online. We will contact you promptly. Please ensure your email is correct.
Notes: For PCB fabrication, we require PCB design file in Gerber RS-274X format (most preferred), *.PCB/DDB (Protel, inform your program version) format or *.BRD (Eagle) format. For PCB assembly, we require PCB design file in above mentioned format, drilling file and BOM. Click to download BOM template To avoid file missing, please include all files into one folder and compress it into .zip or .rar format.
ESP32 WiFi: Station & Access Point Modes Explained
The ESP32 WiFi capabilities make this microcontroller one of the most versatile choices for IoT and embedded projects. Unlike simpler WiFi modules that only connect to existing networks, the ESP32 can operate as a client, create its own network, or do both simultaneously. Understanding these different modes is essential for designing robust wireless applications.
I’ve deployed ESP32-based systems in everything from industrial monitoring setups to portable data loggers, and choosing the right WiFi mode often determines whether a project succeeds or becomes a maintenance headache. This guide explains each ESP32 WiFi mode, when to use it, and provides working code you can adapt for your own Arduino projects.
Understanding ESP32 WiFi Operating Modes
The ESP32 WiFi subsystem supports three distinct operating modes, each serving different networking requirements. Before diving into code, it’s worth understanding what each mode actually does at the network level.
The Three ESP32 WiFi Modes
Mode
Function Code
Description
Typical Use Case
Station (STA)
WIFI_STA
Connects to existing WiFi network
IoT devices connecting to home/office router
Access Point (AP)
WIFI_AP
Creates its own WiFi network
Direct device-to-device communication
Dual Mode (AP+STA)
WIFI_AP_STA
Runs both modes simultaneously
Configuration portals, network bridging
The mode is set using the WiFi.mode() function from the WiFi.h library. This fundamental decision shapes your entire network architecture, so let’s examine each option in detail.
ESP32 WiFi Station Mode (WIFI_STA)
Station mode is what most people think of when they imagine a WiFi-connected device. Your ESP32 behaves like a laptop or smartphone—it scans for available networks, authenticates with credentials, and receives an IP address from the router’s DHCP server.
When to Use Station Mode
Station mode works best when your ESP32 needs internet connectivity or must communicate with other devices on an existing network. Typical applications include sending sensor data to cloud services, receiving commands from a home automation server, or fetching information like weather data or time synchronization.
The main advantage is straightforward: your ESP32 becomes part of your existing network infrastructure. Any device on that network can communicate with it, and if the network has internet access, your ESP32 does too.
Station Mode Connection Code
Here’s a reliable station mode implementation with proper error handling:
#include <WiFi.h>
const char* ssid = “YourNetworkName”;
const char* password = “YourPassword”;
void setup() {
Serial.begin(115200);
delay(1000);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print(“Connecting to WiFi”);
int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 30) {
delay(500);
Serial.print(“.”);
attempts++;
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println(“\nConnected!”);
Serial.print(“IP Address: “);
Serial.println(WiFi.localIP());
Serial.print(“Signal Strength (RSSI): “);
Serial.print(WiFi.RSSI());
Serial.println(” dBm”);
} else {
Serial.println(“\nConnection failed!”);
}
}
void loop() {
// Your application code
}
ESP32 WiFi Status Codes Reference
When troubleshooting connection issues, understanding the status codes returned by WiFi.status() saves hours of debugging:
Status Code
Meaning
Common Cause
WL_IDLE_STATUS (0)
Temporary status during connection
Normal during WiFi.begin()
WL_NO_SSID_AVAIL (1)
Network not found
Wrong SSID, out of range, or network down
WL_SCAN_COMPLETED (2)
Network scan finished
Informational only
WL_CONNECTED (3)
Successfully connected
Normal operation
WL_CONNECT_FAILED (4)
Connection attempt failed
Wrong password or authentication issue
WL_CONNECTION_LOST (5)
Connection dropped
Signal interference, router restart, power issues
WL_DISCONNECTED (6)
Not connected
WiFi.disconnect() called or never connected
Setting a Static IP Address in Station Mode
DHCP works fine for most projects, but production devices often need predictable IP addresses. Here’s how to configure a static IP:
#include <WiFi.h>
const char* ssid = “YourNetwork”;
const char* password = “YourPassword”;
IPAddress staticIP(192, 168, 1, 200);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress dns(8, 8, 8, 8);
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.config(staticIP, gateway, subnet, dns);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(“.”);
}
Serial.println(“\nConnected with static IP:”);
Serial.println(WiFi.localIP());
}
void loop() {}
The WiFi.config() call must come before WiFi.begin() for the static settings to take effect.
ESP32 WiFi Access Point Mode (WIFI_AP)
Access Point mode transforms your ESP32 into a WiFi router—other devices connect directly to the ESP32 rather than through existing infrastructure. This is technically called a “Soft AP” (software-enabled access point) since the ESP32 simulates router functionality in software.
When to Use Access Point Mode
AP mode shines in scenarios where no existing network is available or when you need direct device-to-device communication. Field-deployed sensors, portable measurement equipment, and initial device configuration are prime candidates.
One of my favorite applications is using AP mode for first-time device setup. A new ESP32 device creates its own network, users connect with their phone, configure WiFi credentials through a web interface, and then the device restarts in station mode to join the home network. This “captive portal” pattern appears in countless commercial IoT products.
Basic Access Point Configuration
Creating a WiFi network with ESP32 requires just a few lines of code:
#include <WiFi.h>
const char* ap_ssid = “ESP32_Network”;
const char* ap_password = “securepassword123”;
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_AP);
WiFi.softAP(ap_ssid, ap_password);
Serial.println(“Access Point Started”);
Serial.print(“AP IP Address: “);
Serial.println(WiFi.softAPIP());
}
void loop() {}
By default, the ESP32 assigns itself the IP address 192.168.4.1 and provides DHCP to connected clients in the 192.168.4.x range.
ESP32 softAP() Function Parameters
The softAP() function accepts several optional parameters for fine-tuning your network:
The default 192.168.4.x range works, but you might need a different subnet for your application:
#include <WiFi.h>
const char* ap_ssid = “ESP32_CustomIP”;
const char* ap_password = “password123”;
IPAddress local_IP(10, 0, 0, 1);
IPAddress gateway(10, 0, 0, 1);
IPAddress subnet(255, 255, 255, 0);
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(local_IP, gateway, subnet);
WiFi.softAP(ap_ssid, ap_password);
Serial.print(“AP IP: “);
Serial.println(WiFi.softAPIP());
}
void loop() {}
The softAPConfig() function must be called before softAP() for the custom IP settings to apply.
Monitoring Connected Clients
Tracking connected devices helps with debugging and security:
void loop() {
int connectedClients = WiFi.softAPgetStationNum();
Serial.print(“Connected clients: “);
Serial.println(connectedClients);
delay(5000);
}
ESP32 WiFi Dual Mode (WIFI_AP_STA)
Dual mode is where the ESP32 really shows its capabilities. Running as both station and access point simultaneously opens up powerful networking scenarios that would require two separate devices with simpler WiFi modules.
When to Use Dual Mode
The most practical application I’ve encountered is the WiFi configuration portal. Your device ships with no network credentials, so it starts in AP mode. Users connect to the ESP32’s network, enter their home WiFi credentials through a web page, and the ESP32 uses station mode to verify the credentials work—all without losing the AP connection. Once confirmed, the device saves credentials and restarts in station-only mode.
Other applications include creating a local configuration interface while maintaining cloud connectivity, or acting as a bridge between WiFi networks (though the ESP32 can’t route traffic between interfaces without additional code).
Dual Mode Implementation
Here’s a working dual-mode configuration:
#include <WiFi.h>
// Station mode credentials (your existing network)
Running both interfaces has some constraints worth understanding:
Consideration
Details
Channel Limitation
Both interfaces must use the same WiFi channel. The station interface follows the router’s channel, forcing the AP to match.
Power Consumption
Dual mode increases current draw, important for battery-powered applications.
Processing Overhead
Managing two interfaces adds CPU load—factor this into timing-critical applications.
IP Addresses
Each interface has its own IP. Use WiFi.localIP() for station and WiFi.softAPIP() for AP.
ESP32 WiFi Power Consumption by Mode
For battery-powered projects, understanding the power implications of each mode is critical:
Operating State
Typical Current Draw
Notes
WiFi TX (transmitting)
180-240 mA
Peak during data transmission
WiFi RX (receiving)
95-100 mA
Listening for packets
Modem Sleep
20-30 mA
WiFi maintained, CPU active
Light Sleep
0.8 mA
WiFi paused, CPU sleeping
Deep Sleep
10-150 µA
Everything off except RTC
Access Point mode generally consumes more power than Station mode because the ESP32 must continuously beacon to advertise the network and handle potential connection requests. For battery-powered devices, consider using Station mode with aggressive sleep patterns.
ESP32 WiFi Events and Callbacks
Rather than polling WiFi.status() in a loop, the ESP32 supports event-driven WiFi programming:
#include <WiFi.h>
const char* ssid = “YourNetwork”;
const char* password = “YourPassword”;
void WiFiEvent(WiFiEvent_t event) {
switch (event) {
case ARDUINO_EVENT_WIFI_STA_START:
Serial.println(“Station mode started”);
break;
case ARDUINO_EVENT_WIFI_STA_CONNECTED:
Serial.println(“Connected to AP”);
break;
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
Serial.print(“Got IP: “);
Serial.println(WiFi.localIP());
break;
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
Serial.println(“Disconnected from AP”);
WiFi.reconnect();
break;
case ARDUINO_EVENT_WIFI_AP_STACONNECTED:
Serial.println(“Client connected to AP”);
break;
case ARDUINO_EVENT_WIFI_AP_STADISCONNECTED:
Serial.println(“Client disconnected from AP”);
break;
default:
break;
}
}
void setup() {
Serial.begin(115200);
WiFi.onEvent(WiFiEvent);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
}
void loop() {
// Main application code runs independently of WiFi handling
}
This approach keeps your main loop clean and ensures immediate response to network changes.
Troubleshooting ESP32 WiFi Connection Issues
After debugging hundreds of ESP32 WiFi problems, here are the most common issues and solutions:
WiFiManager: github.com/tzapu/WiFiManager (captive portal for easy configuration)
AsyncTCP + ESPAsyncWebServer: Asynchronous web server for responsive applications
Development Tools:
Arduino IDE with ESP32 Board Package
PlatformIO (recommended for larger projects)
ESP-IDF (for advanced development)
Frequently Asked Questions
Can ESP32 WiFi work on both 2.4GHz and 5GHz bands?
No, the ESP32 only supports 2.4GHz WiFi (802.11 b/g/n). If your router broadcasts separate 2.4GHz and 5GHz networks, make sure your code specifies the 2.4GHz SSID. Dual-band routers with a single SSID may cause connection issues if they steer devices toward 5GHz—consider creating a dedicated 2.4GHz network for your ESP32 devices.
How many devices can connect to an ESP32 in Access Point mode?
The default maximum is 4 simultaneous connections, configurable up to 10 on some ESP32 variants. In practice, I recommend keeping it at 4 or fewer for reliable performance. Each connected client adds overhead, and the ESP32’s limited RAM means performance degrades as connections increase. For applications needing more clients, consider using a proper router with the ESP32 in station mode.
Does ESP32 WiFi support WPA3 security?
Yes, ESP32 supports WPA3 starting with ESP-IDF 4.0 and Arduino core 2.0. However, WPA3 support requires both the ESP32 firmware and your router to support it. Most projects still use WPA2, which remains secure for typical applications. To enable WPA3, ensure you’re using recent firmware and the router has WPA3 enabled.
Why does my ESP32 WiFi connection drop after a few hours?
Intermittent disconnections usually indicate power supply problems or aggressive router settings. First, ensure your power supply can deliver at least 500mA during WiFi transmission peaks. Add a 100-470µF electrolytic capacitor near the ESP32’s power pins. Also check your router’s DHCP lease time and any “inactive client timeout” settings. Implementing automatic reconnection using WiFi events (shown earlier) makes your device more resilient.
Can I use ESP32 WiFi and Bluetooth simultaneously?
Yes, but with caveats. WiFi and Bluetooth share the same 2.4GHz radio, so the ESP32 uses time-division multiplexing to handle both. This works for low-bandwidth applications but can cause issues with high-throughput WiFi transfers or latency-sensitive Bluetooth audio. For best results, avoid simultaneous heavy usage of both radios, and consider using BLE (Bluetooth Low Energy) instead of Classic Bluetooth when WiFi is active.
Choosing the Right ESP32 WiFi Mode for Your Project
Selecting the appropriate WiFi mode comes down to understanding your networking requirements:
Choose Station Mode when your ESP32 needs to access the internet, communicate with cloud services, or integrate into an existing network infrastructure. This is the go-to mode for most IoT applications.
Choose Access Point Mode when you need direct device communication without existing infrastructure, such as portable equipment, initial device configuration, or remote field deployments where no router exists.
Choose Dual Mode when you need the flexibility of both—typically for configuration interfaces that maintain internet connectivity, or for advanced networking scenarios like creating local control access while maintaining cloud connections.
The ESP32 WiFi implementation is remarkably robust once you understand these fundamentals. Start with the basic examples in this guide, verify they work with your hardware, then expand to meet your specific application needs. The combination of flexible modes, event-driven programming, and comprehensive library support makes ESP32 WiFi projects achievable even for complex networking requirements.
Inquire: Call 0086-755-23203480, or reach out via the form below/your sales contact to discuss our design, manufacturing, and assembly capabilities.
Quote: Email your PCB files to Sales@pcbsync.com (Preferred for large files) or submit online. We will contact you promptly. Please ensure your email is correct.
Notes: For PCB fabrication, we require PCB design file in Gerber RS-274X format (most preferred), *.PCB/DDB (Protel, inform your program version) format or *.BRD (Eagle) format. For PCB assembly, we require PCB design file in above mentioned format, drilling file and BOM. Click to download BOM template To avoid file missing, please include all files into one folder and compress it into .zip or .rar format.