How to send data via wifi faster?
Posted: 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:
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
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();
}
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