ESP8266WiFi callbacks not working

I'm trying to get callbacks working with the ESP8266WiFi library but cant for the life of me figure out why it's not working. When I upload the sketch the builtin led turns on and then never turns off. When I connect to the AP nothing gets printed to the serial monitor and the light remains on.

Any help is greatly appreciated!

#include <ESP8266WiFi.h>

const char* ssid = "NodeMCU-v1.0.0";
const char* password = "123456789";

IPAddress local_IP(192,168,4,22);
IPAddress gateway(192,168,4,9);
IPAddress subnet(255,255,255,0);

void setup() {
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
  WiFi.mode(WIFI_AP);
  WiFi.softAPConfig(local_IP, gateway, subnet);
  WiFi.softAP(ssid, password);
  WiFi.onSoftAPModeStationConnected([](const WiFiEventSoftAPModeStationConnected& event) {
    Serial.printf("Station connected: %02x:%02x:%02x:%02x:%02x:%02x\n", event.mac[0], event.mac[1], event.mac[2], event.mac[3], event.mac[4], event.mac[5]);
    digitalWrite(LED_BUILTIN, HIGH);
  });
  WiFi.onSoftAPModeStationDisconnected([](const WiFiEventSoftAPModeStationDisconnected& event) {
    Serial.printf("Station disconnected: %02x:%02x:%02x:%02x:%02x:%02x\n", event.mac[0], event.mac[1], event.mac[2], event.mac[3], event.mac[4], event.mac[5]);
    digitalWrite(LED_BUILTIN, LOW);
  });
}

void loop(){
  delay(1000);
}

Never used the 8266, your code looks allot different from the demo..
WiFiEvents Demo..
curious, what does this do??

#include <ESP8266WiFi.h>
#include <stdio.h>


const char* ssid = "NodeMCU-v1.0.0";
const char* password = "123456789";

IPAddress local_IP(192, 168, 4, 22);
IPAddress gateway(192, 168, 4, 9);
IPAddress subnet(255, 255, 255, 0);


WiFiEventHandler stationConnectedHandler;
WiFiEventHandler stationDisconnectedHandler;


void setup() {
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
  WiFi.mode(WIFI_AP);
  WiFi.softAPConfig(local_IP, gateway, subnet);
  WiFi.softAP(ssid, password);
  // Call "onStationConnected" each time a station connects
  stationConnectedHandler = WiFi.onSoftAPModeStationConnected(&onStationConnected);

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

void onStationConnected(const WiFiEventSoftAPModeStationConnected& evt) {
  Serial.print("Station connected: ");
  Serial.println(macToString(evt.mac));
  digitalWrite(LED_BUILTIN, HIGH);
}

void onStationDisconnected(const WiFiEventSoftAPModeStationDisconnected& evt) {
  Serial.print("Station disconnected: ");
  Serial.println(macToString(evt.mac));
  digitalWrite(LED_BUILTIN, LOW);
}

String macToString(const unsigned char* mac) {
  char buf[20];
  snprintf(buf, sizeof(buf), "%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  return String(buf);
}



void loop() {
  delay(1000);
}

good luck.. ~q

I've verified serial does work but putting prints inside the loop but it still doesn't print anything when connecting / disconnecting.

And I should clarify, the LED turns on before I even connect and remains on no matter what i do, connecting/disconnecting/etc.

I'm not connecting instantly though, I've waited until I got a message over serial and then tried connecting, and still nothing. By connecting I mean connecting my phone to the AP

works OK

Station connected: 52:11:b4:d5:69:54
Station disconnected: 52:11:b4:d5:69:54
Station connected: 52:11:b4:d5:69:54
Station disconnected: 52:11:b4:d5:69:54

I tend to avoid putting Serial.print statements in interrupt handlers and callback functions
cause exceptions on ESP32 devices

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.