'How to know client status in ESP8266?

I am following the code from https://siytek.com/communication-between-two-esp8266/

I am trying to communicate between two ESP8266. The only problem is when the client disconnected due to power losses; the server freezes at the last status until the client turns on again. I would let the server set the write (Relay, LOW) if the client disconnected and return to the normal loop when the client connected again.

Your support is highly appreciated.

Code for the client ESP

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
  
// Set WiFi credentials
#define WIFI_SSID "TheOtherESP"
#define WIFI_PASS "flashmeifyoucan"
 
// UDP
WiFiUDP UDP;
IPAddress remote_IP(192,168,4,1);
#define UDP_PORT 4210

void setup() {
 
  // Setup IO
    pinMode(LED_BUILTIN, OUTPUT);
  // Setup serial port
  Serial.begin(115200);
  Serial.println();
  
  // Begin WiFi
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  WiFi.mode(WIFI_STA);
  
  // Connecting to WiFi...
  Serial.print("Connecting to ");
  Serial.print(WIFI_SSID);
  // Loop continuously while WiFi is not connected
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(100);
    Serial.print(".");
  }
  
  // Connected to WiFi
  Serial.println();
  Serial.print("Connected! IP address: ");
  Serial.println(WiFi.localIP());
 
  // Begin UDP port
  UDP.begin(UDP_PORT);
  Serial.print("Opening UDP port ");
  Serial.println(UDP_PORT);
  
}
  
void loop() {
 
  // Sent to server 
  char PumpStatus = 1;
 
  // Send Packet
  UDP.beginPacket(remote_IP, UDP_PORT);
  UDP.write(PumpStatus);
  Serial.println("Pump ON");
  digitalWrite(LED_BUILTIN,HIGH);
  
  UDP.endPacket();
  delay(1000);

     PumpStatus = 0;
 
  // Send Packet
  UDP.beginPacket(remote_IP, UDP_PORT);
  UDP.write(PumpStatus);
    Serial.println("Pump OFF");
  digitalWrite(LED_BUILTIN,LOW);
  UDP.endPacket();
  delay(1000);
   
}

Code for the Server ESP

  
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
 
// Set AP credentials
#define AP_SSID "TheOtherESP"
#define AP_PASS "flashmeifyoucan"
#define Relay 0
// UDP
WiFiUDP UDP;
IPAddress local_IP(192,168,4,1);
IPAddress gateway(192,168,4,1);
IPAddress subnet(255,255,255,0);
#define UDP_PORT 4210
 
// UDP Buffer
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
 
void setup() {
 
  // Setup LED pin
 pinMode(LED_BUILTIN, OUTPUT);
   pinMode(Relay, OUTPUT); 
  // Setup serial port
  Serial.begin(115200);
  Serial.println();
 
  // Begin Access Point
  Serial.println("Starting access point...");
  WiFi.softAPConfig(local_IP, gateway, subnet);
  WiFi.softAP(AP_SSID, AP_PASS);
  Serial.println(WiFi.localIP());
 
  // Begin listening to UDP port
  UDP.begin(UDP_PORT);
  Serial.print("Listening on UDP port ");
  Serial.println(UDP_PORT);
 
}
 
void loop() {
 
  // Receive packet
  UDP.parsePacket();
  UDP.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
  if (packetBuffer[0]){
    digitalWrite(LED_BUILTIN, HIGH);
    Serial.println("Relay ON");
    digitalWrite(Relay, HIGH);
    
  } else {
    digitalWrite(LED_BUILTIN, LOW);
    Serial.println("Relay OFF");
    digitalWrite(Relay, LOW);
  }      
 
} 


Solution 1:[1]

In your server code, the UDP.parsePacket() returns the size of the packet in bytes, or 0 if no packets is available, so you should only proceed to read the packet and run the rest of codes if UDP.parsePacket() return a value other than 0.

void loop() {
 
  if (UDP.parsePacket()) {
    UDP.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
    if (packetBuffer[0]){
      digitalWrite(LED_BUILTIN, HIGH);
      Serial.println("Relay ON");
      digitalWrite(Relay, HIGH); 
    } else {
      digitalWrite(LED_BUILTIN, LOW);
      Serial.println("Relay OFF");
      digitalWrite(Relay, LOW);
    }
  }
  delay(100);   // adjust this if necessary 
} 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 hcheung