ESP32 IDF: Setting a soft AP

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 ESP-WROOM-32 device integrated in a ESP32 FireBeetle 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 ESP-WROOM-32 device integrated in a ESP32 FireBeetle 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.

ESP32 IDF set soft AP

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


References

[1] http://esp-idf.readthedocs.io/en/latest/api-reference/wifi/esp_wifi.html?highlight=esp_wifi_init#_CPPv213esp_wifi_initP18wifi_init_config_t

[2] http://esp-idf.readthedocs.io/en/latest/api-reference/wifi/esp_wifi.html?highlight=esp_wifi_set_storage#_CPPv220esp_wifi_set_storage14wifi_storage_t

[3] https://github.com/espressif/esp-idf/blob/3c9ea3cb8efec28d362b09fff4ccba5e456dad4a/components/esp32/include/esp_wifi_types.h#L185

[4] http://esp-idf.readthedocs.io/en/latest/api-reference/wifi/esp_wifi.html?highlight=esp_wifi_set_mode#_CPPv217esp_wifi_set_mode11wifi_mode_t

Advertisements
This entry was posted in ESP32 and tagged , , , , , , . Bookmark the permalink.

4 Responses to ESP32 IDF: Setting a soft AP

  1. Colin Marsh says:

    Hi. I have been hunting the internet for something like this and found it today. Thanks heaps for making the code available for people to look at. I am a noob to ESP32 and programming in general. I followed the step-by-step and I think that I have it conceptually. However, when I compile, I get a heap of “… was not declared in this scope”. Is there something that I am missing? I looked at the references but have not recognized anything that would help me to compile. Am I missing something in your text or is it something that I need to know about programming? Thanks once again.

    Liked by 1 person

    • antepher says:

      Hi! Thank you very much for your feedback, I’m glad you found this tutorial useful 🙂

      That’s weird. Did you copy the whole code that is at the bottom of the post or did you modify something?

      If you modified something, please try to copy and paste the code as it is and check if it works. If it does, it is better to introduce your changes gradually and re-test.

      Or maybe it is something related with your project location or compiler configurations. Have you been able to run other IDF code or is this your first program?

      Best regards,
      Nuno Santos

      Like

  2. Colin Marsh says:

    BTW, I am using WiFi.h, but have changed references in the sketch.

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s