#include <WiFi.h>
#include <WiFiUdp.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// #include <TimeLib.h>
#define SCREEN_WIDTH 128 // Change this to match your display
#define SCREEN_HEIGHT 64 // Change this to match your display
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const char *ssid = "Your_SSID"; // Change this to your WiFi network SSID
const char *password = "Your_PASSWORD"; // Change this to your WiFi password
const char *ntpServer = "europe.pool.ntp.org";
const int timeZone = 1; // Change this to your time zone offset
WiFiUDP udp;
unsigned int localPort = 8888;
void setup() {
// Initialize the OLED display
if(!display.begin(SSD1306_I2C_ADDRESS, OLED_RESET)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
// Connect to Wi-Fi
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Initialize UDP client
udp.begin(localPort);
Serial.println("UDP client started");
// Set your time zone and update the time
configTime(timeZone * 3600, 0, ntpServer);
Serial.println("Waiting for time sync...");
while (!time(nullptr)) {
delay(1000);
}
}
void loop() {
display.clearDisplay();
// Get the current time
time_t now = time(nullptr);
// Format and display the time on the OLED display
struct tm timeInfo;
localtime_r(&now, &timeInfo);
char timeStr[20];
strftime(timeStr, sizeof(timeStr), "%Y-%m-%d %H:%M:%S", &timeInfo);
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("Time:");
display.setTextSize(2);
display.setCursor(0, 20);
display.println(timeStr);
display.display();
delay(1000);
}