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

ESP32: using lambdas as WiFi event handling functions

DFRobot Sep 25 2019 655

In this tutorial we will learn how to use C++ lambdas as WiFi event handling functions, on the ESP32. We will be using the Arduino core. The tests shown here were performed using an ESP32 board from DFRobot.


Introduction

In this tutorial we will learn how to use C++ lambdas as WiFi event handling functions, on the ESP32. We will be using the Arduino core.

We have already covered how to configure handling functions for WiFi events on the ESP32 in previous tutorials. You can check the “Related Posts” sections at the end of this post for a list of tutorials on this subject.

In all the tutorials, we have always defined a named function as handling function for the WiFi event we wanted to catch. Then, we passed it to the onEvent method of the WiFi extern variable, to register it as handling function of that particular event.

Nonetheless, we can specify these handling functions in an alternative way that leads to a more compact syntax. As already mentioned, we can use the C++ lambda syntax to define anonymous functions and pass them directly to the onEvent method.

Note however that there’s no rule to decide when to use named functions or the lambda syntax, as it will depend on the application scenario.

For example, if we are catching a lot of different events and all their handling functions are really simple (one or two lines), most likely using lambda functions will be cleaner since we can keep the definition of the function near the event it will handle and the code will be still remain readable.

On the other hand, if we have really complex handling functions, then probably it will be cleaner to define them as named functions, to separate their definition from the binding to the handling event.

These were just two possible scenarios but the objective of this post is just to present an alternative syntax. Naturally, depending on your use case, you should ponder which approach will be more adequate.

For testing purposes, we will catch the “Station got IP address” event and print the IP address assigned to the ESP32 on the corresponding handling function.

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


The code

The code for this tutorial will be very similar to what we have been covering, except for the fact that we will declare the event handling function using the C++ lambda syntax.

So, as before, we start by including the WiFi.h library. Followed by that we will declare the credentials of the WiFi network: SSID and password.

#include <WiFi.h>
   
const char* ssid = "yourNetworkName";
const char* password =  "yourNetworkPass";

We will write the rest of the code in the Arduino setup, since we will declare our handling function inline. As usual, we start by opening a serial connection, so we can output some results from our program.

Serial.begin(115200);

Then we will take care of registering our event handling function. As before, we will call the onEvent method on the WiFi extern variable.

The only difference is that now, instead of passing here the name of a function we will define elsewhere, we will directly pass a lambda function, which already contains the body definition. This will make our code much more compact.

The lambda function syntax we will be using is the following [1]:

[ captures ] ( params ) { body }

Our function won’t capture anything, so we start with empty square brackets. You can read more about captures here, to understand the use cases.

The parameters will be the same we have covered in previous tutorials:

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)

Our function will return void, but we can omit the returning type, which will by inferred by the compiler. So, our lambda takes the following format:

[](WiFiEvent_t event, WiFiEventInfo_t info){
// body implementation
}

In the body implementation we will simply print the local IP assigned to the ESP32. So, the full lambda definition can be seen below:

[](WiFiEvent_t event, WiFiEventInfo_t info){
       
   Serial.println(WiFi.localIP());
       
}

Now that we have checked how to define our lambda function, we can simply call the onEvent method on the WiFi extern variable, passing as first input our lambda and as second input the event ID.

WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info){
       
  Serial.println(WiFi.localIP());
       
}, SYSTEM_EVENT_STA_GOT_IP);

To finalize the setup function, we simply need to start the connection to the WiFi network.

WiFi.begin(ssid, password);

The final code can be seen below. Note that we can leave the main loop empty since we won’t do any computation ther.e

#include <WiFi.h>
   
const char* ssid = "yourNetworkName";
const char* password =  "yourNetworkPass";
   
void setup()
{
    Serial.begin(115200);
   
    WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info){
       
      Serial.println(WiFi.localIP());
       
    }, SYSTEM_EVENT_STA_GOT_IP);
       
    WiFi.begin(ssid, password);
   
}
   
void loop(){}


Testing the code

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

You should get an output similar to figure 1. As can be seen, we have obtained the IP address assigned to the ESP32, as expected.

Output of the program, showing the IP address assigned to the ESP32.

Figure 1 – Output of the program, showing the IP address assigned to the ESP32.


Related Posts

ESP32: Getting started with WiFi Events

ESP32: Getting WiFi event information

ESP32: Station Got IP address event


References

[1] https://en.cppreference.com/w/cpp/language/lambda

REVIEW