Skip to content

uPesy only delivers currently in France.

Contents Menu Expand Light mode Dark mode Auto light/dark mode

Create a Wi-Fi access point with an ESP32

The Access Point mode allows you to use the ESP32 to create a Wi-Fi network to connect. This is similar to Wi-Fi connection sharing available on phones. As with phones, the operation of a Wi-Fi box is simulated: this is known as a Soft AP (for ‘software’ Wi-Fi access point). You should therefore not expect the same performance as with a conventional Wi-Fi router, especially on a microcontroller!

Also, note that unlike the phone’s connection sharing, the ESP32 is not connected to the Internet. So you can just use the Access Point mode to create a private Wi-Fi local area network wholly isolated from the Internet.

Here are some uses of the Access Point mode:
  • Connecting to the ESP32 in Access Point mode temporarily to enter the credentials of its Wi-Fi box and allow the ESP32 to connect to our classic Wi-Fi network. Most connected objects use this principle to connect to the home Wi-Fi.

  • Have a separate network of your home network and not connected to the Internet.

  • To easily communicate between several ESP32 by Wi-Fi.

  • To have a local server on an isolated Wi-Fi network.

Set the ESP32 to Access Point mode (AP):

Simply use WiFi.mode(WIFI_AP) and WiFi.softAP() to enable Access Point mode. Here is a straightforward example of how to create a Wi-Fi network with an ESP32:

#include <WiFi.h>

const char* ssid     = "uPesy_AP";
const char* password = "super_strong_password";

void setup()
{
    Serial.begin(115200);
    Serial.println("\n[*] Creating AP");
    WiFi.mode(WIFI_AP);
    WiFi.softAP(ssid, password);
    Serial.print("[+] AP Created with IP Gateway ");
    Serial.println(WiFi.softAPIP());
}

void loop(){}

Hint

Don’t forget to import the WiFi.h module so that the code works with #include <WiFi.h> .

Terminal outpout

[*] Creating AP
 [+] AP Created with IP Gateway 192.168.4.1

You can then connect to the Wi-Fi network by entering the password. In this example, it’s a computer on Windows 10 that will connect to ESP32.

seo

Connection to the ESP32 Wi-Fi network

You can also create an open Wi-Fi network by setting NULL for the password:

#include <WiFi.h>

const char* ssid     = "uPesy_AP";
const char* password = NULL;

void setup()
{
    Serial.begin(115200);
    Serial.println("\n[*] Creating AP");
    WiFi.mode(WIFI_AP);
    WiFi.softAP(ssid, password);
    Serial.print("[+] AP Created with IP Gateway ");
    Serial.println(WiFi.softAPIP());
}

void loop(){}

The access point appears as an open network:

seo

Connect to the ap without password

Hint

The IP displayed in the terminal corresponds to the router’s local IP on the new Wi-Fi network.

The access point can also be further customized by specifying the Wi-Fi channel used (more information on the Wi-Fi channels are available on Wikipedia ), the visibility or not of the network as well as the maximum number of devices that can be connected at the same time.

#include <WiFi.h>

const char* ssid           = "uPesy_AP";                // SSID Name
const char* password       = "super_strong_password";   // SSID Password - Set to NULL to have an open AP
const int   channel        = 10;                        // WiFi Channel number between 1 and 13
const bool  hide_SSID      = false;                     // To disable SSID broadcast -> SSID will not appear in a basic WiFi scan
const int   max_connection = 2;                         // Maximum simultaneous connected clients on the AP

void setup()
{
    Serial.begin(115200);
    Serial.println("\n[*] Creating AP");
    WiFi.mode(WIFI_AP);
    WiFi.softAP(ssid, password, channel, hide_SSID, max_connection);
    Serial.print("[+] AP Created with IP Gateway ");
    Serial.println(WiFi.softAPIP());
}

void loop(){}

Once connected to the Wi-Fi network from a device, one can then ping the router, in this case, the ESP32 in the Windows command prompt with:

ping 192.168.4.1
seo

Ping the ESP32 from Windows terminal

We have our own local Wi-Fi router on an ESP32 on which one can add several devices (computer, phone, esp32, raspberry pi …)

Examples of application:

Here is an example displaying a list of devices connected to ESP32 with their MAC addresses and local IP addresses. This is a simplified version of the one you can have on a classic Wi-Fi box.

#include <WiFi.h>
#include "esp_wifi.h"

const char* ssid           = "uPesy_AP";                // SSID Name
const char* password       = "super_strong_password";   // SSID Password - Set to NULL to have an open AP
const int   channel        = 10;                        // WiFi Channel number between 1 and 13
const bool  hide_SSID      = false;                     // To disable SSID broadcast -> SSID will not appear in a basic WiFi scan
const int   max_connection = 2;                         // Maximum simultaneous connected clients on the AP

