-->
Page 1 of 1

UDP send/reply buffer problem [SOLVED]

PostPosted: Wed Jul 22, 2015 7:42 am
by FOXi
Hello,

I want send data from my esp-01 to server and server must confirm that receive this data. If server doesn't confirm receive data esp resend this data. Everything is working. The problem occur if during operation my UDP server fail. If my server fail, esp continue like to receive reply messages. Because my server send multiple reply, ESP store this reply from previous cycle and find that UDP server fail usually after 4 cycle and not immediately.
It seem that esp receive and store data in some buffer and clear this buffer after 4 cycle but I need clear this buffer immediately or set this buffer to my data size.

anybody help please?

Code: Select all#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

void loop() {
  if (digitalRead(startPin)) { // if (!digitalRead(startPin))
    Serial.println("start");
    receivePacketSize = 0;
   
    // Send packet and waiting for delivery confirmation
    while (!receivePacketSize) {
      sendPacket();
      Serial.println("sending...");
      receivePacketSize = udp.parsePacket();     ////////// this is my problem, it  4 times return "true" if my server fail////////////
     
      // Read packet data
      if(receivePacketSize) {
        Serial.println("reading incomming data...");
        int len = udp.read(packetBuffer, receivePacketSize);
        if (len > 0) {
            packetBuffer[len] = 0;
        }
     
        // Check if reply data are correct
        if ( strcmp(packetBuffer, id) != 0) {
          receivePacketSize = 0;
          Serial.println("invalid data");
        }
      }
    }
    udp.flush();
   
    Serial.println(packetBuffer);
    delay(5000);   
  }
  else {
    Serial.println("Cakam za startom...");
  }
 
  delay(100);
}

// Create and send UDP packet
void sendPacket() {
  udp.beginPacket(gateway, remotePort);
  udp.write(id);
  udp.endPacket();
}

Re: UDP send/reply buffer problem

PostPosted: Fri Jul 24, 2015 10:35 am
by FOXi
Problem solved. I add begin(localport) to my loop() and now program working as expected. :D

Re: UDP send/reply buffer problem [SOLVED]

PostPosted: Tue Apr 09, 2019 11:05 am
by bholesh
I also seen the solution of this problem

Re: UDP send/reply buffer problem [SOLVED]

PostPosted: Sat Jun 12, 2021 9:13 pm
by SharrySteve1
Used the "instruction" begin(localport) to my loop(). The program worked fine. Thank you so much.