How to send data via wifi faster?

detectivet
Posts: 2
Joined: Thu Jan 02, 2020 1:40 pm

How to send data via wifi faster?

Postby detectivet » Sat May 30, 2020 3:17 pm

I'm trying to use ESP32 to receive audio via an I2S ADC, and send the data through wifi to a computer in real time.
Here is the relevant part of my code:

Code: Select all

void reader(void *pvParameters) {
  uint64_t read_sum;
  String for_send;
  // The 4 high bits are the channel, and the data is inverted
  uint16_t offset = (int)ADC_INPUT * 0x1000 + 0xFFF;
  size_t bytes_read;
  while (1) {
    uint16_t buffer[2] = {0};
    i2s_read(I2S_NUM_0, &buffer, sizeof(buffer), &bytes_read, 15);
    //Serial.printf("%d  %d  %d\n", offset - buffer[0], offset - buffer[1],offset - buffer[2]);
    if (bytes_read == sizeof(buffer)) {
      read_sum = offset - buffer[0];
      if (is_client == 1) {
        for_send = int64String(read_sum);
        webSocket.sendTXT(0, for_send);
      }
    } else {
      Serial.println("buffer empty");
    }
  }
}

void setup() {
  Serial.begin(115200);
  // Initialize the I2S peripheral
  i2sInit();
  // Create a task that will read the data
  xTaskCreatePinnedToCore(reader, "ADC_reader", 2048, NULL, 1, NULL, 0);

  // Print our IP address
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }

  // Print ESP32 Local IP Address
  Serial.println(WiFi.localIP());

  // Start WebSocket server and assign callback
  webSocket.begin();
  webSocket.onEvent(onWebSocketEvent);
}

void loop() {
  // Look for and handle WebSocket data
  webSocket.loop();
}
The approach i'm currently using is to send data via websocket. The main loop and websocket loop are running on core 1, and I2S ADC reader is running on core 0, the code works alright, except the websocket is send data very slow. ADC sampling rate is set to be 44100, I tested it by setting up a counter variable in the i2s_read loop, the counting number is a little below 44100, but very close. But on the computer receiving side, the data received per second is only about 6000. And the missed data is not lagging behind, they are just missed, I guess new data overwrites the old ones in webSocket.sendTXT() queue, and sending speed is depending on the speed of webSocket.loop().

Is websocket the right protocol to use to send data very fast via wifi? The audio data is generating at 44100 samples * 12 bits per sample / 8 = 66150 bytes per second, that means the uploading speed must be higher than 66.150kb/s. I can accept some missing packages and latency if buffer is necessary, but what approach should I use on this sending data to computer part? Thanks

ESP_Sprite
Posts: 9730
Joined: Thu Nov 26, 2015 4:08 am

Re: How to send data via wifi faster?

Postby ESP_Sprite » Sun May 31, 2020 9:20 am

Don't read and send only 2 samples at the same time. Each time you call i2s_read/websocket.send, there is a fixed amount of overhead that needs to be done regardless of how much data you read or send. If you only read and send 2 samples at a time, that adds up very quickly.

Who is online

Users browsing this forum: No registered users and 32 guests