#include <Arduino.h>
#include <U8g2lib.h>
#include <Wire.h>
#include <WiFi.h>
#include "time.h"
// --- WiFi credentials ---
const char* ssid = "Airtel_arvi_3035";
const char* password = "air26289";
// --- NTP server and timezone ---
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 19800; // IST = UTC + 5:30 = 19800 seconds
const int daylightOffset_sec = 0; // no DST in India
// OLED setup (ESP32-C3 Mini I2C pins: SDA=8, SCL=9)
U8G2_SH1107_128X128_1_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE, /* clock=*/ 9, /* data=*/ 8);
int center_x = 64;
int center_y = 40; // move clock up to leave space for digital text
void setup() {
Serial.begin(115200);
// Start OLED
u8g2.begin();
u8g2.setContrast(255);
// Connect WiFi
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected!");
// Init NTP
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
}
// Draw analog clock background
void draw_background() {
float xpos, ypos, xpos2, ypos2;
u8g2.setDrawColor(1);
u8g2.drawCircle(center_x, center_y, 30, U8G2_DRAW_ALL);
// 60 dots
for (int i=0; i<60; i++) {
xpos = round(center_x + sin(radians(i * 6)) * 28);
ypos = round(center_y - cos(radians(i * 6)) * 28);
u8g2.drawPixel(xpos,ypos);
}
// Labels
u8g2.setFont(u8g2_font_5x8_mf);
u8g2.drawStr(center_x-3, center_y-20,"12");
u8g2.drawStr(center_x+20, center_y+3,"3");
u8g2.drawStr(center_x-3, center_y+25,"6");
u8g2.drawStr(center_x-25, center_y+3,"9");
}
void draw_hand(int angle, int length) {
int x = round(center_x + sin(radians(angle)) * length);
int y = round(center_y - cos(radians(angle)) * length);
u8g2.drawLine(center_x, center_y, x, y);
}
void loop() {
// Get current time
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("Failed to obtain time");
return;
}
int hours = timeinfo.tm_hour;
int minutes = timeinfo.tm_min;
int seconds = timeinfo.tm_sec;
// Draw screen
u8g2.firstPage();
do {
// Analog clock
draw_background();
draw_hand(hours*30 + minutes/2, 15); // hour hand
draw_hand(minutes*6, 22); // minute hand
draw_hand(seconds*6, 26); // second hand
// Center circle
u8g2.setColorIndex(1);
u8g2.drawDisc(center_x, center_y, 2);
// Digital time below
char buf[20];
sprintf(buf, "%02d:%02d:%02d", hours, minutes, seconds);
u8g2.setFont(u8g2_font_8x13B_mn);
u8g2.drawStr(30, 100, buf);
// Branding text
u8g2.setFont(u8g2_font_5x8_mf);
u8g2.drawStr(20, 120, "AI Centre Nandurbar");
} while (u8g2.nextPage());
delay(1000); // update once per second
}