Hi , i am making a simple weather station with the forecast for tomorrow. I am stucking with get temp info from openweather , it just show "null" . i think i extracted wrong format for JSON object inside square brackets , but after 2 days research , i could not find a solution
my raw open weather here:
{"city":{"id":1566083,"name":"Ho Chi Minh City","coord":{"lon":106.6602,"lat":10.7626},"country":"VN","population":1000000,"timezone":25200},"cod":"200","message":24.7380754,"cnt":1,"list":[{"dt":1656648000,"sunrise":1656628480,"sunset":1656674358,"temp":{"day":304.48,"min":298.21,"max":304.88,"night":299.4,"eve":301.07,"morn":298.53},"feels_like":{"day":310.11,"night":299.4,"eve":305.51,"morn":299.5},"pressure":1005,"humidity":65,"weather":[{"id":501,"main":"Rain","description":"moderate rain","icon":"10d"}],"speed":4.57,"deg":233,"gust":11.62,"clouds":100,"pop":1,"rain":16.48}]}
And my code
#include <WiFi.h>
#include <HTTPClient.h>
#include <Arduino_JSON.h>
const char* ssid = "CandyLan";
const char* password = "66666668";
String jsonBuffer;
void setup(void)
{
Serial.begin(9600);
WiFi.begin(ssid, password);
Serial.println("Connecting to WIFI...");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
Serial.println("First set of readings will appear after 10 seconds");
}
void loop() {
if(WiFi.status()== WL_CONNECTED){
String server = "http://api.openweathermap.org/data/2.5/forecast/daily?lat=10.762622&lon=106.660172&cnt=1&appid=c8e3f476ce7e5445145807b7b98d38ba";
jsonBuffer = httpGETRequest(server.c_str());
Serial.println(jsonBuffer);
JSONVar myObject = JSON.parse(jsonBuffer);
// JSON.typeof(jsonVar) can be used to get the type of the var
if (JSON.typeof(myObject) == "undefined") {
Serial.println("Parsing input failed!");
return;
}
Serial.print("Location: ");
Serial.println(myObject["city"]["name"]);
Serial.print("COD: ");
Serial.println(myObject["cod"]);
Serial.print("Temperature: ");
Serial.println(myObject["list"]["temp"]["day"]);
Serial.print("Humidity: ");
Serial.println(myObject["list"]["humidity"]);
Serial.print("Wind Speed: ");
Serial.println(myObject["list"]["speed"]);
}
else {
Serial.println("WiFi Disconnected");
}
delay (10000);
}
String httpGETRequest(const char* serverName) {
WiFiClient client;
HTTPClient http;
// Your Domain name with URL path or IP address with 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;
}
Blowing up my mind now , really thanks for you help ![]()