AP mode allows an ESP32 or ESP8266 to create its own WiFi so other devices can connect directly to it.
When we use Access Point mode (AP), the board stops being a client of an existing network and starts creating its own network. This is useful for initial setup, isolated networks, portable devices, or projects that do not depend on an external router.
In a way, the board behaves “like your home router”, except that your home router shares an Internet connection with the connected devices. Our ESP32, however, does not share “anything”; it simply acts as an AP for other devices. This mode is called “SoftAP”.
The example is oriented to ESP32. In many cases it can also be adapted to ESP8266 by changing libraries and a few pin details.
Create a WiFi Network
Using AP mode to create a WiFi network is just as easy as using STA mode to connect to an existing WiFi, thanks to the same WiFi library.
To generate the WiFi network we use the function WiFi.softAP(…), which returns true if the WiFi network was created successfully and false otherwise.
WiFi.softAP(ssid, passphrase = NULL, channel = 1, ssid_hidden = 0, max_connection = 4)
- ssid, name of the WiFi network we will generate (up to 32 chars)
- password, optional, WiFi password (min 8, max 63 chars)
- channel, optional, WiFi channel to use (1 to 13)
- hidden, optional, if true the SSID will not be broadcast
- max_connection, optional, maximum number of connections (max 8)
Therefore, an example to create a WiFi network with the ESP32 in AP mode would be the following:
#include <WiFi.h> // Include the Wi-Fi library
// Configuration of the generated WiFi
const char *ssid = "ssid";
const char *password = "password";
void setup() {
Serial.begin(115200);
delay(10);
WiFi.mode(WIFI_AP);
while(!WiFi.softAP(ssid, password))
{
Serial.println(".");
delay(100);
}
Serial.print("Started AP ");
Serial.println(ssid);
Serial.print("IP address:\t");
Serial.println(WiFi.softAPIP());
}
void loop() { }
By default, 4 devices can connect to the generated network, although the maximum number of stations that can connect simultaneously can be changed from 0 to 8. Once the maximum number is reached, any other station trying to connect will be forced to wait until a connected station disconnects.
On the other hand, the WiFi.mode(…) function sets the WiFi operating mode, which can be:
| WIFI_OFF | Off |
|---|---|
| WIFI_STA | Station |
| WIFI_AP | Access point |
| WIFI_AP_STA | Station+Access Point |
Just like in STA mode, the network credentials are stored in memory, which persists even if we reprogram the board. Therefore, if we want to disable AP mode we must do so explicitly by calling the WiFi.mode(…) function. Otherwise, the device will generate its own WiFi even after being reprogrammed, even if we have not used the WiFi.softAP(…) function in the new program.
Summarized Example
If we split our code to make it simpler and reusable,
#include <WiFi.h>
#include "config.h" // Replace with your network data
#include "ESP32_Utils.hpp"
void setup()
{
Serial.begin(115200);
ConnectWiFi_AP();
}
void loop()
{
}
An additional file called ‘ESP32_Utils.hpp’ containing the connection functions.
void ConnectWiFi_AP()
{
Serial.println("");
WiFi.mode(WIFI_AP);
while(!WiFi.softAP(ssid, password))
{
Serial.println(".");
delay(100);
}
Serial.println("");
Serial.print("Started AP:\t");
Serial.println(ssid);
Serial.print("IP address:\t");
Serial.println(WiFi.softAPIP());
}
A ‘config.h’ file with our WiFi data
const char* ssid = "ssid";
const char* password = "password";
With this we can already use the ESP32 as an access point. It is a very practical configuration for setup assistants, temporary local networks, or devices that need to work without depending on an external router.
Download the Code
All the code from this post is available for download on Github.
Version for ESP8266: https://github.com/luisllamasbinaburo/ESP8266-Examples
Version for ESP32: https://github.com/luisllamasbinaburo/ESP32-Examples

