Skip to content

WiFiEvent, disconnection not detected #3279

@Banzai00

Description

@Banzai00

Basic Infos

Hardware

Hardware: Wroom-02
Core Version: 2.3.0-rc2

I have two Wroom-02 : one is in the soft AP mode and the other in station mode. And i want to detect when the second one connect or disconnect to/from the AP of the first one. So, i am using WiFiEvents example.
When i power on the wroom 02 in station mode, the onStationConnected works and the connection is detected. But my problem is when i power off the wroom 02, the onStationDisconnected don't get called and the disconnection is not detected.
This problem only occur with two esp. When, i connect and disconnect using a smartphone it works great.
The code of the esp in ap mode :

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

const char *ssid = "APssid";
const char *password = "APpassword";

WiFiEventHandler stationConnectedHandler;
WiFiEventHandler stationDisconnectedHandler;


void setup() {
  Serial.begin(115200);

  // Don't save WiFi configuration in flash - optional
  WiFi.persistent(false);

  // Set up an access point
  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);  
}

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

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

void loop() {
  
}

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);
}

The code of the esp in station mode :

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

IPAddress staticIP(192,168,4,222);
IPAddress gateway(192,168,4,1);
IPAddress subnet(255,255,255,0);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println();
  
  WiFi.mode(WIFI_STA);
  WiFi.begin("APssid", "APpassword");
  WiFi.config(staticIP, gateway, subnet);
  Serial.print("Connecting");
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");   
    
  }
  Serial.println();

  Serial.print("Connected, IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  // put your main code here, to run repeatedly:

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions