#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
// WiFi credentials
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", 10800, 60000);
// 3rd param is timezone offset in seconds, adjust if needed
// 4th param is update time in millis
// Define LCD with I2C address 0x27 and size 20x4
LiquidCrystal_I2C lcd(0x27, 20, 4);
String weekDays[7]={"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
// Initialize the LCD
lcd.init();
lcd.backlight();
// Connect to Wi-Fi
//WiFi.begin(ssid, password);
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
// Initialize NTP Client
timeClient.begin();
}
void loop() {
// Update the NTP client
timeClient.update();
//Serial.println(timeClient.getFormattedTime());
// Get the current time
unsigned long epochTime = timeClient.getEpochTime();
struct tm *ptm = gmtime ((time_t *)&epochTime);
// Format the time
int hour = ptm->tm_hour;
int minute = ptm->tm_min;
int second = ptm->tm_sec;
// Display the time on the LCD
lcd.setCursor(0, 0);
lcd.print("Time: ");
if(hour < 10) lcd.print('0');
lcd.print(hour);
lcd.print(":");
if(minute < 10) lcd.print('0');
lcd.print(minute);
lcd.print(":");
if(second < 10) lcd.print('0');
lcd.print(second);
// Display the date on the LCD
int day = ptm->tm_mday;
int month = ptm->tm_mon + 1;
int dayWeek = ptm->tm_wday;
//int year = ptm->tm_year + 1900;
lcd.setCursor(0, 1);
lcd.print("Date: ");
if(day < 10) lcd.print('0');
lcd.print(day);
lcd.print("/");
if(month < 10) lcd.print('0');
lcd.print(month);
lcd.print(" ");
lcd.print(weekDays[dayWeek]);
delay(1000); // Update every second
}