#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
#define TFT_DC 2
#define TFT_CS 15
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org");
void drawClock(int hour, int minute, int second) {
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(0, 0);
int w = tft.width();
int h = tft.height();
for (int i = 0; i < 12; i++) {
float a = (i * 360/12) * DEG_TO_RAD;
int r = 90;
int x = w / 2 + r * sin(a);
int y = h / 2 + r * cos(a);
tft.drawLine(x, y, x, y, ILI9341_WHITE);
}
// Draw clock face
// tft.fillCircle(tft.width() / 2, tft.height() / 2, 100, ILI9341_WHITE);
tft.drawCircle(tft.width() / 2, tft.height() / 2, 100, ILI9341_WHITE);
// Draw hour hand
float hourAngle = (hour % 12 + minute / 60.0) * 30 * DEG_TO_RAD;
int hourX = tft.width() / 2 + 40 * cos(hourAngle);
int hourY = tft.height() / 2 + 40 * sin(hourAngle);
tft.drawLine(tft.width() / 2, tft.height() / 2, hourX, hourY, ILI9341_RED);
// Draw minute hand
float minuteAngle = (minute + second / 60.0) * 6 * DEG_TO_RAD;
int minuteX = tft.width() / 2 + 60 * cos(minuteAngle);
int minuteY = tft.height() / 2 + 60 * sin(minuteAngle);
tft.drawLine(tft.width() / 2, tft.height() / 2, minuteX, minuteY, ILI9341_GREEN);
// Draw second hand
float secondAngle = second * 6 * DEG_TO_RAD;
int secondX = tft.width() / 2 + 80 * cos(secondAngle);
int secondY = tft.height() / 2 + 80 * sin(secondAngle);
tft.drawLine(tft.width() / 2, tft.height() / 2, secondX, secondY, ILI9341_BLUE);
}
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
tft.begin();
tft.setRotation(1);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(100);
tft.print(".");
}
tft.print("\nOK! IP=");
tft.println(WiFi.localIP());
Serial.println(WiFi.localIP());
timeClient.begin();
timeClient.setTimeOffset(3600); // Set offset to 0 for UTC time
}
void loop() {
timeClient.update();
int currentHour = timeClient.getHours();
int currentMinute = timeClient.getMinutes();
int currentSecond = timeClient.getSeconds();
Serial.printf("%02d:%02d:%02d\n", currentHour, currentMinute, currentSecond);
drawClock(currentHour, currentMinute, currentSecond);
delay(1000); // Update every second
}