WiFiNINA - Check if a device is connected to AP nework

Hi everyone, I am pleased to write here, hoping in the help of this great community.
This is my first post using this account, 'cause I became an educator and I changed account, but I use Arduino from years, and I was already active in this forum - few years ago - in the Ita section. But, let’s move to the topic of this post.

I am trying, using an Arduino MRK1010, to create an Access Point network, and to host a server onto the board.
I’d like to know if a device has been connected, or disconnected, from my network. The final purpose is to run a task if any device is connected, and to run anotherone if no devices are connected.

I thought to use

WiFi.status()

wich, according to what I understood, should return a value when a device has been connected, and another one if a device is not connected.

Then I check if the status value has changed… and it does not work.
I can understand if a device connect to the network, but not when it disconnect. If I use the first commented line in the loop to print the status value, it is 7 when waiting a connection, it turns to 8 when a device connects, but it still remains 8 when the device disconnects. I trusted that it should became back 7 if a device disconnected.

Is there anyone who can help me, maybe finding an error that I am not able to see, or suggesting me another way to do what I am trying to achieve?

Below my piece of code that I am trying to use.

I can try to give you further information if needed, please ask any information you need and I have not provided.

Edit: I noticed just now that a “Networking, Protocols, and Devices” section exist. I kindly ask moderators to move my post there if they think this section better suits my question.

Thanks in advance, hoping for your replies,
my kindest regards,
Luca Gilardi

#include <SPI.h> // Needed by WiFiNINA
#include <WiFiNINA.h> // WiFi and network library

const char ssid[] = "myssid";// your network SSID (name)
const char pass[] = "mypass";    // your network password (use for WPA)
IPAddress ip(192, 168, 4, 1);

int status = WL_IDLE_STATUS;
int port = 80;
WiFiServer server(port);
WiFiClient client;

void setup() {
  //Initialize serial
  Serial.begin(9600);
  while (!Serial)
    ; // Wait for serial connection

  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.print("ERROR! Communication with WiFi module failed!\nExecution aborted\n");
    while (true) // don't continue
      ;
  }

  // Create access point
  Serial.print("Creating Access Point\n");
  status = WiFi.beginAP(ssid, pass);
  if (status != WL_AP_LISTENING) {
    Serial.print("ERROR! Access Point creation failed!\nExecution aborted\n");
    while (true) // don't continue
      ;
  }

  delay(10000); // wait 10 seconds for connection

  Serial.print("Access Point started, SSID = " + String(ssid) + "\n");

  // start the web server
  Serial.print("Starting server\n");
  server.begin();
  Serial.print("Server started\nPort = " + String(port) + "\t IP = " + ip + "\n\n");
  Serial.print("Waiting for a connectiont\n");
}


void loop() {
  Serial.println(WiFi.status());
  if (status != WiFi.status()) { // If the connection status to the AP has changed
    status = WiFi.status(); // Update the status
    if (status == WL_AP_CONNECTED) { // If a device has connected to the AP
      Serial.print("Device connected to AP\n");
    }
    else { // a device has disconnected from the AP, go back in listening mode
      Serial.print("Device disconnected from AP\n");
    }
  }
}

I can understand if a device connect to the network, but not when it disconnect. If I use the first commented line in the loop to print the status value, it is 7 when waiting a connection, it turns to 8 when a device connects, but it still remains 8 when the device disconnects. I trusted that it should became back 7 if a device disconnected.

The purpose of the AP mode in these devices is not replacing a WiFi access point. It’s for having the possibility of doing an initial setup over the WiFi network, including configuring the device as a client in your existing WiFi network. You also shouldn’t expect that “access point” to allow communication between the other participants or doing any routing or the like.
So the above described behavior is to be expected.

BTW, the actual hardware behind the WiFiNINA is an ESP32 by Espressif, so even a modified firmware cannot do anything that processor doesn’t support.

Hi Pylon,
Thanks for your reply. So, I am understanding the issue.

