abfall/src/ntp_client.cpp

40 lines
903 B
C++

#include "ntp_client.h"
#include <ctime>
#include "hardware/rtc.h"
#include "pico/util/datetime.h"
// Called by lwip sntp in order to set the time
void set_system_time(u32_t sec) {
time_t epoch = sec;
struct tm *utc = gmtime(&epoch);
datetime_t datetime;
datetime.year = utc->tm_year + 1900;
datetime.month = utc->tm_mon + 1;
datetime.day = utc->tm_mday;
datetime.hour = utc->tm_hour;
datetime.min = utc->tm_min;
datetime.sec = utc->tm_sec;
datetime.dotw = utc->tm_wday;
bool success = rtc_set_datetime(&datetime);
#ifdef DEBUG
if (success) {
printf("RTC successfully set.\n");
}
#endif
}
void NtpClient::setDateTime() {
sntp_setoperatingmode(SNTP_OPMODE_POLL);
sntp_init();
// It takes some time for the system to set the RTC. Wait, until the RTC runs properly.
while (!rtc_running()) {
sleep_ms(500);
}
}