Hope you are having a good day,
I am trying to maximise data rate over TCP from esp32 client to server on PC.
When I increase the packet size (by changing numOfTs from 32 to 64), the server receives the data slower.
Here is my code:
#include <WiFi.h>
/* WiFi network name and password */
const char * ssid = "WiFi Guest_";
const char * pwd = "";
/* Ip address of server (PC) and udp port - same as python script */
const char * hostIP = "192.168.10.57";
const int port = 1234;
unsigned short* buff;
// number of adc channels and number of timesteps sent with one packet
int numOfCh = 8;
int numOfTs = 64;
int adcSample1 = 0b1111111111111110;
WiFiClient client;
void setup()
{
Serial.begin(115200);
Serial.print("Connecting to ");
Serial.println(ssid);
/* connect to your WiFi */
WiFi.begin(ssid, pwd);
/* wait until ESP32 connect to WiFi*/
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected with IP address: ");
Serial.println(WiFi.localIP());
buff = reinterpret_cast<unsigned short*>(malloc((numOfCh+2) * (numOfTs) * sizeof(short)));
}
void loop()
{
for(int ts = 0; ts<numOfTs; ts++){
for(int ch = 0; ch<(numOfCh); ch++){
if(ch == 0){
*(buff+(ts*(numOfCh+2))) = adcSample1; // 0b10101010101010101010101010101010;
*(long*)(buff+(8 + (ts*(numOfCh+2)))) = 0b10101010101010101010101010101010; // 0b11111111111111111111111111111110;
}
else{
*(buff+(ch + (ts*(numOfCh+2)))) = adcSample1;
}
}
}
sendTCP(buff);
}
void sendTCP(unsigned short *packet){
while (!client.connect(hostIP, port)){
client.connect(hostIP, port);
}
client.write((uint8_t*)packet, (sizeof(short) * (numOfCh+2) * (numOfTs)));
}
Am I using sendTCP() correctly? Is there a better way of using WiFiClient? Do I need to run client.connect() each time I send a packet? Using client.connected() ocassionally gives a rogue packet of a very large length.
Thanks in advance for your suggestions,
Will