WiFi.softAPgetStationNum() problems

HI All,
I have 2 ESP32 CAM AI thinkers working, one in AP mode and one in STA mode, both on same SSID. I can view both on individual IP addresses just fine.
On the AP board, I’m trying to flash the little red built-in LED GPIO33, when the STA one is connected.

void loop() {
    delay(500);
    uint8_t clientCount = WiFi.softAPgetStationNum();
    if (clientCount == 0) {
      LED_BLINKER = 0;                       // STOP LED blink
      digitalWrite(LED_BUILTIN, LOW);        // built-in LED ON (LED has pull-up)
    } else {
      LED_BLINKER = 1;                       // make LED blink
    }
    
    Serial.println(WiFi.softAPgetStationNum());

    // =======================================================
    // ALL GOOD ????      FLASH BUILT IN LED GPIO33 ???
    // =======================================================
    if (LED_BLINKER == 1) {                   // WiFi OK ?
      if (millis() - LED_Flasher >= 500) {
        digitalWrite(LED_BUILTIN, HIGH);      // built-in LED OFF (LED has pull-up)
      }
      if (millis() - LED_Flasher >= 1000) {
        digitalWrite(LED_BUILTIN, LOW);      // built-in LED ON   (LED has pull-up)
        LED_Flasher = millis(); 
      }
    } 
}

So, switch on the AP board and GPIO33 LED comes ON and WiFi.softAPgetStationNum() is 0 on the serial monitor.
Then switch on the STA board and when it connects, GPIO33 LED starts flashing ok and WiFi.softAPgetStationNum() shows as 1 on the serial monitor.
However, when I switch the STA board OFF, WiFi.softAPgetStationNum() stays as 1, doesn’t go to 0. This leaves the LED still flashing !!!

A few minutes later, the LED stops flashing and WiFi.softAPgetStationNum() has gone to 0, but this takes ages.
Any ideas ???
Thank you
Trevor

This is actually correct. If you just power off the STA, then the AP does not immediately notice the STA is gone. No packet (like a WiFi deauthentication packet) is sent from the STA to the AP, with a power loss the STA just stops sending from the AP’s point of view. The STA is only registered as “disconencted” when a timeout occurs and no data is received from the STA.

Per ESP-IDF documentation, that timeout is 300 seconds (=5 minutes). So that’s where your “a few minutes later” is coming from.

If you don’t like this behavior and would want your AP to behave more aggressively, change it in the firmware running the SoftAP.

#include "esp_wifi.h" /* may be needed if not auto-included by just WiFi.h */

/* 15 seconds timeout for a non-sending STA to be seen as 'disconnected' on the SoftAP interface */
esp_wifi_set_inactive_time(WIFI_IF_AP, 15);

Conversely, a STA will not immediately notice a powered-down AP, again per above that timeout is just 6 seconds though. You can change it with esp_wifi_set_inactive_time(WIFI_IF_STA, 3); to e.g. 3 seconds.

Perfect as always Max, thank you so much.
Aha !! yes the AP timeout is indeed 300s = 5 minutes, I understand what’s happening now.
I’ve taken your tips and changed it to 11 seconds (more than 10, as the spec mentions).
The STA timeout is the default 6 seconds, which works for me.
Yes, did need “esp_wifi.h”
So I now have a system where the LEDs on both boards are ON to confirm “Power OK” and then FLASH when the STA is connected to the AP.
Each board stops flashing if there’s a Wifi or power probelm on the other board.

BTW, I’m building some rechargeable battery powered, remote wildlife cameras.
I’ve tested the two CAM boards at around 75 meters (using external antennae) and viewing both symultaneously on my laptop.
I can see both LEDs from quite a long way away, which is perfect.
Thank you Max