ESP32 WiFi UDP Bunching Packets

I am attempting to send data with very small latency over a router to a VR headset. When the UDP Client is called and written to. It bunches packets together and sends them out in 200ms bursts. This isn't ideal since the packets should represent the most up-to-date values which are read from the sensors on the board. How do I make it so that the packets will be sent out immediately after being written to?

WiFi.begin(SECRET_SSID, SECRET_PASS);
	if(debugOutput) Serial.println("Connecting to Router");
	

	if (WiFi.waitForConnectResult() != WL_CONNECTED) {
		Serial.println("Connection Failed");
		init();
	}
	Serial.println("Connected to Router");

				udp.beginPacket("192.168.8.255", PORT_NUMBER);
				udp.write(buffer, bufferStream.bytes_written + 6);
				udp.endPacket();

I use the AsyncUDP library and it does not show the delay you see.

Hi, I was using Async and it gave me the same output.

Do you know how fast you are sending the packets ?

I found this:
Linux places very restrictive limits on the performance of UDP protocols by limiting the size of the UDP traffic that is allowed to buffer on the receive socket. It is highly recommended that you increase these OS limits to at least 25MB before trying to run UDP traffic to your server. 25MB is just a recommendation.

https://medium.com/@CameronSparr/increase-os-udp-buffers-to-improve-performance-51d167bb1360

Roundabout 150 70byte packets per second

That does not look like a high data rate to me.

Not a high data rate but a pretty high number of packets per second.

One every 7 msecs. So video is 30 frames per seconds so yes that is pretty fast.

#include "WiFi.h"
#include "AsyncUDP.h"

const char * ssid = "CarRouter2.4G";
const char * password = "viajero2023";

AsyncUDP udp;

void setup()
{
    Serial.begin(115200);
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, password);
    if (WiFi.waitForConnectResult() != WL_CONNECTED) {
        Serial.println("WiFi Failed");
        while(1) {
            delay(1000);
        }
    }else{
      Serial.print("Connected!");
    }
    delay(10);
}

void loop()
{
  byte b[5];
  b[0] = byte(13);
  b[2] = byte(13);
  b[1] = byte(13);
  b[3] = byte(13);
  b[4] = byte(13);

  udp.broadcastTo((uint8_t*)&b, sizeof(b),2000);
}

I put this code on the esp32 and it gives a similar result to the one before.

Almost like they are being buffered then sent out when the buffer length reaches some value then repeats. The time between bursts is halved though.

the time between bursts can vary quite a bit but it averages about 200ms.

I have even tried making multiple udp sender objects and sending data through different port and they always come out in these bursts, so I think it is something at a low level.

Yes, like the webpage i referenced the Os may be rate limiting udp packets. Above my pay grade.

Fair enough thanks for trying to help anyways.

1 Like

a UDP datagram carries the source and destination IP addresses, port numbers, etc and each packet has to be routed thru the network
it may be quicker to use a TCP protocol which sets up a virtual circuit thru the network
however, TCP has flow control, error correction, etc which add an overhead which may offset the advantge of the virtual circuit

Hey there, I set-up a tcp protocol for now and it works well. The only caveat is that udp supports broadcasting, which means that the board could send packets to a bunch of headsets with a single send. I am still interested in figuring out how to solve the udp problem but TCP works well for now thanks.

UDP multicast has the advantage you can transmit to multiple targets with a single transmission -
what microcontrollers are you using?
how far apart are the headsets?
are they all on the same subnet?

ESP32 Sparkfun Thing Plus

The headsets will all be less than 4m from the router, currently though just one headset although I am doing testing in the unity web player but regardless I know it is coming from the Arduino side since I am using wireshark to test for the latency and I have tested it with build versions on an android phone and on the headset

All the headsets are connected to a single router running without internet access

if using ESP devices try ESP-NOW and reference - direct device to device using MAC addresses - it has a multicast facility

They were using ESP-NOW before they hired me, not sure if they were aware it can be done direct to a non-esp device tho.

After looking into it for a while it seems to be an issue with the firmware on the board. May end up just getting a different board to be honest. It is weird though because I couldn't imagine anybody thinking that programming UDP packets to have a delay before sending is a good idea.

Anyway thanks for the help, much appreciated. Will just keep to TCP for now because I don't think they have any plans to use multiple VR headsets anytime soon.