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.
I've found some solution here: ESP8266 - Response from server gets cut
Using read() instead of readString() and putting delay(1) seem to work at first, but when I was saving the read character to an array instead of printing it, the response was getting cut again. However when I deleted the delay(1), everything stared working, not sure why. This is the final 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";
char responseBuff[18000];
int fetchWeatherJson(struct weatherDataPacketStruct *wdps) {
client.flush();
if (!isConnected())
return -1;
Serial.println("FETCHING weather");
if (!client.connect(weatherHost, 80)) {
Serial.println("connection failed");
return -1;
}
client.setTimeout(15000);
client.print(String("GET ") + weatherUrl + " HTTP/1.1\r\n" +
"Host: " + weatherHost + "\r\n" +
"Connection: close\r\n\r\n");
while (client.connected()) {
String line = client.readStringUntil('\n');
Serial.println(line);
if (line == "\r") {
Serial.println("headers received");
break;
}
}
long int time = millis();
long int wait = 1000 * 10;
int counter = 0;
while ((time + wait) > millis()) {
while (client.available()) {
char c = client.read();
responseBuff[counter] = c;
counter++;
Serial.print(c);
if (c == '\0')
continue;
}
}
Serial.println(counter);
return parseWeatherJson(responseBuff, wdps);
}