EasyManuals Logo

Espressif ESP32-S2 User Manual

Espressif ESP32-S2
1695 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Page #1063 background imageLoading...
Page #1063 background image
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() to set one of the following sync modes:
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 in project configuration.
A code example that demonstrates the implementation of time synchronization based on the lwIP SNTP library is
provided in protocols/sntp directory.
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 GNU libc documentation (although the implementation
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. It is possible to replace all SNTP update functionality by placing a
sntp_sync_time() function in the app firmware source. If the default implementation is used, calling
sntp_set_sync_mode() allows the time synchronization mode to be changed to instant or smooth. If a callback
Espressif Systems 1052
Submit Document Feedback
Release v4.4

Table of Contents

Questions and Answers:

Question and Answer IconNeed help?

Do you have a question about the Espressif ESP32-S2 and is the answer not in the manual?

Espressif ESP32-S2 Specifications

General IconGeneral
BrandEspressif
ModelESP32-S2
CategorySingle board computers
LanguageEnglish