[ESP32 forecast] Fail to get object

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 :slight_smile:

What does come out of the sketch? Which error do you get?

That is what i got, just object outside the square brackets could be shown :frowning:

Connecting to WIFI...
.
IP Address: 192.168.1.27
First set of readings will appear after 10 seconds
HTTP Response code: 200
{"city":{"id":1566083,"name":"Ho Chi Minh City","coord":{"lon":106.6602,"lat":10.7626},"country":"VN","population":1000000,"timezone":25200},"cod":"200","message":3.7255741,"cnt":1,"list":[{"dt":1656734400,"sunrise":1656714894,"sunset":1656760765,"temp":{"day":302.99,"min":297.6,"max":304.02,"night":299.8,"eve":301.52,"morn":297.71},"feels_like":{"day":307.57,"night":299.8,"eve":305.18,"morn":298.7},"pressure":1006,"humidity":69,"weather":[{"id":501,"main":"Rain","description":"moderate rain","icon":"10d"}],"speed":6.66,"deg":240,"gust":11.06,"clouds":99,"pop":1,"rain":10.71}]}
Location: "Ho Chi Minh City"
COD: "200"
Temperature: null
Humidity: null
Wind Speed: null

I found the solution is :

Serial.print("Location: ");
      Serial.println(myObject["city"]["name"]);
      Serial.print("Temperature: ");
      Serial.println(myObject["list"][0]["temp"]["day"]);
      Serial.print("Humidity: ");
      Serial.println(myObject["list"][0]["humidity"]);
      Serial.print("Wind Speed: ");
      Serial.println(myObject["list"][0]["speed"]);

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.