abfall/src/ntp_client.cpp

36 lines
824 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) {
sec += 3600; // MEZ
time_t epoch = sec;
struct tm *mez = gmtime(&epoch);
datetime_t datetime;
datetime.year = mez->tm_year + 1900;
datetime.month = mez->tm_mon + 1;
datetime.day = mez->tm_mday;
datetime.hour = mez->tm_hour;
datetime.min = mez->tm_min;
datetime.sec = mez->tm_sec;
datetime.dotw = mez->tm_wday;
rtc_set_datetime(&datetime);
}
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);
}
}