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) {
|
|
|
|
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;
|
2023-01-03 08:57:39 +01:00
|
|
|
|
|
|
|
bool success = rtc_set_datetime(&datetime);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (success) {
|
2023-01-02 11:10:22 +01:00
|
|
|
printf("RTC successfully set.\n");
|
|
|
|
}
|
2023-01-03 08:57:39 +01:00
|
|
|
#endif
|
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);
|
|
|
|
}
|
|
|
|
}
|