Speeding Up WiFi Client Prints on ESP32-S3

JoseeMallah
Posts: 1
Joined: Tue Jul 02, 2024 2:47 pm

Speeding Up WiFi Client Prints on ESP32-S3

Postby JoseeMallah » Tue Jul 02, 2024 2:53 pm

Hello,
I am using an esp32-s3 to do analog reads and send the data over WiFi to my laptop. When I read 4 sensors, I get a frequency > 1kHz, but with 6 sensors it drops below 1kHz.
What is slowing it down and how to fix it?
Thank you

Here is my esp32 code:

Code: Select all

#include <WiFi.h>

// Replace with your network credentials
const char* ssid = "AndroidAP";
const char* password = "kesy1320";

WiFiServer server(80);

unsigned long start = 0;

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }

  Serial.println("Connected to WiFi");
  
  // Print the IP address
  Serial.print("ESP32 IP Address: ");
  Serial.println(WiFi.localIP());
  
  server.begin();
}

void loop() {
  WiFiClient client = server.available();  // Listen for incoming clients

  if (client) {
    while (client.connected()) {
      // Send data at 1 kHz (1000 times per second)
      start = micros();
      
      client.println((String) start + " " + analogRead(14) + " " + analogRead(13) + " " + analogRead(12) + " " + analogRead(11) + " " + analogRead(10) + " " + analogRead(9));
      
      /*while (micros() - start < 625) {
        // wait for the next 1 ms
      }*/
    }
    
    client.stop();
    Serial.println("Client disconnected");
  }
}
And here is the python code running on PC:

Code: Select all

import socket
import time

# Replace with your ESP32's IP address
ESP32_IP = '192.168.225.242'
ESP32_PORT = 80

def main():
    # Create a socket object
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    
    # Connect to the ESP32 server
    client_socket.connect((ESP32_IP, ESP32_PORT))
    print("Connected to ESP32")

    try:
        with open("received_data.log", "w") as log_file:
            while True:
                data = client_socket.recv(1024).decode()  # Receive data from ESP32
                if data:
                    log_file.write(f"{data.strip()}\n")
                    log_file.flush()  # Ensure data is written to the file immediately
    except KeyboardInterrupt:
        print("Interrupted")
    except Exception as e:
        print(f"Error: {e}")
    finally:
        client_socket.close()
        print("Connection closed")

if __name__ == "__main__":
    main()

lbernstone
Posts: 789
Joined: Mon Jul 22, 2019 3:20 pm

Re: Speeding Up WiFi Client Prints on ESP32-S3

Postby lbernstone » Wed Jul 03, 2024 7:35 pm

The adc reads are very fast (I show six completed in 150 micros). Most of your device time is going to be spent in network transfers. I set up a quick test with TCP vs UDP connections (and used netcat as a receiver). When I send over udp, the packets arrive in a very consistent 550us frequency, with the occasional bump up to 1000us. When I send over tcp, the packets are typically 550us apart, but the outliers are as long as 500000us gap, as TCPIP does it's verifications. There's trade offs there, but if you can afford to lose a packet here and there, datagrams will give you much better performance. Sending in binary (ie, less data), and combining data into a larger buffer, which will more accurately match the network MTU, would also improve your transfer rates (although the network driver will do some of that automatically).

Who is online

Users browsing this forum: No registered users and 93 guests