Difference between revisions of "Internet Clock 1"

From
Jump to: navigation, search
Line 143: Line 143:
 
   delay(1000);  // Update every second
 
   delay(1000);  // Update every second
 
}</pre>
 
}</pre>
 +
 +
 +
Code adapté pour tenir compte du daylight<br>
 +
 +
<PRE>
 +
#include <U8g2lib.h>
 +
#include <Wire.h>
 +
#include <WiFi.h>
 +
#include <time.h>
 +
 +
// WiFi credentials
 +
const char* ssid    = "Your_wifi_SSID";
 +
const char* password = "Wifi_password";
 +
 +
// OLED display (SSD1306 128x64)
 +
U8G2_SSD1306_128X64_NONAME_F_SW_I2C
 +
u8g2(U8G2_R0, /* clock=*/ 22, /* data=*/ 21, /* reset=*/ U8X8_PIN_NONE);
 +
 +
void setup() {
 +
  Serial.begin(115200);
 +
 +
  // Connexion Wi-Fi
 +
  WiFi.begin(ssid, password);
 +
  while (WiFi.status() != WL_CONNECTED) {
 +
    delay(1000);
 +
    Serial.println("Connecting to WiFi...");
 +
  }
 +
  Serial.println("Connected to WiFi");
 +
 +
  // Initialisation OLED
 +
  u8g2.begin();
 +
 +
  // Configuration NTP + fuseau horaire Europe/Zurich avec DST
 +
  configTime(0, 0, "pool.ntp.org", "time.nist.gov");
 +
  setenv("TZ", "CET-1CEST,M3.5.0,M10.5.0/3", 1);
 +
  tzset();
 +
}
 +
 +
void loop() {
 +
  struct tm timeinfo;
 +
  if (!getLocalTime(&timeinfo)) {
 +
    Serial.println("Failed to obtain time");
 +
    return;
 +
  }
 +
 +
  // Extraction des composants
 +
  int hours = timeinfo.tm_hour;
 +
  int minutes = timeinfo.tm_min;
 +
  int seconds = timeinfo.tm_sec;
 +
  int day = timeinfo.tm_mday;
 +
  int month = timeinfo.tm_mon + 1;
 +
  int year = timeinfo.tm_year + 1900;
 +
 +
  String daysOfWeek[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
 +
  String dayOfWeek = daysOfWeek[timeinfo.tm_wday];
 +
 +
  // Effacer l'écran
 +
  u8g2.clearBuffer();
 +
 +
  // Heure
 +
  u8g2.setFont(u8g2_font_ncenB18_tr);
 +
  String timeStr = String(hours) + ":" +
 +
                  (minutes < 10 ? "0" + String(minutes) : String(minutes)) + ":" +
 +
                  (seconds < 10 ? "0" + String(seconds) : String(seconds));
 +
  int textWidth = u8g2.getStrWidth(timeStr.c_str());
 +
  u8g2.setCursor((128 - textWidth) / 2, 24);
 +
  u8g2.print(timeStr);
 +
 +
  // Date
 +
  u8g2.setFont(u8g2_font_ncenB10_tr);
 +
  String dateStr = (day < 10 ? "0" + String(day) : String(day)) + "/" +
 +
                  (month < 10 ? "0" + String(month) : String(month)) + "/" +
 +
                  String(year);
 +
  int dateTextWidth = u8g2.getStrWidth(dateStr.c_str());
 +
  u8g2.setCursor((128 - dateTextWidth) / 2, 40);
 +
  u8g2.print(dateStr);
 +
 +
  // Jour de la semaine
 +
  int dayOfWeekWidth = u8g2.getStrWidth(dayOfWeek.c_str());
 +
  u8g2.setCursor((128 - dayOfWeekWidth) / 2, 56);
 +
  u8g2.print(dayOfWeek);
 +
 +
  u8g2.sendBuffer();
 +
 +
  delay(1000);
 +
}
 +
 +
</PRE>

Revision as of 21:56, 15 September 2025

retour

Internet Clock with ESP32 & OLED 128×64 Display

https://techlogics.net/create-an-internet-clock-with-esp32-oled-128x64-display-live-date-time-tutorial/

GMT Offsets
https://techlogics.net/how-to-use-gmt-offsets-in-your-arduino-projects-for-accurate-time-synchronization/

#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 (128x128 SH1107)
// U8G2_SH1107_SEEED_128X128_1_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);

 
// Initialize OLED display (128x64 SH1106)
// U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);

// Initialize OLED display (128x64 SSD1306) Ideaspark ESP32 0,96 OLED Board
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(&currentTime);  // 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
}


Code adapté pour tenir compte du daylight

#include <U8g2lib.h>
#include <Wire.h>
#include <WiFi.h>
#include <time.h>

// WiFi credentials
const char* ssid     = "Your_wifi_SSID";
const char* password = "Wifi_password";

// OLED display (SSD1306 128x64)
U8G2_SSD1306_128X64_NONAME_F_SW_I2C 
u8g2(U8G2_R0, /* clock=*/ 22, /* data=*/ 21, /* reset=*/ U8X8_PIN_NONE);

void setup() {
  Serial.begin(115200);

  // Connexion Wi-Fi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");

  // Initialisation OLED
  u8g2.begin();

  // Configuration NTP + fuseau horaire Europe/Zurich avec DST
  configTime(0, 0, "pool.ntp.org", "time.nist.gov");
  setenv("TZ", "CET-1CEST,M3.5.0,M10.5.0/3", 1);
  tzset();
}

void loop() {
  struct tm timeinfo;
  if (!getLocalTime(&timeinfo)) {
    Serial.println("Failed to obtain time");
    return;
  }

  // Extraction des composants
  int hours = timeinfo.tm_hour;
  int minutes = timeinfo.tm_min;
  int seconds = timeinfo.tm_sec;
  int day = timeinfo.tm_mday;
  int month = timeinfo.tm_mon + 1;
  int year = timeinfo.tm_year + 1900;

  String daysOfWeek[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
  String dayOfWeek = daysOfWeek[timeinfo.tm_wday];

  // Effacer l'écran
  u8g2.clearBuffer();

  // Heure
  u8g2.setFont(u8g2_font_ncenB18_tr);
  String timeStr = String(hours) + ":" +
                   (minutes < 10 ? "0" + String(minutes) : String(minutes)) + ":" +
                   (seconds < 10 ? "0" + String(seconds) : String(seconds));
  int textWidth = u8g2.getStrWidth(timeStr.c_str());
  u8g2.setCursor((128 - textWidth) / 2, 24);
  u8g2.print(timeStr);

  // Date
  u8g2.setFont(u8g2_font_ncenB10_tr);
  String dateStr = (day < 10 ? "0" + String(day) : String(day)) + "/" +
                   (month < 10 ? "0" + String(month) : String(month)) + "/" +
                   String(year);
  int dateTextWidth = u8g2.getStrWidth(dateStr.c_str());
  u8g2.setCursor((128 - dateTextWidth) / 2, 40);
  u8g2.print(dateStr);

  // Jour de la semaine
  int dayOfWeekWidth = u8g2.getStrWidth(dayOfWeek.c_str());
  u8g2.setCursor((128 - dayOfWeekWidth) / 2, 56);
  u8g2.print(dayOfWeek);

  u8g2.sendBuffer();

  delay(1000);
}