esp-clock/include/ntp_time.h

31 lines
782 B
C++

#pragma once
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <Timezone.h>
namespace Ntp {
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "192.168.6.1");
// Central European Time (Frankfurt, Paris)
TimeChangeRule CEST = { "CEST", Last, Sun, Mar, 2, 120 }; // Central European Summer Time
TimeChangeRule CET = { "CET ", Last, Sun, Oct, 3, 60 }; // Central European Standard Time
Timezone CE(CEST, CET);
time_t updateTime() {
if (timeClient.forceUpdate()) {
time_t newTime = CE.toLocal(timeClient.getEpochTime());
setTime(newTime);
return newTime;
} else {
return 0;
}
}
void setup() {
timeClient.begin();
Serial.println("NTP setup");
}
}