Skip to content

Commit 808bf50

Browse files
authored
wifi: add SoftAPModeProbeRequestReceived event handler (#2917)
- add probe request event handler (#2910) - update WiFi events handling example to use new handler Pro tip: replace blinking LED with ‘analogWrite’ and connect the pin to a loudspeaker (or use a servo to hit a bell). Get notified when someone with a smartphone wanders around your country house.
1 parent cc84a64 commit 808bf50

File tree

4 files changed

+113
-52
lines changed

4 files changed

+113
-52
lines changed

libraries/ESP8266WiFi/examples/WiFiClientEvents/WiFiClientEvents.ino

-52
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
This sketch shows how to use WiFi event handlers.
3+
4+
In this example, ESP8266 works in AP mode.
5+
Three event handlers are demonstrated:
6+
- station connects to the ESP8266 AP
7+
- station disconnects from the ESP8266 AP
8+
- ESP8266 AP receives a probe request from a station
9+
10+
Written by Markus Sattler, 2015-12-29.
11+
Updated for new event handlers by Ivan Grokhotkov, 2017-02-02.
12+
This example is released into public domain,
13+
or, at your option, CC0 licensed.
14+
*/
15+
16+
#include <ESP8266WiFi.h>
17+
#include <stdio.h>
18+
19+
const char* ssid = "ap-ssid";
20+
const char* password = "ap-password";
21+
22+
WiFiEventHandler stationConnectedHandler;
23+
WiFiEventHandler stationDisconnectedHandler;
24+
WiFiEventHandler probeRequestPrintHandler;
25+
WiFiEventHandler probeRequestBlinkHandler;
26+
27+
bool blinkFlag;
28+
29+
void setup() {
30+
Serial.begin(115200);
31+
pinMode(LED_BUILTIN, OUTPUT);
32+
digitalWrite(LED_BUILTIN, HIGH);
33+
34+
// Don't save WiFi configuration in flash - optional
35+
WiFi.persistent(false);
36+
37+
// Set up an access point
38+
WiFi.mode(WIFI_AP);
39+
WiFi.softAP(ssid, password);
40+
41+
// Register event handlers.
42+
// Callback functions will be called as long as these handler objects exist.
43+
// Call "onStationConnected" each time a station connects
44+
stationConnectedHandler = WiFi.onSoftAPModeStationConnected(&onStationConnected);
45+
// Call "onStationDisconnected" each time a station disconnects
46+
stationDisconnectedHandler = WiFi.onSoftAPModeStationDisconnected(&onStationDisconnected);
47+
// Call "onProbeRequestPrint" and "onProbeRequestBlink" each time
48+
// a probe request is received.
49+
// Former will print MAC address of the station and RSSI to Serial,
50+
// latter will blink an LED.
51+
probeRequestPrintHandler = WiFi.onSoftAPModeProbeRequestReceived(&onProbeRequestPrint);
52+
probeRequestBlinkHandler = WiFi.onSoftAPModeProbeRequestReceived(&onProbeRequestBlink);
53+
}
54+
55+
void onStationConnected(const WiFiEventSoftAPModeStationConnected& evt) {
56+
Serial.print("Station connected: ");
57+
Serial.println(macToString(evt.mac));
58+
}
59+
60+
void onStationDisconnected(const WiFiEventSoftAPModeStationDisconnected& evt) {
61+
Serial.print("Station disconnected: ");
62+
Serial.println(macToString(evt.mac));
63+
}
64+
65+
void onProbeRequestPrint(const WiFiEventSoftAPModeProbeRequestReceived& evt) {
66+
Serial.print("Probe request from: ");
67+
Serial.print(macToString(evt.mac));
68+
Serial.print(" RSSI: ");
69+
Serial.println(evt.rssi);
70+
}
71+
72+
void onProbeRequestBlink(const WiFiEventSoftAPModeProbeRequestReceived&) {
73+
// We can't use "delay" or other blocking functions in the event handler.
74+
// Therefore we set a flag here and then check it inside "loop" function.
75+
blinkFlag = true;
76+
}
77+
78+
void loop() {
79+
if (millis() > 10000 && probeRequestPrintHandler) {
80+
// After 10 seconds, disable the probe request event handler which prints.
81+
// Other three event handlers remain active.
82+
Serial.println("Not printing probe requests any more (LED should still blink)");
83+
probeRequestPrintHandler = WiFiEventHandler();
84+
}
85+
if (blinkFlag) {
86+
blinkFlag = false;
87+
digitalWrite(LED_BUILTIN, LOW);
88+
delay(100);
89+
digitalWrite(LED_BUILTIN, HIGH);
90+
}
91+
delay(10);
92+
}
93+
94+
String macToString(const unsigned char* mac) {
95+
char buf[20];
96+
snprintf(buf, sizeof(buf), "%02x:%02x:%02x:%02x:%02x:%02x",
97+
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
98+
return String(buf);
99+
}

libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,19 @@ WiFiEventHandler ESP8266WiFiGenericClass::onSoftAPModeStationDisconnected(std::f
186186
return handler;
187187
}
188188

189+
WiFiEventHandler ESP8266WiFiGenericClass::onSoftAPModeProbeRequestReceived(std::function<void(const WiFiEventSoftAPModeProbeRequestReceived&)> f)
190+
{
191+
WiFiEventHandler handler = std::make_shared<WiFiEventHandlerOpaque>(WIFI_EVENT_SOFTAPMODE_PROBEREQRECVED, [f](System_Event_t* e){
192+
auto& src = e->event_info.ap_probereqrecved;
193+
WiFiEventSoftAPModeProbeRequestReceived dst;
194+
memcpy(dst.mac, src.mac, 6);
195+
dst.rssi = src.rssi;
196+
f(dst);
197+
});
198+
sCbEventList.push_back(handler);
199+
return handler;
200+
}
201+
189202
// WiFiEventHandler ESP8266WiFiGenericClass::onWiFiModeChange(std::function<void(const WiFiEventModeChange&)> f)
190203
// {
191204
// WiFiEventHandler handler = std::make_shared<WiFiEventHandlerOpaque>(WIFI_EVENT_MODE_CHANGE, [f](System_Event_t* e){

libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class ESP8266WiFiGenericClass {
6161
WiFiEventHandler onStationModeDHCPTimeout(std::function<void(void)>);
6262
WiFiEventHandler onSoftAPModeStationConnected(std::function<void(const WiFiEventSoftAPModeStationConnected&)>);
6363
WiFiEventHandler onSoftAPModeStationDisconnected(std::function<void(const WiFiEventSoftAPModeStationDisconnected&)>);
64+
WiFiEventHandler onSoftAPModeProbeRequestReceived(std::function<void(const WiFiEventSoftAPModeProbeRequestReceived&)>);
6465
// WiFiEventHandler onWiFiModeChange(std::function<void(const WiFiEventModeChange&)>);
6566

6667
int32_t channel(void);

0 commit comments

Comments
 (0)