Server monitoring clients

bleepooo
Posts: 1
Joined: Sat Jan 18, 2020 5:14 pm

Server monitoring clients

Postby bleepooo » Sat Jan 18, 2020 5:53 pm

Hello. This is my first time creating a project using ESPs and is also my first time in the forum.

I am making a simple project that consists of a server that constantly monitors clients connected into it. I am using ESP32 as a server in soft AP mode and ESP01s as the connected clients in station mode. The server(ESP32) is being updated of the connected clients(ESP01) via serial monitor. Once a connection is established, it displays a "station connected" text and its MAC address.

My problem is that the clients maintain their connection to the server even after disconnecting the client's power source. This poses a problem when determining if the client has gone out of range from the server.

this is the code uploaded on the client:

Code: Select all

#include<ESP8266WiFi.h>

const char* ssid = "ESP32";
const char* password = "12345678";

void setup(){
  WiFi.mode(WIFI_STA);

  WiFi.begin(ssid, password);


 }
 void loop(){

  }
and on the server:

Code: Select all

#include <WiFi.h>


WiFiServer server(80);
bool alreadyConnected = false;

const char *ssid = "ESP32";
const char *password = "12345678";

IPAddress local_IP(192, 168, 4, 1);
IPAddress gateway(192, 168, 4, 1);
IPAddress subnet(255, 255, 0, 0);
void WiFiStationConnected(WiFiEvent_t event, WiFiEventInfo_t info){
   
  Serial.println("Station connected");
   
  for(int i = 0; i< 6; i++){
     
    Serial.printf("%02X", info.sta_connected.mac[i]);  
    if(i<5)Serial.print(":");
  }
 
  Serial.println("\n------------");
}

void WiFiStationDisconnected(WiFiEvent_t event, WiFiEventInfo_t info){

  Serial.println("Station disconnected");
   
  for(int i = 0; i< 6; i++){
     
    Serial.printf("%02X", info.sta_disconnected.mac[i]);  
    if(i<5)Serial.print(":");
  }

  Serial.println("\n------------");
}

void setup() {
  
  Serial.begin(115200);
  WiFi.softAP(ssid, password);
 WiFi.config(local_IP, gateway, subnet);
  delay(3000);


  WiFi.onEvent(WiFiStationConnected, SYSTEM_EVENT_AP_STACONNECTED);

  WiFi.onEvent(WiFiStationDisconnected, SYSTEM_EVENT_AP_STADISCONNECTED);
}
void loop() {
  // wait for a new client:
  WiFiClient client = server.available();

  // when the client sends the first byte, say hello:
  if (client) {
    if (client.connected()) {
      if (!alreadyConnected) {
        Serial.println("We have a new client");
        client.println("Hello, client!");
        alreadyConnected = true;
      }

      while (client.available()) {
        // read the byte incoming from the client:
        char c = client.read();
        // echo the byte back to the client:
        client.write(c);
        // echo the byte to the server as well:
        Serial.write(c);
      }
    } else {
      alreadyConnected = false;
      client.stop();
    }
  }
  }
Please excuse my lack of knowledge since I only started this recently and have no background in such. Thus, I am missing out on a lot of information here. I hope you can help me with my problem as well as educate me. Thank you.

Who is online

Users browsing this forum: No registered users and 54 guests