#include <WiFi.h>
#include "time.h"
#include "SPI.h"
#include "TFT_eSPI.h"
TFT_eSPI tft = TFT_eSPI();
unsigned long prev = 0;
int prev_mday;
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 3600*9;
const int daylightOffset_sec = 0;
void setup() {
Serial.begin(115200);
tft.begin();
tft.setRotation(3); // 0~3
tft.fillScreen(TFT_BLACK);
Serial.printf("Connecting to %s ", ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("CONNECTED");
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
printLocalTime();
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
}
void loop() {
unsigned long now = millis();
if (now - prev > 1000) {
prev = now;
printLocalTime();
}
}
void printLocalTime() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("Failed to obtain time");
return;
}
Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
if (prev_mday != timeinfo.tm_mday) {
prev_mday = timeinfo.tm_mday;
tft.fillScreen(TFT_BLACK);
}
tft.setTextSize(3);
tft.setCursor(20, 30);
tft.setTextColor(TFT_YELLOW);
tft.print(&timeinfo, "%B %d %Y"); // May 15 2023
tft.setTextSize(3);
tft.setCursor(60, 80);
tft.setTextColor(TFT_CYAN);
tft.println(&timeinfo, "%A"); // Monday
tft.fillRect(39, 149, 240, 40, ILI9341_BLACK);
tft.setTextSize(5);
tft.setCursor(40, 150);
tft.setTextColor(TFT_GREEN);
tft.println(&timeinfo, "%H:%M:%S"); // 10:30:05
}