Post topics, source code that relate to the Arduino Platform

User avatar
By FOXi
#23918 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();
}
Last edited by FOXi on Fri Jul 24, 2015 10:38 am, edited 1 time in total.