ESP32 Soft AP: Getting number of stations connected

In this tutorial we will check how to obtain the number of stations connected to a network hosted by the ESP32, operating as Soft AP. We will be using the Arduino core. The tests from this tutorial were performed using an ESP32 board from DFRobot.

Introduction

In this tutorial we will check how to obtain the number of stations connected to a network hosted by the ESP32, operating as Soft AP. We will be using the Arduino core.

Note that there are multiple ways of achieving this. For example, in the previous tutorial, we have worked with some lower level data structures to obtain the IP addresses of all the stations connected to the network.

As seen, the tcpip_adapter_sta_list_t struct contains the total number of devices connected, which we could use.

Alternatively, we can also keep track of the station connection and disconnection events, as seen here, and store in a variable how many stations are connected at a given time.

Nonetheless, there’s a much simpler way of obtaining this value, using the higher level classes and method from the Arduino core, as we will see in the code below.

The tests from this tutorial were performed using an ESP32 board from DFRobot.

The code

We will start by including the WiFi.h library, so we have access to the WiFi extern variable. This variable exposes the methods we need to both setup the ESP32 to work as soft AP and obtaining the number of stations connected at a given moment.

#include <WiFi.h>

Moving on to the Arduino setup, we will start by opening a serial connection. This will allow us to print the results of our program.

Serial.begin(115200);

Then we will setup the WiFi network by calling the softAP method on the WiFi extern variable. As input, we will pass a string with the name we want to assign to the network.

WiFi.softAP("MyESP32AP");

The complete setup function can be seen below.

void setup() {

  Serial.begin(115200);

  WiFi.softAP("MyESP32AP");

}

We will write the rest of our code in the Arduino loop function, so we periodically print the number of stations connected. We do this with a call to the softAPgetStationNum method on the WiFi extern variable.

This method takes no arguments and returns as output the number of stations connected to the network. We will print the result directly to the serial port.

Serial.println(WiFi.softAPgetStationNum());

We will introduce a small delay between each iteration of the Arduino loop function. The full loop can be seen below.

void loop() {

  Serial.print("Stations connected: ");
  Serial.println(WiFi.softAPgetStationNum());
  delay(5000);
}

The final code can be seen below.

#include <WiFi.h>

void setup() {

  Serial.begin(115200);

  WiFi.softAP("MyESP32AP");

}

void loop() {

  Serial.print("Stations connected: ");
  Serial.println(WiFi.softAPgetStationNum());
  delay(5000);
}

Testing the code

To test the previous code, simply compile it and upload it to your device using the Arduino IDE. After the procedure finishes, open the Arduino IDE serial monitor.

Then, wait a bit until the network is established. It should be visible from any WiFi enabled device. You should start seeing the number 0 being printed, since no station will be connected at the beginning.

To finalize, connect one or more devices to the network. As shown in figure 1, you should start seeing the total number of stations connect getting printed.

Note: At the time of writing, there’s an issue on the Arduino core that makes the additional debug messages seen in figure 1 getting printed when a station connects. You can track the progress of the issue here.

Output of the program, showing the number of stations connected.
Figure 1 – Output of the program, showing the number of stations connected.

Leave a Reply