0

$USD

$USD
TUTORIALS ESP32

ESP32 IDF Tutorial: Setting a soft AP

DFRobot Apr 24 2018 1611

The objective of this ESP32 tutorial is to explain how to create a soft AP using the ESP32 and IDF. The tests of this ESP32 tutorial were performed using a DFRobot’s ESP32 module device integrated in a ESP32 development board.

Introduction

The objective of this ESP32 tutorial is to explain how to create a soft AP using the ESP32 and IDF. For this simple example, we will just set the soft AP and confirm that it will appear in the list of available WiFi connections.

This tutorial is based on the this excellent example from lucadentella, which as more functionalities and that I encourage you to try.

The tests of this ESP32 tutorial were performed using a DFRobot’s ESP32 module device integrated in a ESP32 development board.

The code

The first thing we are going to do is including the esp_wifi.h library. Here, we will have access to the functionality needed to start our soft AP.

#include "esp_wifi.h"

Since we are going to set a soft AP, we will need to define a SSID, so other devices can find it. So, we will define a string to be used latter. I will call it “ESP32AP” but you can name it as you like.

#define SSID "ESP32AP"

Moving to the main code, the first thing we will do is initializing the WiFi with a call to the esp_wifi_init function. Note that this call needs to be performed before any other call to the WiFi API [1].

As input, this function receives the address of a parameter of type wifi_init_config_t, which will contain the WiFi stack configuration. Luckily, for this simple example, we will not need to worry about these low level configurations and we can call the WIFI_INIT_CONFIG_DEFAULT macro, which will return a set of default configurations for WiFi.

wifi_init_config_t wifiInitializationConfig = WIFI_INIT_CONFIG_DEFAULT();
 
esp_wifi_init(&wifiInitializationConfig);

Next, we will need to set the WiFi storage configuration, with a call to the esp_wifi_set_storage function. This function receives as input a variable of type wifi_storage_t. By default, the value is WIFI_STORAGE_FLASH [2], meaning the configurations will be stored in both memory and flash [3].

In our case, we will pass as input the value WIFI_STORAGE_RAM, which specifies that all the configurations will be stored in RAM [3].

esp_wifi_set_storage(WIFI_STORAGE_RAM);

Next, we will set the WiFi operating mode with a call to the esp_wifi_set_mode function. This function allows to set the WiFi to operate in station mode, soft AP or both [4]. By default, the mode will be soft AP [4].

As input, this function receives a variable of type wifi_mode_t. Although we have the previously mentioned default value, we will explicitly set the mode to soft AP by passing as input the value WIFI_MODE_AP.

esp_wifi_set_mode(WIFI_MODE_AP);

Besides setting the mode, we will need to specify the remaining soft AP configurations. To do so, we will need to create a variable of type wifi_config_t. Since we are setting the ESP32 to work as station, we will need to configure the wifi_ap_config_t structure that is part of the wifi_config_t union.

The first value we will set is the SSID, using value we defined in the beginning of the code. The structure member for this configuration is called ssid.

In this simple example we will leave our AP open, so no password will be needed. To do so, we set the authmode member to WIFI_AUTH_OPEN. Note that this is a variable of type wifi_auth_t, which supports many other values.

The other members we will set are the channel, which we will set to 0, the ssid_hidden, which we will set to 0 so the SSID is broadcasted, the beacon_interval, which we will set to 100 milliseconds, and the max_connection, which we will set to 1.

Note that the max_connection struct member is related to the maximum number of stations that can connect to the AP and its max value allowed is 4.

We will pass this configuration in a call to the esp_wifi_set_config function. This function also receives as parameter a variable of type wifi_interface_t. Since we want our ESP32 to operate as soft AP, we pass the value WIFI_IF_AP for this argument.

wifi_config_t ap_config = {
        .ap = {
            .ssid = SSID,
        .channel = 0,
        .authmode = WIFI_AUTH_OPEN,
        .ssid_hidden = 0,
        .max_connection = 1,
        .beacon_interval = 100
        }
};
 
esp_wifi_set_config(WIFI_IF_AP, &ap_config);

Finally, we call the esp_wifi_start function to start the ESP32 WiFi accordingly to the previously defined configuration. This function takes no arguments.

esp_wifi_start();

The final code can be seen bellow.

#include "esp_wifi.h"
 
#define SSID "ESP32AP"
 
void app_main()
{   
 
    wifi_init_config_t wifiInitializationConfig = WIFI_INIT_CONFIG_DEFAULT();
 
    esp_wifi_init(&wifiInitializationConfig);
 
    esp_wifi_set_storage(WIFI_STORAGE_RAM);
 
    esp_wifi_set_mode(WIFI_MODE_AP);
 
    wifi_config_t ap_config = {
          .ap = {
            .ssid = SSID,
            .channel = 0,
            .authmode = WIFI_AUTH_OPEN,
            .ssid_hidden = 0,
            .max_connection = 1,
            .beacon_interval = 100
          }
        };
 
    esp_wifi_set_config(WIFI_IF_AP, &ap_config);
 
    esp_wifi_start();
 
}

Testing the code

To test the code, simply flash it to the ESP32 by using the IDF command line tools. Upon uploading the code, open your computer available networks list. A new network with the name we defined on the code should appear as available, as shown in figure 1.

Figure 1 – ESP32 soft AP available in the list of WiFi connections, in Windows.

DFRobot supply lots of esp32 arduino tutorials and esp32 projects for makers to learn.