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