Problem with WiFi-connected project

Hi,

I have a project that involves WiFi communication between a server running on my computer and the Arduino UNO + Arduino WiFi shield.

I basically have an interface running on my computer where I can set the value of a LED which is connected to the Arduino (On or Off). Then, the WiFi shield constantly connects to the server running on my computer, and makes a HTTP GET request to get the value of the LED, and then turns the LED on or off accordingly.

It all works fine, but there is a huge lag between the moment I press the button and the moment the LED changes its state. It has to be the code I am using, because the ping of the board is around 15 ms and I have a lag of 2-3 seconds. I looked into the details, and it seems the Arduino takes a long time to receive the answer from the computer. Here is the Arduino code in question (the part about sending the GET request):

String send_request (String request) {
  
    // Start connection
    Serial.println("Starting connection to server...");
    if (client.connect(server_ip, port)) {
     
     Serial.println("Connected to server");
      
     // Send request
     Serial.println(request);
     client.println(request);
     client.println();
    }
    
    // Store result
    String result;
     while(client.connected()) {
       while (client.available()) {
        char c = client.read();
        //Serial.write(c);
        result = result + c;
    }
    }
    
    // Disconnect
    Serial.println("Disconnecting from server.");
    client.stop();
  
    return result;
}

The request itself is something like:

"GET /server.php?device="+sensor_id+"&phase=command HTTP/1.0"

On the server side, a PHP script just answer with echo $value_of_the_led;

So my question is, is there a way to speed things up ? Is my approach wrong/inefficient ?

Thanks !

My guess is that the slow part is the “while(client.connected()) {”. Perhaps you should look for the end of the data and disconnect the client rather than wait for the server side to disconnect?