Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By spiff
#21733 Hi all,

I am trying to have the ESP push data to a web-server using a simple GET-request. If you look at the code below, you will probably see the similarity with the example.

What bothers me is the "delay(10)" and "while (client.available())". This does not seem to be the proper way to do it. I have seen occasionally that my data gets submitted, but I get no response, presumably because the reply from the web server was not ready within 10ms, hence client.available() was not true.

And what happens if there is a long reply, but it gets sent in bursts? Certainly "while (client.available())" will skip out as soon as there is an instant with nothing in the buffer. To me it looks like this primarily works due to the comparatively slow serial debug printing.

There must be a better way, like reading until connection is closed (perhaps with a time-out). Any suggestions?

Thanks in advance.

Code: Select all  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;

  if (!client.connect(host, httpPort)) {
    Serial1.println("connection failed");
    return;
  }
  Serial1.println("Connected");
 
  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");
  delay(10);
 
  // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial1.print(line);
    Serial1.print("\r");
  }
  client.stop();
  Serial1.println("Disconnected");