#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
// Define the OLED display
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Replace with your network credentials
//const char* ssid = "BSNL 5G";
//const char* password = "KIMAYA0207";
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
void setup() {
// Initialize the display
if(!display.begin(SSD1306_PAGEADDR, OLED_RESET)){
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
// Initialize NTP Client
timeClient.begin();
timeClient.setTimeOffset(3600); // UTC+1 (Change this according to your timezone)
// Clear the display
display.clearDisplay();
display.display();
// Set text size and color
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
// Display welcome message
display.setCursor(0, 0);
display.println("IoT Clock");
display.display();
}
void loop() {
// Get the current time from the NTP server
timeClient.update();
// Display the time on the OLED
display.clearDisplay();
display.setCursor(0, 0);
display.print("Time: ");
display.println(timeClient.getFormattedTime());
display.display();
delay(1000); // Update every second
}