0

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

1 Answers1

0

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 
} 
hcheung
  • 3,377
  • 3
  • 11
  • 23
  • Thanks for your answer. I did the correction but it still doesn't work. once the client disconnected, the server stopped at the last staus( Either on or off) and never back to work unless hard resetting. – Hay packup Mar 07 '22 at 09:16