I've just written a pair of programs, one to send and one to receive UDP messages, between two ESP32 controllers. Written using VSCode and Platform IO.
Code all works, and I see all sent messages on wireshark, but the receiver misses quite a few of the messages, regardless of how close to the sender it is. I even tried using an ESP32 with external antenna with no discernible improvement.
Would anyone care to offer an idea of why so many messages are being missed? Sometimes its say one in 10, sometimes it could be 2 or three missed in a row. I was expecting none to be missed to be honest. (I tried a couple of different ways of sending the packet, hence the commented out stuff in the sender - but I think its in the receipt that the error happens).
Here is the sender code:
#include <Arduino.h>
#include <WiFi.h>
//#include <AsyncUDP.h>
#include <WiFiUDP.h>
#define TX_PORT 1234
const char* ssid = "********";
const char* password = "********";
byte counter = 0;
//AsyncUDP udp;
WiFiUDP udp;
void setup() {
pinMode(2, OUTPUT);
Serial.begin(115200);
WiFi.disconnect(true);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid,password);
Serial.print("Connecting to Wifi");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("\nWifi Connected");
}
void loop()
{
//char msgBuf[100];
//sprintf(msgBuf,"Hello from Node Msg#%d",counter);
//udp.broadcastTo(msgBuf, TX_PORT);
//Serial.printf("Sent UDP Message [%s]\n",msgBuf);
//counter++;
//udp.broadcastTo("Hello World", TX_PORT);
uint8_t buffer[50] = "hello world";
//This initializes udp and transfer buffer
udp.beginPacket("192.168.0.255", TX_PORT);
udp.write(buffer, 11);
udp.endPacket();
memset(buffer, 0, 50);
delay(1000);
}
and here is the receiver code:
#include <Arduino.h>
#include <WiFi.h>
#include <AsyncUDP.h>
#define RX_PORT 1234
const char* ssid = "********";
const char* password = "********";
byte counter = 0;
AsyncUDP udp;
bool lightOn = true;
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
WiFi.disconnect(true);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid,password);
Serial.print("Connecting to Wifi");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
if(udp.listen(RX_PORT))
{
Serial.printf("Listening for UDP on Port %04d, IP:",RX_PORT);
Serial.println(WiFi.localIP());
udp.onPacket([](AsyncUDPPacket packet){
Serial.print("UDP Packet Type: ");
Serial.print(packet.isBroadcast()?"Broadcast":packet.isMulticast()?"Multicast":"Unicast");
Serial.print(", From: ");
Serial.print(packet.remoteIP());
Serial.print(":");
Serial.print(packet.localPort());
Serial.print(", To: ");
Serial.print(packet.localIP());
Serial.print(":");
Serial.print(packet.localPort());
Serial.print(", Length: ");
Serial.print(packet.length());
//Serial.print(", Data: ");
//Serial.write(packet.data(), packet.length());
Serial.println();
char* tempStr = (char*) malloc (packet.length()+1);
memcpy (tempStr, packet.data(), packet.length());
tempStr[packet.length()] = '\0';
String message = String(tempStr);
free(tempStr);
Serial.println(message);
// do message actions here.....
if (lightOn)
digitalWrite(LED_BUILTIN, HIGH);
else
digitalWrite(LED_BUILTIN, LOW);
lightOn = !lightOn;
});
}
}
void loop()
{
//delay(1000);
//udp.broadcast("Hello from a node");
}
Cheers,
Jim