Let me try to explain what I’d like to achieve: I want to create a remote-controlled robot, where both the controller and the robot are MKR1010 based. I’m trying to put the robot in an emergency mode (all motors brakes) if the connection to the controller is lost. Do you, or someone else, can suggest to me any path that I can follow? I’d like to do not use external hardware, if possible. If there aren’t other ways I can implement an external router, but I prefer to do not.
Another question: is it possible to understand, if I use an external router, from the robot side, if the controller loses connection to the router? I can’t figure any way to do it.

With warm regards,
Luca

Just wondered whether you should be using Server.status() rather than Wifi.status() ?
https://www.arduino.cc/en/Reference/WiFiNINAServerstatus

Hi countrypaul,
thanks for your reply.

I already tryed to use it. Calling, from the server side,

server.status();

it will always return 1, and calling

client.status();

will return 0 until the client connects. After the connections it became 4, but still remain 4 also if clients disconnect.

Here my interested part of the server code:

void setup() {
  //Initialize serial
  Serial.begin(9600);
  delay(5000); // Give enough time for serial connection

  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.print("ERROR! Communication with WiFi module failed!\nExecution aborted\n");
    while (true) // don't continue
      ;
  }

  // Create access point
  Serial.print("Creating Access Point\n");
  status = WiFi.beginAP(ssid, pass);
  if (status != WL_AP_LISTENING) {
    Serial.print("ERROR! Access Point creation failed!\nExecution aborted\n");
    while (true) // don't continue
      ;
  }
  delay(10000); // wait 10 seconds for connection
  Serial.print("Access Point started, SSID = " + String(ssid) + "\n");

  // start the web server
  Serial.print("Starting server\n");
  server.begin();
  Serial.print("Server started\nPort");
  Serial.print("Waiting for a client\n");
}


void loop() {
  // wait for a client
  while (!client.connected())
    client = server.available();

  if (client.available() > 0) { // if there's something to read from the client
    // do something
  }

  Serial.print("Serv: ");
  Serial.print(server.status());
  Serial.print('\t');
  Serial.print("Cli: ");
  Serial.println(client.status());
}

and here from the client side

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  delay(5000);

  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }

  // Connect to the network
  Serial.print("Attempting connecting to the network. SSID = ");
  Serial.println(ssid);
  while (status != WL_CONNECTED) {
    status = WiFi.begin(ssid, pass);
    delay(10000);  // wait 10 seconds for connection:
  }

  Serial.print("Connected to the network ");
  Serial.println(ssid);
  Serial.print("My IP address is ");
  Serial.println(ip);

  Serial.println("Connecting to the server");
  while (!client.connect(serverIP, 80)) {

  }
  Serial.print("Connected to the server at ");
  Serial.println(serverIP);
}


void loop() {
  if (client.connected()) {
    // do something
  } else {
    Serial.println("ERROR - Client disconnected. Attemtping new connection.");
    client.connect(serverIP, 80);
  }
}

With warm regards,
Luca

Could you use WIFI.ping to check whethee the client is still connected?
https://www.arduino.cc/en/Reference/WiFiNINAPing

Thanks for this,
It could be an option, but I excluded it because - as far as I understood - the ping method has a very long timeout (maybe 5 s) if the pinged device is not reachable. During this time it seems that the program will wait, so I will lose control of the robot until the end of the timeout. Do you know if the timeout is adjustable? In other posts on the net, I saw that it could be hardcoded and then fixed.

Anyway, I'll try this option.

Obviously, any other suggestion will be welcome.

My kindest regards,
Luca

I thought you could set your own timeout, but it seems I'm mistaken which does limit its use quite significantly for your purpose.

You could however easily adjust the timeout down from 5000 to something more suitable for your purposes. Looking in nina-fw/WiFi.cpp at bb31ec8102aa02821b3c39d8b6a28324bb278ad9 · arduino/nina-fw · GitHub shows that the timeout is a simple parameter currently set to 5000 (I presume milliseconds).
You could easily adjust that value in your own copy of the library.

Just a suggestion.

Thanx,
I will try ASAP an I'll report here.