How to get Date and Time?

MalteJ
Posts: 62
Joined: Wed Sep 21, 2016 10:26 pm

How to get Date and Time?

Postby MalteJ » Sun Feb 19, 2017 11:11 am

Hi,

what is the preferred way to realize a datetime clock on ESP32?
I'd like to get a reference time from the internet (e.g. via NTP) and set a local clock.

The only thing I have found is xTaskGetTickCount().
Using this I can imagine something like

Code: Select all

uint32_t offset;

void init_timestamp() {
    // get current timestamp from the internet
    uint32_t ntp_timestamp = ntp_get_timestamp();
    
    offset = ntp_timestamp - xTaskGetTickCount() / (portTICK_RATE_MS * 1000);
}

uint32_t get_timestamp() {
    return offset + xTaskGetTickCount() / (portTICK_RATE_MS * 1000);
}
Would this be an accurate time source?
Is there a better solution?

Thank you!

Best,
Malte

MalteJ
Posts: 62
Joined: Wed Sep 21, 2016 10:26 pm

Re: How to get Date and Time?

Postby MalteJ » Sun Feb 19, 2017 11:33 am

Oh, there is an example I have missed:
https://github.com/espressif/esp-idf/tr ... ocols/sntp

:)

Who is online

Users browsing this forum: No registered users and 160 guests