$USD
  • EUR€
  • £GBP
  • $USD
TUTORIALS ESP32

ESP32 Soft AP: Station connection event

DFRobot Dec 16 2019 845

In this tutorial we will setup the ESP32 to work as a soft Access Point and learn how to register an handling function to process the event triggered when a station connects to the network. The tests shown here were performed using an ESP32 board from DFRobot.


Introduction

In this tutorial we will setup the ESP32 to work as a soft Access Point and learn how to register an handling function to process the event triggered when a station connects to the network.

For a detailed tutorial on how to setup a soft AP on the ESP32, please check here. For simplicity, we will setup a network without password.

The tests shown here were performed using an ESP32 board from DFRobot.


The code

We will start by including the WiFi.h library. Once we do it, we will have access to the WiFi extern variable, which allows us to both setup a soft AP and configuring WiFi events handling functions.

#include <WiFi.h>

Moving on to the Arduino setup, we will open a serial connection to output the results of our program.

Serial.begin(115200);

Then we will setup the ESP32 to work as soft AP. For our testing scenario, we don’t need to have a password on the network the ESP32 is going to host, so we will just assign to it a network name (SSID).

So, we simply need to call the softAP method on the WiFi extern variable, passing as input the SSID we want to assign to our network. We will call it “MyESP32AP“.

WiFi.softAP("MyESP32AP");
After that we will register an handling function for the SYSTEM_EVENT_AP_STACONNECTED event, which will be fired whenever a new station connects to the network. We will call our handling function WiFiStationConnected and define it later.

Recall from previous tutorials that we can register a WiFi event handling function by calling the onEvent method on the WiFi extern variable, passing as first input the handling function and as second input the identifier of the event.

WiFi.onEvent(WiFiStationConnected, SYSTEM_EVENT_AP_STACONNECTED);

To finalize, we will define our event handling function. For this introductory tutorial, we will simply print a message indicating a station has connected.

void WiFiStationConnected(WiFiEvent_t event, WiFiEventInfo_t info){
  Serial.println("Station connected");
}
The final code can be seen below.

#include <WiFi.h>
 
void WiFiStationConnected(WiFiEvent_t event, WiFiEventInfo_t info){
  Serial.println("Station connected");
}
  
void setup() {
  
  Serial.begin(115200);
   
  WiFi.softAP("MyESP32AP");
  
  WiFi.onEvent(WiFiStationConnected, SYSTEM_EVENT_AP_STACONNECTED);
 
}
  
void loop() {}


Testing the code

To test the code, first compile and upload it to your ESP32, using the Arduino IDE. When the procedure finishes, wait for the soft AP to be setup. It should be ready as soon as a WiFi network called “MyESP32AP” is found by nearby WiFi devices (ex: a laptop or a smartphone).

After that, open the Arduino IDE serial monitor and then use some WiFi enabled device to connect to the “MyESP32AP” network. After the connection is established, you should get a result similar to figure 1 on the Arduino IDE serial monitor.

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.

Figure 1 – Output of the program, showing the message getting printing when a station connects to the ESP32 network.


Tutorials Writter: Techtutorialsx

REVIEW