Advertisement
pleasedontcode

RTC for Timekeeping

Feb 12th, 2025
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 0.63 KB | Source Code | 0 0
  1. #include "time.h"
  2.  
  3. const char* ntpServer = "pool.ntp.org";
  4. const long  gmtOffset_sec = 3600;  // Adjust according to timezone
  5. const int   daylightOffset_sec = 3600;
  6.  
  7. void setup() {
  8.     Serial.begin(115200);
  9.  
  10.     // Configure and start NTP for time synchronization
  11.     configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
  12.  
  13.     struct tm timeinfo;
  14.     if (getLocalTime(&timeinfo)) {
  15.         Serial.println("Current time:");
  16.         Serial.printf("%02d:%02d:%02d\n", timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec);
  17.     } else {
  18.         Serial.println("Failed to obtain time");
  19.     }
  20. }
  21.  
  22. void loop() {
  23.     delay(1000);
  24. }
  25.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement