Chapter 2. API Reference
SNTP Time Synchronization
To set the current time, you can use the POSIX functions settimeofday() and adjtime(). They are used
internally in the lwIP SNTP library to set current time when a response from the NTP server is received. These
functions can also be used separately from the lwIP SNTP library.
A function to use inside the lwIP SNTP library depends on a sync mode for system time. Use the function
sntp_set_sync_mode()
• SNTP_SYNC_MODE_IMMED (default) updates system time immediately upon receiving a response from the
SNTP server after using settimeofday().
• SNTP_SYNC_MODE_SMOOTH updates time smoothly by gradually reducing time error using the funcion
adjtime(). If the difference between the SNTP response time and system time is more than 35 minutes,
update system time immediately by using settimeofday().
The lwIP SNTP library has API functions for setting a callback function for a certain event. You might need the
following functions:
• sntp_set_time_sync_notification_cb() - use it for setting a callback function that will notify
of the time synchronization process
• sntp_get_sync_status() and sntp_set_sync_status() - use it to get/set time synchronization
status
To start synchronization via SNTP, just call the following three functions.
sntp_setoperatingmode(SNTP_OPMODE_POLL);
sntp_setservername(0, "pool.ntp.org");
sntp_init();
An application with this initialization code will periodically synchronize the time. The time synchronization period is
determined by CONFIG_LWIP_SNTP_UPDATE_DELAY (default value is one hour). To modify the variable, set
CONFIG_LWIP_SNTP_UPDATE_DELAY
A code example that demonstrates the implementation of time synchronization based on the lwIP SNTP library is
provided in
protocols/sntp
Timezones
To set local timezone, use the following POSIX functions:
1. Call setenv() to set the TZ environment variable to the correct value depending on the device location. The
format of the time string is the same as described in the
is different).
2. Call tzset() to update C library runtime data for the new time zone.
Once these steps are completed, call the standard C library function localtime(), and it will return correct local
time taking into account the time zone offset and daylight saving time.
API Reference
Header File
•
components/lwip/include/apps/esp_sntp.h
Functions
void sntp_sync_time(struct timeval *tv)
This function updates the system time.
This is a weak-linked function.
sntp_sync_time() function in the app firmware source.
sntp_set_sync_mode() allows the time synchronization mode to be changed to instant or smooth. If a callback
Espressif Systems
to set one of the following sync modes:
in project configuration.
directory.
It is possible to replace all SNTP update functionality by placing a
Submit Document Feedback
GNU libc documentation
If the default implementation is used, calling
1052
(although the implementation
Release v4.4
Need help?
Do you have a question about the ESP32-S2 and is the answer not in the manual?
Questions and answers