I have the attached script that reads data from a socket server (it is PHP at the server end).

My problem (I think) is that the readStringUntil() needs a test above it, so that it "reads up to \r" only if there is any data being received, otherwise it seems to keep trying to read forever, because when I DO send data, it is received with 1-2 seconds delay.

If I constantly send (properly \r delimited) data without any pause, then it seems to be received instantly (10-20ms or so).

I tried: if (client.available()) { String received_message = client.readStringUntil('\r'); } but it doesn't seem to be the right function, doesn't do anything.

This is ESP32 board, compiler mentioned it found two wifi libraries, one in arduino folder and one in ESP32, it used the second one. This might be relevant to what functions are available.

#include <WiFi.h>

const char* ssid = "MyWiFi";
const char* password =  "1234";

const uint16_t port = 8080;
const char * host = "myserver.example.com";

void setup() {

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {

  Serial.print("wifi connected with IP: ");

void loop() {

  WiFiClient client;

  if (!client.connect(host, port)) {
    Serial.println("connection to host failed");

  Serial.println("connected to server");

  // I assume this is where I need a test like "if any received data exists" >>>

  String received_message = client.readStringUntil('\r');

  // intentional delay doesn't seem to do much 
  // when it constantly tries to read for data that is not always there

  // do I need to disconnect here ?!
  // client.stop();

EDIT: my PHP loop looks like this:

    $accept = socket_accept($socket) or die("Could not accept incomming connection");

    // $message_to_send = 'hi there '.microtime(true)."\r";
    // OR 
    // get message from memcached variables
        echo "\nsending: ".$message_to_send;
        socket_send($accept, $message_to_send, strlen($message_to_send), MSG_OOB);
} while (true);
  • esp WiFi libraries close the connection when the last WiFiClient object which referenced it is destroyed. so here at the end of the loop().
    – Juraj
    Commented Jul 31, 2020 at 16:49
  • does the server send data immediately after you connect to it? you send no request
    – Juraj
    Commented Jul 31, 2020 at 16:51
  • 1
    client.readStringUntil() waits a second for the data. if the server sends the data immediately after the connection from client is established, then the readStringUntil would catch it. if it sends the data later, the connection is already closed. if (!connected) connect
    – Juraj
    Commented Jul 31, 2020 at 17:46
  • 2
    then you need project guidance and not an answer on a Q&A site. forum.arduino.cc
    – Juraj
    Commented Jul 31, 2020 at 18:17
  • 1
    I posted a topic exactly there under project guidance yesterday, nobody replied :/
    – adrianTNT
    Commented Jul 31, 2020 at 19:40


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.