void display_connected_devices()
{
    wifi_sta_list_t wifi_sta_list;
    tcpip_adapter_sta_list_t adapter_sta_list;
    esp_wifi_ap_get_sta_list(&wifi_sta_list);
    tcpip_adapter_get_sta_list(&wifi_sta_list, &adapter_sta_list);

    if (adapter_sta_list.num > 0)
        Serial.println("-----------");
    for (uint8_t i = 0; i < adapter_sta_list.num; i++)
    {
        tcpip_adapter_sta_info_t station = adapter_sta_list.sta[i];
        Serial.print((String)"[+] Device " + i + " | MAC : ");
        Serial.printf("%02X:%02X:%02X:%02X:%02X:%02X", station.mac[0], station.mac[1], station.mac[2], station.mac[3], station.mac[4], station.mac[5]);
        Serial.println((String) " | IP " + ip4addr_ntoa(&(station.ip)));
    }
}

void setup()
{
    Serial.begin(115200);
    Serial.println("\n[*] Creating AP");
    WiFi.mode(WIFI_AP);
    WiFi.softAP(ssid, password, channel, hide_SSID, max_connection);
    Serial.print("[+] AP Created with IP Gateway ");
    Serial.println(WiFi.softAPIP());
}

void loop()
{
    display_connected_devices();
    delay(5000);
}

Terminal outpout

[+] Device 0 |  MAC: 44: E2: 42: 08: D7: 11 |  IP 192.168.4.2
 [+] Device 1 |  MAC: 00: 0A: F7: 42: 42: 42 |  IP 192.168.4.3
 -----------
 [+] Device 0 |  MAC: 44: E2: 42: 08: D7: 11 |  IP 192.168.4.2
 [+] Device 1 |  MAC: 00: 0A: F7: 42: 42: 42 |  IP 192.168.4.3

Alternatively, some devices could also be prevented from connecting by filtering MAC addresses. For example, only allow the ESP32 to connect to it. Indeed, the esp_wifi_deauth_sta(int device_id) function can be used by specifying the device number you want to remove.

Hint

The solution of the first person who will send me an example of a code that allows filtering the MAC addresses on the WIFI router created by the ESP32 will be proposed here!

Use station and AP mode at the same time

It is possible to have the ESP32 that works with both station and AP modes simultaneously. The ESP32 is connected to a classic Wi-Fi (station mode) with also a Wi-Fi access point activated (AP mode). This mode is called WIFI_AP_STA .

Note

There is a separate network interface for each mode. Since the interfaces are separated, they each have a different IP address.

seo

Use both STATION and AP mode

One possible application is connecting the local Wi-Fi network created by the ESP32 in Soft AP mode.

seo

Example

The Arduino code mixes both the management of the station mode and the AP mode. The code is relatively simple when using 2 modes separately:
  • We use the Wi-Fi mode WIFI_AP_STA

  • We create our local Wi-Fi access point (Soft AP)

  • Then we connect to the classic Wi-Fi box.

Here is an example that allows you to have both modes at the same time:

#include <WiFi.h>

const char* wifi_network_ssid     = "Lounge";
const char* wifi_network_password =  "cupcakes";

const char *soft_ap_ssid          = "uPesy_AP";
const char *soft_ap_password      = NULL;

void setup()
{
    Serial.begin(115200);
    WiFi.mode(WIFI_AP_STA);

    Serial.println("\n[*] Creating ESP32 AP");
    WiFi.softAP(soft_ap_ssid, soft_ap_password);
    Serial.print("[+] AP Created with IP Gateway ");
    Serial.println(WiFi.softAPIP());

    WiFi.begin(wifi_network_ssid, wifi_network_password);
    Serial.println("\n[*] Connecting to WiFi Network");

    while(WiFi.status() != WL_CONNECTED)
    {
        Serial.print(".");
        delay(100);
    }

    Serial.print("\n[+] Connected to the WiFi network with local IP : ");
    Serial.println(WiFi.localIP());
}

void loop() {}

Terminal outpout

[*] Creating ESP32 AP
 [+] AP Created with IP Gateway 192.168.4.1

 [*] Connecting to WiFi Network
 ..................................................  ...........
 [+] Connected to the WiFi network with local IP: 192.168.85.37

Warning

Don’t get the credentials wrong:
  • wifi_network corresponds to the identifiers of the classic Wi-Fi box

  • soft_ap corresponds to the access point identifiers that we will create with the ESP32. You will need to enter this password on devices that want to connect to the ESP32.

See also:

We use cookies to make your visit to the site as pleasant as possible. Privacy Policy