Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By lajolo
#26381 Hello,

in order to see the WiFiClient example running correctly, I had to change:

// Read all the lines of the reply from server and print them to Serial
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}

into this:

// Read all the lines of the reply from server and print them to Serial
while(client.connected()){
String line = client.readStringUntil('\r');
Serial.print(line);
}

Could you please tell me if this is correct?

Thanks,
Marcello
User avatar
By kolban
#26382 Howdy Marcello,
I think your question hinges on the concept of client.available() vs client.connected().

I think client.available() returns the number of bytes that are available to be read right away. i.e. how many bytes are available to be read without having to wait for new ones to arrive.

The client.connected() returns true if the network connection between the two TCP partners is still good.

By looking at these functions in this light, we see that they perform dramatically different tasks. If your goal is to continue receiving data from the client until it disconnects, then it is likely you will want to use client.connected().

Neil
User avatar
By lajolo
#26383 Hello,

yes, you interpreted correctly my question.

The WiFiClient was not working with client.available() since I was receiving immediately:
closing connection.

I believe that the example should use client.connected() and I would be interested to know if others also had a similar experience with this example.

Thanks,
Marcello
User avatar
By martinayotte
#26390 The WifiClient example is working fine. As Kolban said, connected() only returns the state of the connection while the available() returns the amount of character already received. Make sure also to have a delay() between the request send and the response loop, which is present in the example, otherwise available() will returns 0 since the response has not yet been received.

Maybe you should provide us your sources to figure out what is wrong.