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

ESP32 Arduino: Getting WiFi Event information

DFRobot Sep 24 2019 934

In this tutorial we will learn how to get the information that is passed to a WiFi event handling function. The tests shown here were performed using an ESP32 board from DFRobot.


Introduction

In this tutorial we will learn how to get the information that is passed to a WiFi event handling function.

We will be testing this on the event that indicates the ESP32 connected to the Access Point (SYSTEM_EVENT_STA_CONNECTED), covered in the previous tutorial. Nonetheless, different events have different information available.

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


The code

We will start the code by including the WiFi.h library. By doing this, we will have access to the WiFi extern variable, which we can use to connect the ESP32 to an Access Point and to register WiFi event handling functions.

#include <WiFi.h>

We will also need to declare the credentials of the WiFi network to which we want to connect the ESP32. We need both the network name (SSID) and password.

const char* ssid = "yourNetworkName";
const char* password =  "yourNetworkPass";
Moving on to the Arduino setup function, we will start by opening a serial connection, to be able to output the results of our program.

Serial.begin(115200);
Then we will register our WiFi event handling function. We do this by calling the onEvent method on the WiFi extern variable.

As first input we pass our handling function (we will call it WiFiStationConnected and define it later) and as second input the ID of the event we want to catch. You can check here the enum that defines the available event identifiers.

In our case we want the SYSTEM_EVENT_STA_CONNECTED event.

WiFi.onEvent(WiFiStationConnected, SYSTEM_EVENT_STA_CONNECTED);

To finish the setup function, we will call the begin method on the WiFi extern variable, to connect the ESP32 to the network. This method receives as input the SSID and password of the network. We will use the values we defined earlier.

WiFi.begin(ssid, password);

As seen in the previous tutorial, this function call will return before the ESP32 connects to the Access Point and the connection procedure might take a while. When the connection is established, then our event handling function will be executed.

The full setup function can be seen below.

void setup()
{
    Serial.begin(115200);
  
    WiFi.onEvent(WiFiStationConnected, SYSTEM_EVENT_STA_CONNECTED);
      
    WiFi.begin(ssid, password);
  
}

We will leave the Arduino main loop function empty, since we don’t have anything to do there. The rest of the code will be written in the WiFi event handling function.

void loop(){}

To finalize the code, we will analyze the implementation of the event handling function. Recall from the previous tutorial about WiFi events that this function needs to follow a predefined signature.

So, our handling function needs to return void and receive two arguments:

A system_event_id_t enumerated value (aliased as WiFiEvent_t, as can be seen here)
A system_event_info_t union (aliased as WiFiEventInfo_t, as can be seen here)

void WiFiStationConnected(WiFiEvent_t event, WiFiEventInfo_t info)
{
  // Handling function code
}

The second argument of the event handling function is the one that contains the information about the event. The definition of this union can be seen here.

In our case, since we are listening to the station connected to AP event, we are going to access the connected union member. We access a member of the union using the dot operator.

This member corresponds to a struct of the type system_event_sta_connected_t, which can be seen here.

This struct contains the following members:

ssid: Network name (should match the SSID we have used when connecting to the network). This is defined as an array of bytes.

ssid_len: Length of the SSID.

bssid: BSSID of the Access Point. It corresponds to the MAC address of the access point and it is a 6 bytes array.

channel: Channel used.

authmode: Authentication mode used. It is defined as an enum value from here.

The first thing we will do is printing the length of the SSID.

Serial.print("SSID Length: ");
Serial.println(info.connected.ssid_len);
Then we will iterate through the array that contains the SSID information, using as stopping condition the SSID length.

Serial.print("SSID: ");
for(int i=0; i<info.connected.ssid_len; i++){
   Serial.print((char) info.connected.ssid[i]);
}
After this we will print the BSSID. As mentioned before, it is defined as an array with a length of 6 bytes. We will print it in its most command format, which corresponds to each byte represented in hexadecimal format, separated by colons.

Serial.print("\nBSSID: ");
for(int i=0; i<6; i++){
   Serial.printf("%02X", info.connected.bssid[i]);
 
   if(i<5){
      Serial.print(":");
   }
}
To finalize we will also print the channel and the authentication mode. The complete handling function can be seen below.

void WiFiStationConnected(WiFiEvent_t event, WiFiEventInfo_t info)
{
    Serial.println("Connected to AP!");
 
    Serial.print("SSID Length: ");
    Serial.println(info.connected.ssid_len);
 
    Serial.print("SSID: ");
    for(int i=0; i<info.connected.ssid_len; i++){
      Serial.print((char) info.connected.ssid[i]);
    }
 
    Serial.print("\nBSSID: ");
    for(int i=0; i<6; i++){
      Serial.printf("%02X", info.connected.bssid[i]);
 
      if(i<5){
        Serial.print(":");
      }
    }
     
    Serial.print("\nChannel: ");
    Serial.println(info.connected.channel);
 
    Serial.print("Auth mode: ");
    Serial.println(info.connected.authmode); 

The final code can be seen below.

#include <WiFi.h>
  
const char* ssid = "yourNetworkName";
const char* password =  "yourNetworkPass";
  
void WiFiStationConnected(WiFiEvent_t event, WiFiEventInfo_t info)
{
    Serial.println("Connected to AP!");
 
    Serial.print("SSID Length: ");
    Serial.println(info.connected.ssid_len);
 
    Serial.print("SSID: ");
    for(int i=0; i<info.connected.ssid_len; i++){
      Serial.print((char) info.connected.ssid[i]);
    }
 
    Serial.print("\nBSSID: ");
    for(int i=0; i<6; i++){
      Serial.printf("%02X", info.connected.bssid[i]);
 
      if(i<5){
        Serial.print(":");
      }
    }
     
    Serial.print("\nChannel: ");
    Serial.println(info.connected.channel);
 
    Serial.print("Auth mode: ");
    Serial.println(info.connected.authmode);  
} 
  
void setup()
{
    Serial.begin(115200);
  
    WiFi.onEvent(WiFiStationConnected, SYSTEM_EVENT_STA_CONNECTED);
      
    WiFi.begin(ssid, password);
  
}
  
void loop(){}


Testing the code

To test the code, simply compile it and upload it to your ESP32, using the Arduino IDE. After the procedure finishes, open the serial monitor tool and wait a bit for the connection to the network to be established.

When the connection finishes, you should get an output similar to figure 1. As can be seen, all the information available from he event was printed, as expected.


Figure 1 – Output of the program, showing the event information.

REVIEW