I’m looking about how to setup a TCP server listening in a port, like 9876 in order to receive strings from an external client.
I do this with the following code i the loop()
WiFiClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
String data_received = client.readStringUntil(‘\r\n’);
Serial.println("Data received " + data_received);
}
}
}
The problem is this code block all the program and i can not do any other task. How can i have this running and continue to execute rest of code in the loop function?
Thanks very much for your response. I have looking in some solutions about async without success and that is the reason of this post.
I have downloaded the example and the compilation is ok. Upload to device is also ok. I start the serial monitoring, reboot the device, but nothing happens… i use a ESP32-2432S024C device.
When i reset, i receive the following on serial monitor and nothing more…:
The example opens an AccessPoint and waits for a WiFi client to connect to the ESP on port 8000.
As long as this does not happen, the code does not output anything on the serial interface. This is therefore not an error but the expected behavior of this example.
Thanks for your reply. Yes, tested and all is working.
But what i’m looking for is to open a port like in the example, but connecting to a WiFi. I do not need client to connect to the ESP AccessPoint. Client(s) and ESP must be connected to same WiFi.
Careful: Casting void* data to char* data, treating it like a C string, and using it to e.g. in a print function like Serial.println(data) will not work, because you are assuming data has a NULL byte at the end when it doesn’t have to (i.e., it’s not a C-string). You always have to see the pointer and the length as a pair and never read beyond its length. That’s why they explicitly use the print overload that takes a pointer and the length.