Uno R4 WiFi - under the hood problem with WiFi.Status ?

Hi to all,
as part of other projects, i need a LED turned on when a device is connected to the AccessPoint. The LED should turn off, after disconnecting that device from the AP.

After reset the WiFi status = 7.
After connecting a device to the AP, the WiFi status = 8.
After disconnecting that device from the AP, the WiFi status remains 8.

I use this code for checking the WiFi status and switching the LED:


#include "WiFiS3.h"

char ssid[] = "CheckAccessPoint";        
char pass[] = ""; 
int keyIndex = 0;                 
int led =  LED_BUILTIN;
int status = WL_IDLE_STATUS;

void setup() {
  
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);

  Serial.begin(9600);

  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    while (true);
  }
 
  WiFi.config(IPAddress(192,168,1,1));

  Serial.println("Creating access point with UNO R4 WiFi... please wait!");
 
  status = WiFi.beginAP(ssid, pass);

  if (status != WL_AP_LISTENING) {
    Serial.println("Creating access point failed");
    while (true);
  }
  delay(2500);

  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  IPAddress ip = WiFi.localIP();
  Serial.print("IP : ");
  Serial.println(ip);

  Serial.println("You may now connect a device to this AP...");
}

void loop() {                                     
                                        
   if (status != WiFi.status()) {
      status = WiFi.status();

      if (status != WL_AP_CONNECTED) {
      Serial.println("No Device connected to AP");
      digitalWrite(13, LOW);
    }

        if (status == WL_AP_CONNECTED) {
        Serial.println("Device connected to AP");
        digitalWrite(13, HIGH);
    }   
          else {
          Serial.println("Device disconnected from AP");
          digitalWrite(13, LOW);
    }
  }
}

Any ideas whats wrong here?

I would be very happy if someone would help me.

I'm finding this just doesn't work for me on Uno R4 Wifi.
int status = WiFi.status();
Will add some details tomorrow. It always returns a 0.

Hi togletree,
many thanks for your response. I would be very happy about help.

Sorry, I am not much help here as I have not played very much with the WIFI stuff.

But noticed one issue with your code:
If you reformat it (ctrl+t) you see:

void loop() {

  if (status != WiFi.status()) {
    status = WiFi.status();

    if (status != WL_AP_CONNECTED) {
      Serial.println("No Device connected to AP");
      digitalWrite(13, LOW);
    }

    if (status == WL_AP_CONNECTED) {
      Serial.println("Device connected to AP");
      digitalWrite(13, HIGH);
    } else {
      Serial.println("Device disconnected from AP");
      digitalWrite(13, LOW);
    }
  }
}

If the state is not WL_AP_CONNECTED you will probably receive two messages and the LED will be set LOW twice. Which is probably not your main issue and probably does not matter, but noticed it so thought I would mention it.

Hi KurtE,
thank you anyway!