I have esp32 wroom 32 chip and w5500 ethernet module. I want to open socket for TCP transmission, to receive 5 bytes long configuration data. The problem is my code is working only 1 time.
Here is the server task which I am currently calling in endless arduino loop function, nothing else is called only this task:
Serv_Task([](serv_config_t *config) {
if (config->address != 0) {
Serial.println(config->power.ch1);
Serial.println(config->power.ch2);
Serial.println(config->power.ch3);
Serial.println(config->power.ch4);
} else {
//Icom_Send(config->address, config->power);
}
});
And here is the function definition:
void Serv_Task(serv_configReceived_t callback) {
// check for new client connecting
EthernetClient newClient = server.accept();
if (newClient) {
if (!client) {
newClient.println("Connected");
client = newClient;
} else {
newClient.println("Another client already connected");
newClient.stop();
}
}
#ifdef WORKING_CODE
while (client && client.available() > 0) {
// read incoming data from the client
Serial.write(client.read());
}
#else
// check for incoming data
if (client && client.available() >= sizeof(serv_config_t)) {
serv_config_t config = {0};
for (uint8_t i = 0; i < sizeof(serv_config_t); i++) {
((uint8_t *)&config)[i] = client.read();
}
// call callback with received data
callback(&config);
}
#endif
// stop client if disconnect
if (client && !client.connected()) {
// read out client buffer?
client.stop();
}
}
When I define macro WORKING_CODE everything is working as it should, I can connect, disconnect as many times as I want. When I send data they appear in serial console.
But the other part when WORKING_CODE is not defined is not working. Basically I just want to collect data from ethernet buffer as 5 bytes chunks. After connection is made I can send data only 1 time (I use putty).
This is putty console:
Connected
0123456789
0123456789
0123456789
And this is the serial output from esp:
49
50
51
52
54
55
56
57
No matter what I send it will not show in console. Only data from the first line will appear. And when I disconnect and connect again I see my "Another client already connected" message.
Can you help? What is going on?