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).

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

  1. In AP_STA mode of a ESP32, could its station part as one of the client members of its own AP network? How to do this in Arduino code?

    1. Hi!

      To be honest I’m not sure, I never tested that particular scenario, since it doesn’t seem to be a common one.

      Nonetheless, I’m curious, why do you need it? Some kind of self testing procedure?

      Best regards,
      Nuno Santos

  2. In AP_STA mode of a ESP32, could its station part as one of the client members of its own AP network? How to do this in Arduino code?

    1. Hi!
      To be honest I’m not sure, I never tested that particular scenario, since it doesn’t seem to be a common one.
      Nonetheless, I’m curious, why do you need it? Some kind of self testing procedure?
      Best regards,
      Nuno Santos

  3. Hi Nuno,
    Thank you for your reply.
    I have a WiFi network (local, without internet), several devices including a ESP32 all connected to an AP for exchanging data through UDP ( no one is server). If the ESP32 is in AP_STA mode, and its STA could connect to its own AP, then I do not need that external AP, I can connect all of the devices include this ESP32’s STA to this ESP32’s AP, I save an AP device.

    Best

    James

    1. Hi!

      You’re welcome 🙂

      Interesting architecture! Since this a not so usual scenario, I really don’t know if this is possible, but it may be.

      I doubt that this will be covered in any type of documentation, so my suggestion is really that you give it a try and see if it works.

      Let us know if it does, I’m curious to know if the ESP32 supports that kind of scenario 🙂

      Best regards,
      Nuno Santos

  4. Hi Nuno,
    Thank you for your reply.
    I have a WiFi network (local, without internet), several devices including a ESP32 all connected to an AP for exchanging data through UDP ( no one is server). If the ESP32 is in AP_STA mode, and its STA could connect to its own AP, then I do not need that external AP, I can connect all of the devices include this ESP32’s STA to this ESP32’s AP, I save an AP device.
    Best
    James

    1. Hi!
      You’re welcome 🙂
      Interesting architecture! Since this a not so usual scenario, I really don’t know if this is possible, but it may be.
      I doubt that this will be covered in any type of documentation, so my suggestion is really that you give it a try and see if it works.
      Let us know if it does, I’m curious to know if the ESP32 supports that kind of scenario 🙂
      Best regards,
      Nuno Santos

    1. Hi!

      Thanks for the feedback, I’m glad you liked the tutorials 🙂

      I’m also a big fan of Rui’s work and recommend everyone to take a look!

      Best regards,
      Nuno Santos

    1. Hi!
      Thanks for the feedback, I’m glad you liked the tutorials 🙂
      I’m also a big fan of Rui’s work and recommend everyone to take a look!
      Best regards,
      Nuno Santos

  5. Pingback: ESP32 Arduino web server: Serving jQuery – techtutorialsx

  6. Pingback: ESP32 Arduino web server: Serving jQuery – techtutorialsx

  7. Hi. I am using ESP32 as an accesspoint and I am coonnecting two ESP8266 to it acting as wifi stations or clients. Now I want to send some data from ESP32 to one of those two ESP8266 clients based on what type of data is. LIke some ‘x’ data to first client and some ‘y’ data to second client. So can you please help me to achieve this task???
    THank you!

    1. Hi!

      There are many ways of achieving that.

      First you need to define who is going to act as a server and who is going to act as client.

      Having the ESP32 as a soft AP doesn’t necessarily mean it will be the server, since it acting as soft AP just means it is hosting the network,

      Next you need to define how you will exchange the data. You can use a lower level approach, such as establishing socket connections and work at that level, or you can use something more higher level such as HTTP.

      I think those are the main two problems you need to tackle first 🙂

      Hope this helps getting you in the right track 🙂

      Best regards,
      Nuno Santos

  8. Hi. I am using ESP32 as an accesspoint and I am coonnecting two ESP8266 to it acting as wifi stations or clients. Now I want to send some data from ESP32 to one of those two ESP8266 clients based on what type of data is. LIke some ‘x’ data to first client and some ‘y’ data to second client. So can you please help me to achieve this task???
    THank you!

    1. Hi!
      There are many ways of achieving that.
      First you need to define who is going to act as a server and who is going to act as client.
      Having the ESP32 as a soft AP doesn’t necessarily mean it will be the server, since it acting as soft AP just means it is hosting the network,
      Next you need to define how you will exchange the data. You can use a lower level approach, such as establishing socket connections and work at that level, or you can use something more higher level such as HTTP.
      I think those are the main two problems you need to tackle first 🙂
      Hope this helps getting you in the right track 🙂
      Best regards,
      Nuno Santos

  9. 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

  10. 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

  11. 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 .

  12. 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

%d bloggers like this: