#include <WiFi.h>
#include <HTTPClient.h>
#include <LiquidCrystal_I2C.h>
#include<ArduinoJson.h>
HTTPClient http;
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27,16,2);
void setup() {
Serial.begin(9600);
Serial.print("Connecting to WiFi");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
lcd.init();
lcd.backlight();
}
void loop() {
if ((WiFi.status() == WL_CONNECTED)) {
HTTPClient http;
http.begin("https://api.openweathermap.org/data/2.5/weather?id=1562693&appid=dd6d81707a23916e8b80e738532f655e");
int httpCode = http.GET();
if (httpCode > 0) {
String payload = http.getString();
Serial.println(httpCode);
Serial.println(payload);
StaticJsonDocument<1024> doc; // Kích thước bộ nhớ cho JSON
DeserializationError error = deserializeJson(doc, payload);
if (error) {
Serial.print("JSON deserialization failed: ");
Serial.println(error.f_str());
return;
}
// Trích xuất thông tin thời tiết
float temperatureKelvin = doc["main"]["temp"]; // Nhiệt độ
float temperatureCelsius = temperatureKelvin - 273.15;
const char* weather = doc["weather"][0]["description"]; // Mô tả thời tiết
// Hiển thị thông tin lên LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperatureCelsius);
lcd.print(" C");
lcd.setCursor(0, 1);
lcd.print(weather);
Serial.print("Temperature: ");
Serial.println(temperatureCelsius);
Serial.print("Weather: ");
Serial.println(weather);
}
else {
Serial.println("Error on HTTP request");
}
http.end();
}
delay(10000);
}