Delayed TCP packets while using WiFi.h on esp32

Hi everyone,

In one of my current projects, I connect multiple esp32 (programmed with arduino code) to a python server on my local network. For the wifi connection and the socket creation, I use the WiFi.h library. The connection is fine, and I can send/receive packets without transmission problems.
Problems start when I try to have low latency packet sending (~1ms would be perfect). When my server send data to the esps, messages arrived grouped and not just after the sending. The problem looks like a buffer problem, but even with the proper flags set, the delay persists.

Here is a simplified version of my codes to exactly understand what I am doing
ESP code

#include <Arduino.h>
#include <WiFi.h>

void setup() {
  Serial.begin(115200);
  delay(500);

  // Connect to wifi
  config_t config = configs[0];
  WiFi.begin(myssid, mypassword);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print('.');
    delay(1000);
  }
  Serial.println("\nconnected");

  // Connect to serveur
  IPAddress remoteHost(192, 168, 1, 101);
  WiFiClient socket;
  socket.setNoDelay(true);
  Serial.printf("Connecting to server %s:%d\n", remoteHost, 4040);
  while (!socket.connect(remoteHost, 4040)) {
    Serial.print('.');
    delay(1000);
  }
  Serial.printf("\nConnected !\n");

  // Get a bunch of messages
  while (socket.connected()) {
    while (socket.available() > 0) {
      uint8_t c;
      socket.read(&c, 1);
      socket.flush();
      Serial.printf("%d\treceived at %d\n", c, millis());
    }
  }
}

Python server code:

import socket
import time;

if __name__ == "__main__":
    with socket.create_server(("", 4040)) as socket_server:
        print("server created")
        socket_server.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
        s, address = socket_server.accept()
        s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
        print("client connected")
        for i in range(10):
            s.sendall(i.to_bytes(length=1, byteorder='big', signed=False))
            print(time.time())
            time.sleep(.1)

The outputs of the python correspond to a message send every 100ms:

server created
client connected
1690365932.8049686
1690365932.9055307
1690365933.0061347
1690365933.1067114
1690365933.207289
1690365933.307892
1690365933.4085824
1690365933.5089648
1690365933.6095665
1690365933.710132

The output of the esp32:

.
connected
Connecting to server

Connected !
0       received at 5694
1       received at 5921
3       received at 6048
4       received at 6099
5       received at 6419
8       received at 6541
9       received at 6668

As you can see in the python output, the messages are send every 100ms with a 1ms variability while the esp is receiving with chaotic delays.

I did another experiment by implementing a python client instead of the esp. I ran the code on a different machine than the server. The python client receives the packets with a 100ms delay (as expected) with 1ms variability. So, the problem is definitely on the ESP + WiFi.h side.

Do you have any idea on what is happening and how to solve the issue ?

Does the 'simplified version' of your code have the delay problem?
Have you tested it? Do the results you show come from that code?

Use Wireshark to see at what intervals the packets left the server.

Also, was your Python to Python test using different machines only using wired ethernet as the physical transport? If so then repeat the test with a WiFi hop in the middle.

Wireless is a shared medium and therefore it is unlikely that you will ever see performance numbers equivalent to a wired connection.

Hi @mikb55 and thanks for your answers.

Second post:

  • Yes it was on 2 different wifi devices, 2 linux laptops. Still no delay

First post:

  • Yes the simplified version has the delay. the outpus correspond to the above codes.
  • I used wireshark and the is no delay in the transmission. Here is the subsample of the 3 first packets transmited:

Python server:

1690450216.5451157
1690450216.645424
1690450216.7461154

Wireshark output for these 3 packets:

173	7.502783120	192.168.1.101	192.168.1.88	TCP	55	4040 → 62361 [PSH, ACK] Seq=1 Ack=1 Win=64240 Len=1
174	7.603097794	192.168.1.101	192.168.1.88	TCP	55	4040 → 62361 [PSH, ACK] Seq=2 Ack=1 Win=64240 Len=1
176	7.703781798	192.168.1.101	192.168.1.88	TCP	55	4040 → 62361 [PSH, ACK] Seq=3 Ack=1 Win=64240 Len=1

ESP output:

0       received at 56575
1       received at 56808
2       received at 56812

100ms between packets in python as expected. This delay is confirmed by wireshark. But still chaotic delays in the ESP.

Hi, did you ever resolve this please? Seeing the same issue in my project.

Nop. Apparently it is a problem inside of the wifi lib (flag not set even if you ask for it).
I moved from arduino to esp idf (espressif). It is more complicated but way more expressive than basic arduino + libs.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.