|
| 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 | +} |
0 commit comments