ESP32 Arduino: Setting a soft AP

The objective of this post is to explain how to set a soft AP using the ESP32 and the Arduino core. 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 post is to explain how to set a soft AP using the ESP32 and the Arduino core. This way, other devices can connect to the ESP32 and exchange data with it without the need to connect to a conventional router.

This may useful, for example, to configure the ESP32 before actually connecting to a Wireless network, in a commercial product. With this feature, we can serve a simple HTML page that the user accesses to input the credentials from his home network, in order for the ESP32 to later connect to it.

In this introductory tutorial we are simply going to explore the basics of setting the soft AP, since many other functionality can then be built on top of it.

The tests of this ESP32 tutorial were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board.

If you prefer a video tutorial, please check below my YouTube channel:


The code

First of all, we are going to include the WiFi.h library, which exposes an extern variable called WiFi that we are going to use to set our soft AP. This is the same variable we have been using in other tutorials to connect to WiFi networks.

#include "WiFi.h"

Since we are going to set a soft AP, we need to specify the network name (SSID) that will be shown to stations that can connect to it. We also need to specify a password, in order to avoid undesirable stations to connect to our AP. We are going to specify both of these variables in global constants.

const char *ssid = "MyESP32AP";
const char *password = "testpassword";

After this, we will do the remaining code in the Arduino setup function. We begin by starting a serial connection, so we can output some results of our program. This way, we can check the results in the Arduino IDE serial monitor.

Serial.begin(115200);

After this, to set our soft AP, we simply need to call the softAP method of the WiFi extern variable, passing as input both the SSID and the password defined before.

Note that this function has some more optional parameters that have default values assigned. These are the channel, an integer specifying if the SSID should be hidden and the maximum number of connections. Besides that, the password is also an optional parameter that defaults to null, which would allow to connect to the AP without a password. Nonetheless, for our example, we specified it.

 WiFi.softAP(ssid, password); 

To finalize, we are going to obtain and print the IP of the soft AP by calling the softAPIP method of the WiFi extern variable. This method takes no arguments and will return the IP address.

 Serial.println(WiFi.softAPIP()); 

The final source code for this ESP32 tutorial can be seen bellow. It includes all the previously analyzed code and some additional prints to make the output more easily readable.

#include "WiFi.h"

const char *ssid = "MyESP32AP";
const char *password = "testpassword";

void setup() {

  Serial.begin(115200);
  WiFi.softAP(ssid, password);

  Serial.println();
  Serial.print("IP address: ");
  Serial.println(WiFi.softAPIP());

}

void loop() {}


Testing the code

To test the code, simply compile it and upload it to the ESP32 board using the Arduino IDE. After the procedure is finished, open the serial monitor of the Arduino IDE. You should get an output similar to figure 1, which shows the IP of the soft AP.

ESP32 Arduino Soft Ap IP

Figure 1 – Output of the program.

Then, if you check the available networks on your computer, a new one should be listed, with the name we defined in the code, as shown in figure 2.

ESP32 Soft AP showing on Windows

Figure 2 – ESP32 Soft AP appearing as available network (Windows 8, Portuguese interface).

69 thoughts on “ESP32 Arduino: Setting a soft AP”

  1. Hello !

    I tried doing this on a ESP32 DevKit V1 and after some tries I finally got a readable output, but not all the times. Sometimes it’s readable, sometimes unreadable characters.

    Also, the Wi-Fi is “on” only if I don’t click on Serial Monitor. After I click on Serial Monitor, the network dissapears.

    Also, it doesn’t matter what SSID I type in, I always get ESP_#### (#### are the same numbers each time).

    I asked here because I found nothing online. Thank you !

    1. Hi!

      That’s a weird issue.

      Do you also get similar problems if you try to print something else to the serial port, just a string or something?

      The problem with the unreadable characters seems to indicate some problem with your hardware.

      It might be the source of the problems you describe.

      I would say that to be sure, the best approach would be to try in a different board.

      Best regards,
      Nuno Santos

    2. I´ve also experienced ssid name change fail with no reasonable cause.
      But everything worked fine when I no more changed password to ‘123456’. Yep! I’ve tried a lot to change ssid and I didn’t succeed. By chance, only by chance, I’ve set another password (‘testpassword’) and that all worked!

      Why a ‘123456’ results bad? I don’t know. Sure, it’s not due to a fully numerical password, since, as an example, a ‘234567’ is ok.

      Hope it may be useful for you.

  2. Hello !
    I tried doing this on a ESP32 DevKit V1 and after some tries I finally got a readable output, but not all the times. Sometimes it’s readable, sometimes unreadable characters.
    Also, the Wi-Fi is “on” only if I don’t click on Serial Monitor. After I click on Serial Monitor, the network dissapears.
    Also, it doesn’t matter what SSID I type in, I always get ESP_#### (#### are the same numbers each time).
    I asked here because I found nothing online. Thank you !

    1. Hi!
      That’s a weird issue.
      Do you also get similar problems if you try to print something else to the serial port, just a string or something?
      The problem with the unreadable characters seems to indicate some problem with your hardware.
      It might be the source of the problems you describe.
      I would say that to be sure, the best approach would be to try in a different board.
      Best regards,
      Nuno Santos

      1. This is an old post, but here’s a reply anyway. Regarding inability to set the SSID, you ***MUST*** ensure that the password is 8 characters or longer. If the assigned password is shorter than 8 characters, the SSID will be ignored. (You can also set a NULL password, if you want an open network)

  3. Is it possible to set channel bandwidth to 20MHz in AP, STA mode? How to do it in Arduino? Android wifi scanner showed me 40MHz .

  4. Hi,

    I am using the soft ap program in that i would like to connect it to internet is it possible ? I would like to act my ESp32 as same as router.

    Thank you

Leave a Reply

Discover more from techtutorialsx

Subscribe now to keep reading and get access to the full archive.

Continue reading