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

Advertisements

25 Replies to “ESP32 Arduino: Setting a soft AP”

    1. Hi!

      Are you trying to send the messages via HTTP?

      Note that the fact that the ESP32 is working as soft AP means that it is hosting the network.

      When you say clients, are you referring to something like HTTP clients or stations connected to the ESP32?

      Give me some more context about what is your objective, so I can try to help 🙂

      Best regards,
      Nuno Santos

      Like

    1. Hi!

      That’s very weird, I never ran into that problem. I was taking a quick look at the Arduino core repository and did not see any change that might be the cause of your problem, such as the classes changing.

      Is there the possibility that you have another WiFi.h library for regular Arduino Boards or something similar that is causing some conflict?

      Additionally, another good place to ask is on the Arduino core GitHub page. Maybe someone has already experienced the same issue and can help.

      Hope this helps getting you in the right track.

      Best regards,
      Nuno Santos

      Like

  1. Hi! i love your tutorials! I’m using this one in combination with the “ESP32 Arduino Bluetooth classic: Getting started” to test for a ibeacon or something similar. It’s working but at the moment of the upload to the board, Arduino IDE sais that the program gets the 99% of the device storage, Is this correct? I can’t program any logic with this. I have upload directly the code to the board, as it comes. Haven’t test with other firmwares or bootloader.

    Liked by 1 person

    1. Hi!

      Thank you very much for the feedback, I’m very happy to know you are finding the tutorials useful 🙂

      I’m not sure how accurate the values shown by the IDE are, but the bluetooth stack should be indeed quite big.

      I never tried to use both Bluetooth and WiFi simultaneously, but those two stacks should indeed occupy a lot of space.

      Nevertheless, maybe there is some compilation option that allows to only include the functionalities needed and leave more free space, since it makes sense to be possible to use both WiFi and Bluetooth in the same application.

      My suggestion is to open an issue on the GitHub page of the Arduino core, most likely someone there already faced the same issue and may have found a solution.

      Let us know if you find any solution, since it should indeed be very useful to be able to run both WiFi and Bluetooth on the same application 🙂

      Best regards,
      Nuno Santos

      Like

  2. Hi, thank you very much for this post. One question, does esp32 in ap mode always deliver the same ip, if not, how do I define a static ip in ap mode?

    Liked by 1 person

    1. Hi! You’re welcome, thank you for the feedback 🙂

      I’ve never tested how to change the IP, but there’s a method called softApConfig that seems to do what you are looking for:
      https://github.com/espressif/arduino-esp32/blob/c92b61739743e6df0768bbcba1e0b53755ec91d8/libraries/WiFi/src/WiFiAP.h#L41

      I haven’t had the chance to test it, but if I have some time I’ll take a look. If you test it meanwhile, let us know if it works 🙂

      Best regards,
      Nuno Santos

      Like

  3. Hello everyone, I have a problem. i have a esp32 sparkfun thing board and i have configurated a softAP with arduino. My problem is that i can connect a station on the AP with an iPhone, macbook but with my PC on windows 10 and a windows tab on windows 8 the connection doesn’t work.
    Do you have a solution please ?

    Liked by 1 person

    1. Hi!

      That’s weird, I’ve never experienced that problem and I’ve been connecting to the ESP32 working as soft AP from windows 8.

      I’ve also been able to do it with windows 10 in the past.

      Do experience the connection problem with windows 8 and windows 10 on the same PC?

      If so, can you test from one of those operating systems running from another computer, just to make sure it is not some issue from your computer network (maybe some problem with the network card?)

      When you mention the connection doesn’t work, does it appear on the available networks or not? If it appears, does it hang while trying to connect?

      Also, a good place to ask around is the Arduino core GitHub page, maybe someone there has already faced the same issue and have some solution.

      Best regards,
      Nuno Santos

      Like

      1. Thanks for your answer.
        The problem is the same with a lot of computer and tablets with windows 8 and 10 not just with a single one.
        Yes the network appear in the wifi settings but when windows 10 is trying to connect, the connexion fail

        Liked by 1 person

        1. You’re welcome 🙂 Well that is indeed very strange.

          Is your program just trying to connect to the soft ap or are you also exchanging some data with the ESP after the connection is established?

          I recall that some times when connecting to the ESP32 via soft AP my Operating system would indicate something like “Limited connection” or something similar, but it was still possible to exchange data with the ESP32.

          Is that your case or it is impossible to exchange data with the ESP32 after the connection attempt?

          You can try it by establishing a http server on the ESP and then trying to connect to it with a web browser running on the computer connected to the ESP.

          If you have that problem, then it is worth trying another ESP32 unit, just to discard any defect on the one you are using.

          Other than that, I’m not sure what may be causing the issue.

          Other thing you can do is trying to update your ESP32 Arduino core to the latest version, to make sure it wasn’t some temporary bug in the code that is affecting the connection on Windows 8 and 10.

          That has already happened to me in the past, using a version of the Arduino core that had a bug which was already fixed in the most updated version. In my case it was in connecting to a WiFi network, which would fail most of the times. After updating the version, it started working fine.

          Also, sharing your problem at the GitHub page of the Arduino core is a good option since someone else may already faced the problem.

          Hope this helps 🙂

          Best regards,
          Nuno Santos

          Like

    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

      Like

  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

    Liked by 1 person

    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

      Like

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s