55 lines
1.4 KiB
C
55 lines
1.4 KiB
C
#include <NTPClient.h>
|
|
#include <WiFiUdp.h>
|
|
|
|
WiFiUDP ntpUDP;
|
|
NTPClient timeClient(ntpUDP, "pool.ntp.org", 10800, 60000); // Update every 60 seconds
|
|
unsigned long startMillis;
|
|
unsigned long syncedMillisOffset;
|
|
|
|
void ntp_begin() {
|
|
timeClient.begin();
|
|
timeClient.update();
|
|
// startMillis = millis() - timeClient.getEpochTime() * 1000;
|
|
startMillis = millis();
|
|
}
|
|
|
|
void ntp_update() {
|
|
timeClient.update();
|
|
}
|
|
|
|
unsigned long ntp_sync_millis() {
|
|
return startMillis + millis();
|
|
}
|
|
|
|
String ntp_print_time() {
|
|
|
|
// Calculate the time components
|
|
unsigned long currentMillis = millis();
|
|
unsigned long syncedMillis = currentMillis + syncedMillisOffset;
|
|
|
|
unsigned long totalSeconds = syncedMillis / 1000;
|
|
unsigned long hours = (totalSeconds % 86400) / 3600;
|
|
unsigned long minutes = (totalSeconds % 3600) / 60;
|
|
unsigned long seconds = totalSeconds % 60;
|
|
unsigned long milliseconds = syncedMillis % 1000;
|
|
|
|
// Adjust for 12-hour format and determine AM/PM
|
|
String ampm = "AM";
|
|
|
|
if(hours >= 12) {
|
|
ampm = "PM";
|
|
if(hours > 12)
|
|
hours -= 12;
|
|
}
|
|
|
|
if(hours == 0) {
|
|
hours = 12;
|
|
}
|
|
|
|
// Generate String
|
|
String x = String(hours) + ":" + String(minutes) + ":" + String(seconds) + ":" + String(milliseconds) + ":" + ampm;
|
|
|
|
// Return String Value
|
|
return x;
|
|
|
|
} |