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

GeorgeFlorian1
Posts: 160
Joined: Thu Jan 31, 2019 2:32 pm

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

Postby GeorgeFlorian1 » Mon May 06, 2019 9:48 am

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 !

boarchuz
Posts: 606
Joined: Tue Aug 21, 2018 5:28 am

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

Postby boarchuz » Tue May 07, 2019 11:09 am

Your question doesn't make sense. When in station mode, your ESP32 *is* the client.

GeorgeFlorian1
Posts: 160
Joined: Thu Jan 31, 2019 2:32 pm

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

Postby GeorgeFlorian1 » Wed May 08, 2019 8:03 am

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 ?

boarchuz
Posts: 606
Joined: Tue Aug 21, 2018 5:28 am

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

Postby boarchuz » Wed May 08, 2019 9:45 am

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

pedros89
Posts: 4
Joined: Tue Aug 03, 2021 9:28 am

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

Postby pedros89 » Fri Aug 27, 2021 3:09 pm

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.

Who is online

Users browsing this forum: No registered users and 76 guests