Simple Network Time Protocol

ESP has built-in support for Simple Network Time Protocol (SNTP). It is support through middleware API calls for configuring servers and reading actual date and time.

Minimum SNTP example
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "sntp.h"
#include "esp/esp.h"

/**
 * \brief           Run SNTP
 */
void
sntp_gettime(void) {
    esp_datetime_t dt;

    /* Enable SNTP with default configuration for NTP servers */
    if (esp_sntp_configure(1, 1, NULL, NULL, NULL, NULL, NULL, 1) == espOK) {
        esp_delay(5000);

        /* Get actual time and print it */
        if (esp_sntp_gettime(&dt, NULL, NULL, 1) == espOK) {
            printf("Date & time: %d.%d.%d, %d:%d:%d\r\n",
                (int)dt.date, (int)dt.month, (int)dt.year,
                (int)dt.hours, (int)dt.minutes, (int)dt.seconds);
        }
    }
}
group ESP_SNTP

Simple network time protocol supported by AT commands.

Functions

espr_t esp_sntp_configure(uint8_t en, int8_t tz, const char *h1, const char *h2, const char *h3, const esp_api_cmd_evt_fn evt_fn, void *const evt_arg, const uint32_t blocking)

Configure SNTP mode parameters.

Return

espOK on success, member of espr_t enumeration otherwise

Parameters
  • [in] en: Status whether SNTP mode is enabled or disabled on ESP device

  • [in] tz: Timezone to use when SNTP acquires time, between -11 and 13

  • [in] h1: Optional first SNTP server for time. Set to NULL if not used

  • [in] h2: Optional second SNTP server for time. Set to NULL if not used

  • [in] h3: Optional third SNTP server for time. Set to NULL if not used

  • [in] evt_fn: Callback function called when command has finished. Set to NULL when not used

  • [in] evt_arg: Custom argument for event callback function

  • [in] blocking: Status whether command should be blocking or not

espr_t esp_sntp_gettime(esp_datetime_t *dt, const esp_api_cmd_evt_fn evt_fn, void *const evt_arg, const uint32_t blocking)

Get time from SNTP servers.

Return

espOK on success, member of espr_t enumeration otherwise

Parameters
  • [out] dt: Pointer to esp_datetime_t structure to fill with date and time values

  • [in] evt_fn: Callback function called when command has finished. Set to NULL when not used

  • [in] evt_arg: Custom argument for event callback function

  • [in] blocking: Status whether command should be blocking or not