2023-01-02 11:10:22 +01:00
|
|
|
#include "ntp_client.h"
|
|
|
|
|
2023-01-03 08:57:39 +01:00
|
|
|
#include <ctime>
|
|
|
|
|
2023-01-02 11:10:22 +01:00
|
|
|
#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) {
|
2023-01-10 11:27:15 +01:00
|
|
|
sec += 3600; // MEZ
|
|
|
|
|
2023-01-02 11:10:22 +01:00
|
|
|
time_t epoch = sec;
|
2023-01-10 11:27:15 +01:00
|
|
|
struct tm *mez = gmtime(&epoch);
|
2023-01-02 11:10:22 +01:00
|
|
|
datetime_t datetime;
|
2023-01-10 11:27:15 +01:00
|
|
|
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);
|
2023-01-02 11:10:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2023-01-04 10:16:35 +01:00
|
|
|
}
|