Difference between revisions of "Internet Clock 1"
| Line 21: | Line 21: | ||
// U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); | // U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); | ||
| − | // Initialize OLED display (128x64 | + | // Initialize OLED display (128x64 SSD1306) |
U8G2_SSD1306_128X64_NONAME_F_SW_I2C | U8G2_SSD1306_128X64_NONAME_F_SW_I2C | ||
u8g2(U8G2_R0, /* clock=*/ 22, /* data=*/ 21, /* reset=*/ U8X8_PIN_NONE); | u8g2(U8G2_R0, /* clock=*/ 22, /* data=*/ 21, /* reset=*/ U8X8_PIN_NONE); | ||
Revision as of 20:36, 10 December 2024
Internet Clock with ESP32 & OLED 128×64 Display
#include <U8g2lib.h>
#include <Wire.h>
#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <time.h>
// Replace with your WiFi credentials
const char* ssid = "Your_wifi_SSID";
const char* password = "Wifi_password";
// Initialize OLED display (128x64 SH1106)
// U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
// Initialize OLED display (128x64 SSD1306)
U8G2_SSD1306_128X64_NONAME_F_SW_I2C
u8g2(U8G2_R0, /* clock=*/ 22, /* data=*/ 21, /* reset=*/ U8X8_PIN_NONE);
// NTP Client Setup
WiFiUDP ntpUDP;
// NTPClient timeClient(ntpUDP, "pool.ntp.org", 19800, 3600000); // Timezone offset (19800 seconds = UTC +5:30) Colombo
NTPClient timeClient(ntpUDP, "pool.ntp.org", 3600, 3600000); // Timezone offset (3600 seconds = UTC +1:00) Zurich
void setup() {
// Start serial communication for debugging
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Initialize the OLED
u8g2.begin();
// Start the NTP client
timeClient.begin();
//timeClient.setTimeOffset(19800); // Indian Standard Time colombo (UTC +5:30)
timeClient.setTimeOffset(3600); // Europe Standard Time Zurich (UTC +1:00)
}
void loop() {
timeClient.update(); // Update time from NTP server
// Get current time in seconds
unsigned long currentEpoch = timeClient.getEpochTime();
// Convert epoch time (unsigned long) to time_t (signed long)
time_t currentTime = (time_t)currentEpoch;
// Convert time to time structure
struct tm* timeInfo;
timeInfo = localtime(¤tTime); // Convert to time structure
// Extract time components
int hours = timeInfo->tm_hour;
int minutes = timeInfo->tm_min;
int seconds = timeInfo->tm_sec;
// Extract date components
int day = timeInfo->tm_mday;
int month = timeInfo->tm_mon + 1; // tm_mon is zero-based, so add 1
int year = timeInfo->tm_year + 1900; // tm_year is years since 1900, so add 1900
// Extract day of the week (0 = Sunday, 1 = Monday, etc.)
String daysOfWeek[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
String dayOfWeek = daysOfWeek[timeInfo->tm_wday];
// Clear the display
u8g2.clearBuffer();
// Set font size for the clock
u8g2.setFont(u8g2_font_ncenB18_tr); // Larger font (18px)
// Format the time
String timeStr = String(hours) + ":" + (minutes < 10 ? "0" + String(minutes) : String(minutes)) + ":" + (seconds < 10 ? "0" + String(seconds) : String(seconds));
// Calculate the width of the time text to center it
int textWidth = u8g2.getStrWidth(timeStr.c_str());
// Set the cursor position to center the time text
int x = (128 - textWidth) / 2; // Center horizontally on 128x64 screen
int y = 24; // Set Y position to the middle of the screen for time
// Draw the time at the center of the screen
u8g2.setCursor(x, y);
u8g2.print(timeStr);
// Set font size for the date
u8g2.setFont(u8g2_font_ncenB10_tr); // Smaller font (10px) for the date
// Format the date as DD/MM/YYYY with leading zeroes if necessary
String dayStr = (day < 10) ? "0" + String(day) : String(day);
String monthStr = (month < 10) ? "0" + String(month) : String(month);
String dateStr = dayStr + "/" + monthStr + "/" + String(year);
// Calculate the width of the date text to center it
int dateTextWidth = u8g2.getStrWidth(dateStr.c_str());
// Set the cursor position to center the date text
int dateX = (128 - dateTextWidth) / 2; // Center horizontally
int dateY = 40; // Move the date 2px up from the previous position
// Draw the date below the time
u8g2.setCursor(dateX, dateY);
u8g2.print(dateStr);
// Set font size for the day of the week
u8g2.setFont(u8g2_font_ncenB10_tr); // Smaller font (10px) for the day of the week
// Calculate the width of the day of the week text to center it
int dayOfWeekWidth = u8g2.getStrWidth(dayOfWeek.c_str());
// Set the cursor position to center the day of the week text
int dayOfWeekX = (128 - dayOfWeekWidth) / 2; // Center horizontally
int dayOfWeekY = 56; // Set Y position below the date text
// Draw the day of the week below the date
u8g2.setCursor(dayOfWeekX, dayOfWeekY);
u8g2.print(dayOfWeek);
// Send the buffer to the display
u8g2.sendBuffer();
delay(1000); // Update every second
}