ESP8266 - WIFI / UDP hangup

Hello,

I have a simple sketch, which is sending UDP messages to a .NET based application.

Under certain conditions, the .NET based application will send a string back to the ESP.

However, The ESP sending data works fine.

But it needs only one UDP message from the .NET side to make the WIFI on the ESP stop working.

In the serial monitor I see, that the ESP is still trying to loop and send data, but I do not receive anything any more.

Pleas help.

Here is my code:

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

// Access Point Zugang
char ssid[] = "ESP_COLLECTOR";  // SSID
char pass[] = "shivashakti";    // password

/***  Zentrale ***/

IPAddress remoteIpNo(192,168,1,128);  // IP Adresse.
unsigned int remotePortNo = 20000;     // Port. [20000-29999] 

/*** Lokal ***/

IPAddress localIpNo(192,168,1,219);   // IP Adresse.
IPAddress subnet(255,255,255,0);      // Subnet Mask.
unsigned int localPort = 10000;       // Port. [10000-19999]
IPAddress gateway(192,168,1,1);
//IPAddress broadcastIP(192,168,1,255);
//IPAddress noIp(0,0,0,0);

/*** Globale Variablen ***/

// Puffer für hereinkommende Packete.
char packetBuffer[64];

// Flag for temp read and UDP send,
byte receivedPacket = 0;  

/*** Objekte ***/

// Intializse UDP
WiFiUDP udp; 

void setup() {

  // Serielle Schnitstelle initialisieren.
  Serial.begin(115200);
  Serial.println("\r\nUDP Sender begin");

  // WIFI initialisieren.
  WiFi.mode(WIFI_AP);
  WiFi.config(localIpNo, gateway, subnet);
  WiFi.begin(ssid, pass);
  
  delay(1000);

  // Mit dem WLAN Netzwerk verbinden.
  Serial.println("attempt to connect to Wifi network");
  while ( WiFi.status() != WL_CONNECTED) {
    delay(50);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi Started");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  
  // UDP starten.
  Serial.println("Starting UDP");
  delay(1000);
  udp.begin(localPort);
  Serial.print("Local port: ");
  Serial.println(localPort);
}

void loop() {

  /*** Daten senden ***/
  sendPacket(send_data(20.02));

  /*** Daten empfangen ***/

  // Verzögerung und auf Daten warten.
  for (int i=0; i <= 100; i++){

    // Empfangene Daten lesen.
    int packetSize = udp.parsePacket();

    // Wenn Daten empfangen wurden.
    if(packetSize > 0) { 
      readPacket(packetSize); 
    }

    // Verzögerung.
    delay(10);
   }
}

// UDP Packet senden.
void sendPacket(String dataString) {
  int len = 0;

  Serial.print("\r\nSend To ");
  Serial.println(remoteIpNo);
  Serial.print(dataString);
  yield();
  
  udp.beginPacket(remoteIpNo, remotePortNo);
  udp.print(dataString);
  udp.endPacket();

  yield();
  delay(50);
}

// UDP Packet lesen.
int readPacket(int packetSize) {
  char receiveBuffer[64]; 
  udp.read(receiveBuffer, packetSize);
  receiveBuffer[packetSize] = 0;
  String packetString = receiveBuffer;
  remoteIpNo = udp.remoteIP();
  remotePortNo = udp.remotePort();
  
  Serial.print("\r\nReceived packet of size ");
  Serial.print(packetSize);
  Serial.print(" From ");
  Serial.print(remoteIpNo);
  Serial.print(", port ");
  Serial.println(remotePortNo);
  Serial.print("Contents:");
  Serial.println(packetString);

  return packetSize;
}

/*******************************************************/
/* ########## Sending data to the .NET side ########## */
/*******************************************************/

// Send information to the .NET side.
void send_info(String value) {
  Serial.flush();
  Serial.print("<");
  Serial.print(value);
  Serial.print(">");
  Serial.flush();
}

// Send sensor data to the .NET side.
String send_data(float value) {
  String payload = String(value);
  String header = String(payload.length());
  String message = "{(" + header + "|temperature_only|1)" + "[" + payload + "]}";
  return message;
}

Andreas1984:

    if(packetSize > 0) { 

readPacket(packetSize);
    }

Why are you reading the packet into an int variable, while readPacket expects a uint8_t array buffer?
Why aren't you checking if the packet fits your buffer?

Pieter

Look at the definition of udp.parsePacket:

It shows includes
#include <SPI.h> // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h> // UDP library

and you're using:
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

WiFiUdp.h has the same functionality and may be fine, but something to look at.

A search shows yet another way to do this: