#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define NTP_SERVER "pool.ntp.org"
#define UTC_OFFSET 3600
#define UTC_OFFSET_DST 3600
const char* apiKey = "26442cc5d88cc5516648ee2229e0f9e4";
const char* city = "Zilina";
String apiUrl = "http://api.openweathermap.org/data/2.5/weather?q=" + String(city) + "&appid=" + String(apiKey) + "&units=metric";
void setup()
{
Serial.begin(115200);
lcd.init();
lcd.backlight();
WiFi.begin("Wokwi-GUEST", "", 6);
Serial.print("Pripájanie na WiFi...");
while (WiFi.status() != WL_CONNECTED)
{
delay(250);
Serial.print(".");
}
Serial.println("\nWiFi pripojené!");
Serial.print("IP adresa: ");
Serial.println(WiFi.localIP());
configTime(UTC_OFFSET, UTC_OFFSET_DST, NTP_SERVER);
delay(100);
}
void loop()
{
if (WiFi.status() == WL_CONNECTED)
{
HTTPClient http;
http.begin(apiUrl.c_str());
int httpResponseCode = http.GET();
if (httpResponseCode > 0)
{
String pocasie = http.getString();
Serial.println(pocasie);
// Parse JSON response
StaticJsonDocument<1024> doc;
DeserializationError error = deserializeJson(doc, pocasie);
if (!error)
{
float temperature = doc["main"]["temp"];
Serial.print("Teplota: ");
Serial.println(temperature);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Teplota: ");
lcd.print(temperature);
lcd.print(" C");
}
else
{
Serial.println("Chyba pri parsovani JSON");
}
}
else
{
Serial.print("Problém so získaním údajov zo serveru: ");
Serial.println(httpResponseCode);
}
http.end();
}
else
{
Serial.println("WiFi nie je pripojená.");
}
delay(60000);
}