I want to get forecasts from OpenWeatherMap.org. I'm able to make a request ang get the JSON file, and parsing it with ArduinoJson.h library. The JSON have four forecast iterations and I wish to save those four iterations values in arrays.
I've no prpblems to get int, float and long values from the JSON and saving them to arrays. But when it gets to some text data (const char*), I can't save them to a string array. I got conflict between the strings array declaration and the assingning the const char* data to this array while compiling, or if not the ESP8266 mcu reset in loop...
Here are the part of the code causing conflict. If I withdraw all things related to "dt_text", everything for just fine. I have omitted in the following code the setup() and loop() loops, functions are called in those loops...
My goal is to print the forecasts data and foreacasts time (dt_text) later in the sketch, on an lcd or on a serial...
Any help on how to declare/convert/assign those JSON dt_text values to the dt_text[] array without causing conflict ?
Strings are something I haven't work with a lot yet...
I'm using Wemos D1 R2 mini, Arduino IDE 2.0.4.
Thanks a lot !
/////////////////////////////////////////////
//OpenWeatherMap.org setup
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <ArduinoJson.h>
const char* openWeatherMapApiKey = "your key";
const char* city = "your town";
const char* countryCode = "your country";
const char* units = "metric"; // "metric" or "imperial"
const char* language = "fr"; // description language "en" for English, "fr" for French
unsigned long weatherReadingLastTime = 0;
unsigned long weatherReadingDelay = 600000; // Timer set to 1 minutes (600000)
//END of OpenWeatherMap.org setup
/////////////////////////////////////////////
long dt[5];
char dt_text[5][20] ;
float temperatures[5];
float feels_like[5];
int humidities[5];
int pressures[5];
float wind_speeds[5];
int weather_ids[5];
void weatherReading() {
// Check WiFi connection status
if (WiFi.status() == WL_CONNECTED) {
getNowWeather();
getForecastWeather();
for (int i = 0; i < 5; i++) {
Serial.print("Time of forecast: ");
Serial.println(dt_text[i]);
Serial.print("Weather: ");
Serial.println(weather_ids[i]);
Serial.print("Temperature: ");
Serial.println(temperatures[i]);
Serial.print("Feel like: ");
Serial.println(feels_like[i]);
Serial.print("Pressure: ");
Serial.println(pressures[i]);
Serial.print("Humidity: ");
Serial.println(humidities[i]);
Serial.print("Wind Speed: ");
Serial.println(wind_speeds[i]);
Serial.println();
}
} else {
Serial.println("WiFi Disconnected");
}
}
void getNowWeather() {
String serverPath = "http://api.openweathermap.org/data/2.5/weather?q=" + String(city) + "," + String(countryCode) + "&APPID=" + String(openWeatherMapApiKey) + "&units=" + String(units) + "&lang=" + String(language);
// Parse the JSON response
String jsonBuffer = httpGETRequest(serverPath.c_str());
Serial.println(jsonBuffer);
DynamicJsonDocument doc(4096);
DeserializationError error = deserializeJson(doc, jsonBuffer);
if (error) {
Serial.print(F("Weather NOW deserializeJson() failed: "));
Serial.println(error.f_str());
return;
}
JsonObject weather_0 = doc["weather"][0];
weather_ids[0] = weather_0["id"]; // "nuageux"
JsonObject main = doc["main"];
dt[0] = doc["dt"];
strcpy("NOW ", dt_text[0]);
temperatures[0] = main["temp"];
feels_like[0] = main["feels_like"]; // 0.24
humidities[0] = main["humidity"]; // 87
pressures[0] = main["pressure"]; // 1009
JsonObject wind = doc["wind"];
wind_speeds[0] = wind["speed"]; // 0.41
}
void getForecastWeather() {
// Getting forcast for cnt events
const char* cnt = "4"; // number of forecasts
String serverPath = "http://api.openweathermap.org/data/2.5/forecast?q=" + String(city) + "," + String(countryCode) + "&cnt=" + String(cnt) + "&APPID=" + String(openWeatherMapApiKey) + "&units=" + String(units) + "&lang=" + String(language);
// Send an HTTP GET request
String jsonBuffer = httpGETRequest(serverPath.c_str());
Serial.println(jsonBuffer);
//Parse the JSON response
DynamicJsonDocument doc(4096);
DeserializationError error = deserializeJson(doc, jsonBuffer);
if (error) {
Serial.print(F("Weather FORECAST deserializeJson() failed: "));
Serial.println(error.f_str());
return;
}
JsonArray list = doc["list"];
for (int i = 0; i < 4; i++) {
JsonObject item = list[i];
JsonObject main = item["main"];
JsonObject weather = item["weather"];
JsonObject wind = item["wind"];
dt[i + 1] = item["dt"];
const char* dt_txt = item["dt_text"];
strcpy(dt_text[i + 1], dt_txt);
temperatures[i + 1] = main["temp"];
feels_like[i + 1] = main["feels_like"];
humidities[i + 1] = main["humidity"];
pressures[i + 1] = main["pressure"];
weather_ids[i + 1] = weather["id"];
wind_speeds[i + 1] = wind["speed"];
}
}
String httpGETRequest(const char* serverName) {
WiFiClient client;
HTTPClient http;
// Your IP address with path or Domain name with URL path
http.begin(client, serverName);
// Send HTTP POST request
int httpResponseCode = http.GET();
String payload = "{}";
if (httpResponseCode > 0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
payload = http.getString();
} else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
return payload;
}