I want my NodeMCU V3 to fetch weather data from OpenWeatherMapApi. I found some sample codes how to send GET request, but the response is getting cut randomly. After reading response, I print the length of the received JSON response. The response should have something about 16k characters, but it's random every request. Sometimes it's 16k as it should be, sometimes 11k, sometimes 13k etc. This is my code:
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
WiFiClient client;
const char *url = "/data/2.5/onecall?lat=51.039117&lon=21.072800&appid=xxx&lang=pl&units=metric&exclude=minutely";
const char *host = "api.openweathermap.org";
int fetchWeatherJson(struct weatherDataPacketStruct *wdps) {
if (!isConnected())
return -1;
Serial.println("Fetching weather");
if (!client.connect(host, 80)) {
Serial.println("Connection failed");
return -1;
}
client.setTimeout(15000);
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
Serial.println("headers received");
break;
}
}
Serial.println("reply was:");
Serial.println("==========");
String line;
while (client.available()) {
line = client.readStringUntil('\n'); //Read Line by Line
// Serial.println(line); //Print response
}
Serial.println(line.length());
parseWeatherJson(line.c_str(), wdps);
return 1;
}
Is there something wrong? Thank you.