Page 1 of 1

How can I know if I have a client connected to the ESP ?

Posted: Mon May 06, 2019 9:48 am
by GeorgeFlorian1
OS: Linux Mint 19.1
Board: ESP32 Wrover-B DEV KIT V4

Hello !

I'm having difficulties finding a way to know when a client has connected to the ESP when it is in STA Mode.

Code: Select all

#include <WiFi.h>

void setup() {
  WiFi.begin(ssid, password);
  delay(500);
  int k = 0;
  while (WiFi.status() != WL_CONNECTED && k<20) {
    k++;
    delay(1000);
    Serial.println((String)"Attempt " + k + " - Connecting to WiFi..");
  }
  if(WiFi.status() == WL_CONNECTED) {
    String IP = WiFi.localIP().toString();
    Serial.println((String)"Connected to " + ssid + " with IP addres: " + IP);
  }
}
Is there a way of knowing if and who connected to the ESP ?

I found WiFi.softAPgetStationNum(); for AP Mode, but I couldn't find a similar function for STA Mode.

Thank you !

Re: How can I know if I have a client connected to the ESP ?

Posted: Tue May 07, 2019 11:09 am
by boarchuz
Your question doesn't make sense. When in station mode, your ESP32 *is* the client.

Re: How can I know if I have a client connected to the ESP ?

Posted: Wed May 08, 2019 8:03 am
by GeorgeFlorian1
boarchuz wrote:
Tue May 07, 2019 11:09 am
Your question doesn't make sense. When in station mode, your ESP32 *is* the client.
Ok then.
I have a ESPAsyncWebServer on the ESP. How can I check if somebody connected to that webserver ?

Re: How can I know if I have a client connected to the ESP ?

Posted: Wed May 08, 2019 9:45 am
by boarchuz
I know they seem to have a lot of overlap but a server and WiFi are completely different things.
It doesn't look like that library has an easy method like that, but do you really need it? There's no Keep-Alive wizardry going on or anything, you can safely assume the connection will be closed after each response.
I suppose you could implement your own. Here's one basic concept to give you an idea but you'd need to think about it a lot more to develop something reliable that could actually be used:

Code: Select all

myServer.on("/", HTTP_GET, [&](AsyncWebServerRequest * request) {
  connections++;
  request->send(SPIFFS, "/reallylarge.file");
  connections--;
});
void endServer(){
  while(connections); //wait
  myServer.end();
}

Re: How can I know if I have a client connected to the ESP ?

Posted: Fri Aug 27, 2021 3:09 pm
by pedros89
Hello,
with normal web server of Arduino core there is this line in https://github.com/espressif/arduino-es ... ebServer.h

Code: Select all

virtual WiFiClient client() { return _currentClient; }
So in order to see if a client is connect you can write

Code: Select all

void checkClient(){

  WiFiClient myclient = server.client();

  if(myclient==0){
    Serial.println("client not connected");
  }
  if(myclient.available()){
    Serial.println("client available");
  }  
  if(myclient.connected()){
    Serial.println("client connected");
  }
}
Run this funciton in the Arduino loop and it will return the different statuses.