#include <WiFi.h>
#include <time.h>
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
// ======================================================
// CYD ESP32-2432S028
// WIFI DIGITAL CLOCK — INDIA TIME
// ======================================================
// ---------------- WIFI ----------------
const char* ssid = "Airtel_arvi_6035";
const char* password = "air26289";
// ---------------- TFT -----------------
#define TFT_CS 15
#define TFT_DC 2
#define TFT_MOSI 13
#define TFT_MISO 12
#define TFT_SCK 14
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
// ---------------- RGB LED -------------
#define LED_R 4
#define LED_G 16
#define LED_B 17
// ---------------- DISPLAY SIZE --------
#define W 320
#define H 240
// ======================================================
// RGB LED CONTROL
// ======================================================
void setRGB(bool r, bool g, bool b) {
digitalWrite(LED_R, r ? LOW : HIGH);
digitalWrite(LED_G, g ? LOW : HIGH);
digitalWrite(LED_B, b ? LOW : HIGH);
}
// ======================================================
// GRADIENT BACKGROUND
// ======================================================
void drawGradient() {
for (int y = 0; y < H; y++) {
uint8_t r = map(y, 0, H, 10, 0);
uint8_t g = map(y, 0, H, 20, 120);
uint8_t b = map(y, 0, H, 80, 255);
tft.drawFastHLine(
0,
y,
W,
tft.color565(r, g, b)
);
}
}
// ======================================================
// FRAME
// ======================================================
void drawFrame() {
tft.drawRoundRect(
8,
8,
W - 16,
H - 16,
12,
ILI9341_CYAN
);
tft.drawRoundRect(
14,
14,
W - 28,
H - 28,
10,
ILI9341_BLUE
);
}
// ======================================================
// WIFI CONNECT
// ======================================================
void connectWiFi() {
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_CYAN);
tft.setTextSize(2);
tft.setCursor(40, 100);
tft.print("Connecting WiFi");
WiFi.begin(ssid, password);
int dots = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
tft.print(".");
dots++;
if (dots > 10) {
tft.fillRect(40, 120, 220, 20, ILI9341_BLACK);
tft.setCursor(40, 120);
dots = 0;
}
setRGB(
random(2),
random(2),
random(2)
);
}
setRGB(false, true, false);
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(60, 110);
tft.setTextColor(ILI9341_GREEN);
tft.print("WiFi Connected");
delay(1500);
}
// ======================================================
// TIME SETUP
// ======================================================
void setupTime() {
configTime(
19800,
0,
"pool.ntp.org",
"time.nist.gov"
);
}
// ======================================================
// CLOCK DISPLAY
// ======================================================
void drawClock() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(60, 110);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(2);
tft.print("Time Failed");
return;
}
// ===============================
// BACKGROUND
// ===============================
drawGradient();
drawFrame();
// ===============================
// TITLE
// ===============================
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.setCursor(80, 28);
tft.print("INDIA DIGITAL CLOCK");
// ===============================
// TIME
// ===============================
char timeStr[20];
strftime(
timeStr,
sizeof(timeStr),
"%H:%M:%S",
&timeinfo
);
tft.setTextColor(ILI9341_YELLOW);
tft.setTextSize(5);
tft.setCursor(40, 95);
tft.print(timeStr);
// ===============================
// DATE
// ===============================
char dateStr[40];
strftime(
dateStr,
sizeof(dateStr),
"%d-%m-%Y",
&timeinfo
);
tft.setTextColor(ILI9341_CYAN);
tft.setTextSize(3);
tft.setCursor(75, 170);
tft.print(dateStr);
// ===============================
// DAY
// ===============================
char dayStr[20];
strftime(
dayStr,
sizeof(dayStr),
"%A",
&timeinfo
);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.setCursor(115, 205);
tft.print(dayStr);
// ===============================
// RGB CLOCK EFFECT
// ===============================
int sec = timeinfo.tm_sec;
if (sec % 3 == 0)
setRGB(true, false, false);
else if (sec % 3 == 1)
setRGB(false, true, false);
else
setRGB(false, false, true);
}
// ======================================================
// SETUP
// ======================================================
void setup() {
Serial.begin(115200);
SPI.begin(
TFT_SCK,
TFT_MISO,
TFT_MOSI,
TFT_CS
);
tft.begin();
tft.setRotation(1);
pinMode(LED_R, OUTPUT);
pinMode(LED_G, OUTPUT);
pinMode(LED_B, OUTPUT);
setRGB(false, false, false);
connectWiFi();
setupTime();
}
// ======================================================
// LOOP
// ======================================================
void loop() {
drawClock();
delay(1000);
}https://wokwi.com/projects/464272224165908481