Skip to content

Commit

Permalink
mention lifetime in example and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mcspr committed Feb 11, 2024
1 parent d20339b commit 22def68
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
22 changes: 17 additions & 5 deletions doc/esp8266wifi/generic-examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,29 @@ Register the Events

To get events to work we need to complete just two steps:

1. Declare the event handler:
1. Declare the event handler in global scope.

.. code:: cpp
WiFiEventHandler disconnectedEventHandler;
2. Select particular event (in this case ``onStationModeDisconnected``)
and add the code to be executed when event is fired.
Alternatively, it can be declared as ``static`` in both function and global scopes.

``cpp disconnectedEventHandler = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected& event) { Serial.println("Station disconnected"); });`` If this event is fired the code will print out information that station has been disconnected.

That's it. It is all we need to do.
2. Select particular event (in this case ``onStationModeDisconnected``).
When this event is fired the code will print out information that station has been disconnected:

.. code:: cpp
disconnectedEventHandler = WiFi.onStationModeDisconnected(
[](auto&& event) {
Serial.println("Station disconnected");
});
3. Disable ``disconnectedEventHandler``, so the event is no longer handled by our callback:

.. code:: cpp
disconnectedEventHandler = nullptr;
Take note that lifetime of the callback handler is up to the app. e.g. if ``onStationModeDisconnected`` is declared in the function scope, it would be discarded immediately after the function exists.

The Code
~~~~~~~~
Expand Down
7 changes: 5 additions & 2 deletions libraries/ESP8266WiFi/examples/WiFiEvents/WiFiEvents.ino
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
const char* ssid = APSSID;
const char* password = APPSK;

// WiFi.on* methods **must** only be called **after** entering setup().
// Assigning immediately in global scope is not adviced, neither is assigning them within any other object constructors.
// These variables **may** exist in function block, but **only** if they are declared as `static`
WiFiEventHandler stationConnectedHandler;
WiFiEventHandler stationDisconnectedHandler;
WiFiEventHandler probeRequestPrintHandler;
Expand All @@ -43,12 +46,12 @@ void setup() {
WiFi.mode(WIFI_AP);
WiFi.softAP(ssid, password);

// Register event handlers.
// Callback functions will be called as long as these handler objects exist.
// Call "onStationConnected" each time a station connects
stationConnectedHandler = WiFi.onSoftAPModeStationConnected(&onStationConnected);

// Call "onStationDisconnected" each time a station disconnects
stationDisconnectedHandler = WiFi.onSoftAPModeStationDisconnected(&onStationDisconnected);

// Call "onProbeRequestPrint" and "onProbeRequestBlink" each time
// a probe request is received.
// Former will print MAC address of the station and RSSI to Serial,
Expand Down

0 comments on commit 22def68

Please sign in to comment.