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#include "sntp.h"
 2#include "lwesp/lwesp.h"
 3
 4/**
 5 * \brief           Run SNTP
 6 */
 7void
 8sntp_gettime(void) {
 9    lwesp_datetime_t dt;
10
11    /* Enable SNTP with default configuration for NTP servers */
12    if (lwesp_sntp_set_config(1, 1, NULL, NULL, NULL, NULL, NULL, 1) == lwespOK) {
13        lwesp_delay(5000);
14
15        /* Get actual time and print it */
16        if (lwesp_sntp_gettime(&dt, NULL, NULL, 1) == lwespOK) {
17            printf("Date & time: %d.%d.%d, %d:%d:%d\r\n",
18                   (int)dt.date, (int)dt.month, (int)dt.year,
19                   (int)dt.hours, (int)dt.minutes, (int)dt.seconds);
20        }
21    }
22}
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.

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

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

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

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

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

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

  • evt_arg[in] Custom argument for event callback function

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

Returns

lwespOK on success, member of lwespr_t enumeration otherwise

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.

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

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

  • evt_arg[in] Custom argument for event callback function

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

Returns

lwespOK on success, member of lwespr_t enumeration otherwise