#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>

const char* ssid = "Wokwi-GUEST";
const char* password = "";

void setup() {
  Serial.begin(115200);

  // اتصال به شبکه Wi-Fi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connection to WiFi...");
  }
  Serial.println("Connected to WiFi");

  
}

void loop() {
  // کدی که در حلقه اصلی برنامه اجرا می‌شود
  // درخواست اطلاعات از وب سایت
  HTTPClient http;
  http.begin("https://api.openweathermap.org/data/2.5/weather?q=Tehran,ir&appid=366ca918ec362e1f41f7bbd0ff6e1671");
  int httpCode = http.GET();
  String response;
  if (httpCode > 0) {
    response = http.getString();
    Serial.println(response);   
    delay(1000);
  }

  http.end();

  StaticJsonDocument<1000> doc;
  DeserializationError error = deserializeJson(doc, response);

  if (error) {
    Serial.print("deserializeJson() failed: ");
    Serial.println(error.c_str());
    return;
  }

  float temperature = doc["main"]["temp"].as<float>();
  int humidity = doc["main"]["humidity"].as<int>();
  String weather_description = doc["weather"][0]["description"].as<String>();
  int pressure = doc["main"]["pressure"].as<int>();
  float speed = doc["wind"]["speed"].as<float>();
  String country = doc["sys"]["country"].as<String>();
  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.println(" K");

  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.println("%");

  Serial.print("pressure: ");
  Serial.print(pressure);
  Serial.println(" Hectopascal");

Serial.print("wind speed: ");
  Serial.print(speed);
  Serial.println(" Meter per second");

  Serial.print("country: ");
  Serial.println(country);
  
  Serial.print("Weather Description: ");
  Serial.println(weather_description);


delay(3000);
}