[ESP8266] Wemos D1 Mini WiFiClient no response data

Hello everybody,

I'm having some issues with a Wemos D1 Mini using WiFiClient. I connect to a REST API with it and need to read the response, but no matter how I try it, I do not get any response data - no body, no headers, nothing.

I'm quite new to the ESP8266 and have not done any Microcontroller programming for years, so please forgive any stupid mistakes :wink:

Here's my code:

#include <ESP8266WiFi.h>

// ... SNIP ...
WiFiClient makeClient(String server, int port) {
  WiFiClient client;
  if(!client.connect(server, port)) {
    return client;
  }
  return client;
}

void fetch() {
  WiFiClient client = makeClient("192.168.178.49", 8899);
  if(client == NULL) {
    return;
  }
  client.print("GET /api/v1/sleep_data/status HTTP/1.1\r\nHost: 192.168.178.49:8899\r\nConnection: close\r\n\r\n");
  Serial.println("Sent request");
  Serial.println("Reading from client...");
  Serial.println("------------------");
  while(client.available()) {
    Serial.write(client.read());
  }
  Serial.println("++++++++++++++++++");
  client.stop();
}

All I get on the Serial monitor is:

Getting update
Sent request
Reading from client...
------------------
++++++++++++++++++

I checked the API several times: it works fine and responds with a body (which is a JSON String). The requests I send from the Wemos D1 Mini get through to the server just fine and even trigger a response, but it seems to be ignored by the board.

I also tried

Serial.println("--------");
while (client.available()) { 
  String line = client.readStringUntil('\r');
  Serial.print(line);
}
Serial.println("--------");

to read the response, but that results in the same thing.

To get the ESP libraries into my Arduino IDE, I used this URL: https://arduino.esp8266.com/stable/package_esp8266com_index.json

I installed esp8266 Vesion 2.7.4

Oh, I tried using HttpClient as most pages I found through Google seem to suggest, but I can't seem to use it, as the library is never found (even after installing it through the IDE's manager).

Any help is highly appreciated :slight_smile: Whether it's using the WiFiClient or the HttpClient does not matter to me, as long as it works (using the HttpClient would require a way to successfully use it, though).

Thanks a lot - steps

you have to wait for the data

Juraj:
you have to wait for the data

Hey, thanks for your reply. I'll update my code and see whether or not this works. I thought the available() call was enough for this kind of thing, but maybe I was mistaken.

Alright, I did find the time sooner than I thought :slight_smile:

Here's the waiting loop I added which works fine:

unsigned long timeout = millis();
while (client.available() == 0) {
  if (millis() - timeout > 5000) { 
    Serial.println(">>> Client Timeout !");
    client.stop(); 
    return; 
  }
}

This solves my problem, maybe it can show others how to do it as well.

Thanks a ton!

Steps:
Alright, I did find the time sooner than I thought :slight_smile:

Here's the waiting loop I added which works fine:

unsigned long timeout = millis();

while (client.available() == 0) {
  if (millis() - timeout > 5000) {
    Serial.println(">>> Client Timeout !");
    client.stop();
    return;
  }
}




This solves my problem, maybe it can show others how to do it as well.

Thanks a ton!

Client has some function which can wait for the data. this is inherited from the base class Stream used for Serial too.
the timeout is set with client.setTimeout().
the functions which wait for data are
readBytes
readByttesUntil
readString
readStringUntil
parseInt
parseFloat

Juraj:
Client has some function which can wait for the data. this is inherited from the base class Stream used for Serial too.
the timeout is set with client.setTimeout().
the functions which wait for data are
readBytes
readByttesUntil
readString
readStringUntil
parseInt
parseFloat

Stream - Arduino Reference

Thanks for your reply - but could you tell me how that actually works? I tried it and got the same results as when I tried my original code. How does it work in conjunction with

client.available()

Thanks :slight_smile:

it depends on what do you want to do with the data and if you want to use String or char array.
for example to read the http response headers line by line you could use something like

char buff[128];
while (true) {
 int l = client.readBytesUntil('\n', buff, sizeof(buff));
 if (l == 1) // last line of headers only contains "\r\n". \n was consumed by readBytesUntil
   break;
 buff[l-1] = 0; // remove \r and set the terminating zero
 Serial.println(buff);
}

Thanks! That's a nice example :slight_smile:

Leaving out the available call seems to do the trick. Cleans up the code as well, thanks again!