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 "lwesp/lwesp.h"

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

    /* Enable SNTP with default configuration for NTP servers */
    if (lwesp_sntp_set_config(1, 1, NULL, NULL, NULL, NULL, NULL, 1) == lwespOK) {
        lwesp_delay(5000);

        /* Get actual time and print it */
        if (lwesp_sntp_gettime(&dt, NULL, NULL, 1) == lwespOK) {
            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 LWESP_SNTP

Simple network time protocol supported by AT commands.

Functions

lwespr_t lwesp_sntp_set_config(uint8_t en, int8_t tz, const char *h1, const char *h2, const char *h3, const lwesp_api_cmd_evt_fn evt_fn, void *const evt_arg, const uint32_t blocking)

Configure SNTP mode parameters.

Return

lwespOK on success, member of lwespr_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

lwespr_t lwesp_sntp_gettime(lwesp_datetime_t *dt, const lwesp_api_cmd_evt_fn evt_fn, void *const evt_arg, const uint32_t blocking)

Get time from SNTP servers.

Return

lwespOK on success, member of lwespr_t enumeration otherwise

Parameters
  • [out] dt: Pointer to lwesp_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