/* caso 1 - retorna todo o payload*/
/*
#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const String endpoint = "http://api.openweathermap.org/data/2.5/weather?q=Brasilia,br&APPID=";
const String key = "a264e51ca27956a736b29ce2b24e756c";
void setup() {
 
  Serial.begin(115200);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());
  Serial.println("Connected to the WiFi network");
  Serial.println("Hello, ESP32!");
}
void loop() {
 
  if ((WiFi.status() == WL_CONNECTED)) { //Check the current connection status
 
    HTTPClient http;
 
    http.begin(endpoint + key); //Specify the URL
    int httpCode = http.GET();  //Make the request
 
    if (httpCode > 0) { //Check for the returning code
 
        String payload = http.getString();
        Serial.println(httpCode);
        Serial.println(payload);
      }
 
    else {
      Serial.println("Error on HTTP request");
    }
 
    http.end(); //Free the resources
  }
 
  delay(30000);
 
}
*/
/*caso 2 fraciona o payload*/
#include <WiFi.h>
#include <HTTPClient.h>
#include <Arduino_JSON.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
String my_Api_Key = "a264e51ca27956a736b29ce2b24e756c";
String my_city = "Brasilia"; //specify your city
String my_country_code = "br"; //specify your country code
//unsigned long last_time = 0;
//unsigned long timer_delay = 10000;
String json_array;
void setup() {
  Serial.begin(115200);
  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("O primeiro conjunto de leituras aparecerá após 10 segundos");
}
int n=0;
void loop() {
  if(n<1){
  //if ((millis() – last_time) > timer_delay) {
  delay(10000);
    if(WiFi.status()== WL_CONNECTED){
      String server = "http://api.openweathermap.org/data/2.5/weather?q=" + my_city + "," + my_country_code + "&APPID=" + my_Api_Key;
      
      json_array = GET_Request(server.c_str());
      //Serial.println(json_array);
      JSONVar my_obj = JSON.parse(json_array);
  
      if (JSON.typeof(my_obj) == "undefined") {
        Serial.println("Parsing input failed!");
        return;
      }
    
      //Serial.print("JSON object = ");
      //Serial.println(my_obj);
      Serial.print("Temperatura: ");
      int temp=my_obj["main"]["temp"];
      Serial.print(temp - 273.15);
      Serial.println(" ºC");
      Serial.print("Pressão: ");
      Serial.print(my_obj["main"]["pressure"]);
      Serial.println(" hPa ");
      Serial.print("Umidade: ");
      Serial.print(my_obj["main"]["humidity"]);
      Serial.println(" % ");
      Serial.print("Velocidade do vento: ");
      Serial.print(my_obj["wind"]["speed"]);
      Serial.println(" Km/h: ");
    }
    else {
      Serial.println("WiFi Disconnected");
    }
   // last_time = millis();
  //}
  n++;
  }
}
String GET_Request(const char* server) {
  HTTPClient http;    
  http.begin(server);
  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);
  }
  http.end();
  return payload;
}