| Supported Targets | ESP32 |
| ----------------- | ----- |
# Example: using LwIP SNTP module and time functions
This example demonstrates the use of LwIP SNTP module to obtain time from Internet servers. See the README.md file in the upper level 'examples' directory for more information about examples.
## Configuring the Example
Open the project configuration menu (`idf.py menuconfig`):
* Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../README.md) for more details.
* Select one method to synchronize time out of the three available in `CONFIG_SNTP_TIME_SYNC_METHOD` (default `update time immediately when received`).
* The time synchronization period is defined by `CONFIG_LWIP_SNTP_UPDATE_DELAY`. This option allows you to set the time synchronization period (default is one hour). This option does not affect this example because after synchronization the program goes into deep sleep for 10 seconds. If you modify this example or use the code from this example, keep in mind that this option will trigger time synchronization. You can change it in `Component config-->LWIP-->SNTP-->Request interval to update time (ms)` menu.
* When using Make build system, set `Default serial port` under `Serial flasher config`.
## Obtaining time using LwIP SNTP module
When this example boots first time after ESP32 is reset, it connects to WiFi and obtains time using SNTP.
See `initialize_sntp` function for details.
## Timekeeping
Once time is synchronized, ESP32 will perform timekeeping using built-in timers.
- RTC clock is used to maintain accurate time when chip is in deep sleep mode
- FRC1 timer is used to provide time at microsecond accuracy when ESP32 is running.
Timekeeping using RTC timer is demonstrated in this example by going into deep sleep mode. After wake up, ESP32 will print current time without connecting to WiFi.
To use this functionality, make sure "Timers used for gettimeofday function" option in "ESP32-specific config" menu of menuconfig is set to "RTC and FRC1" or "RTC".
## Working with time
To get current time, [`gettimeofday`](http://man7.org/linux/man-pages/man2/gettimeofday.2.html) function may be used. Additionally the following [standard C library functions](https://en.cppreference.com/w/cpp/header/ctime) can be used to obtain time and manipulate it:
gettimeofday
time
asctime
clock
ctime
difftime
gmtime
localtime
mktime
strftime
To set time, [`settimeofday`](http://man7.org/linux/man-pages/man2/settimeofday.2.html) POSIX function can be used. It is used internally in LwIP SNTP library to set current time when response from NTP server is received.
## Timezones
To set local timezone, use [`setenv`](http://man7.org/linux/man-pages/man3/setenv.3.html) and [`tzset`](http://man7.org/linux/man-pages/man3/tzset.3.html) POSIX functions. First, call `setenv` to set `TZ` environment variable to the correct value depending on device location. Format of the time string is described in [libc documentation](https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html). Next, call `tzset` to update C library runtime data for the new time zone. Once these steps are done, `localtime` function will return correct local time, taking time zone offset and daylight saving time into account.
## Additional options
This example can use 3 time synchronization method:
- `update time immediately when received` - update time immediately as received an answer from SNTP server. Allows syncing the time without any additional code from the user side, and use a callback function or getting status for notification of the process of sync.
- `update time with smooth method (adjtime)` - time synchronization will use the adjtime function to smoothly update the time. Allows syncing the time without any additional code from the user side, and use a callback function or getting status for notification of the process of sync.
- `custom implementation` - allows replacing the built-in time synchronization functionality. This option redefines `sntp_sync_time()` function.
## Useful API function:
- `sntp_set_time_sync_notification_cb()` - use this function to set a callback function to notify about the time synchronization process.
- `sntp_get_sync_status()` and `sntp_set_sync_status()` - get/set time synchronization status.
- `sntp_get_sync_mode()` and `sntp_set_sync_mode()` - get/set the sync mode. Allowable two mode: `SNTP_SYNC_MODE_IMMED` and `SNTP_SYNC_MODE_SMOOTH`.
## Mode of update time
`sntp_set_sync_mode()` - Set the sync mode of the system time. It has two mode:
* `SNTP_SYNC_MODE_IMMED` - Update system time immediately when receiving a response from the SNTP server.
* `SNTP_SYNC_MODE_SMOOTH` - Smooth time updating. Time error is gradually reduced using adjtime function. If the difference between SNTP response time and system time is large (more than 35 minutes) then update immediately. This mode uses `adjtime()` function.
## Adjtime()
`int adjtime(const struct timeval *delta, struct timeval *outdelta)`
`adjtime()` is a libc function that is called automatically in "smooth" time update mode, but can also be called from custom time synchronization code.
If the time error is less than 35 minutes then `adjtime` function will start smooth adjusting otherwise the return value is -1.
This function speeds up or slows down the system clock in order to make a gradual adjustment. This ensures that the calendar time reported by the system clock is always monotonically increasing, which might not happen if you simply set the clock. If adjusting the system clock by `adjtime()` is already done during the second call `adjtime()`, and the delta of the second call is not NULL, the earlier tuning is stopped, but the already completed part of the adjustment is not canceled.
The delta argument specifies a relative adjustment to be made to the clock time. If negative, the system clock is slowed down for a while until it has lost this much elapsed time. If positive, the system clock is speeded up for a while.
If the olddelta argument is not a null pointer, the adjtime function returns information about any previous time adjustment that has not yet completed.
The return value is 0 on success and -1 on failure.
To stop the smooth time adjustment, you need to record the current time using the function `settimeofday()`.
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
ESP32C3通过wifi实现SNTP时钟同步 (1283个子文件)
libwpa_supplicant.a 7.13MB
liblwip.a 5.5MB
libdriver.a 5.24MB
liblibsodium.a 4.86MB
libmbedcrypto.a 4.5MB
libcoap.a 3.25MB
libasio.a 2.92MB
libnghttp.a 2.68MB
libhal.a 2.45MB
libfreemodbus.a 2.28MB
libexpat.a 2.15MB
libnvs_flash.a 1.82MB
libmbedtls.a 1.48MB
libspi_flash.a 1.48MB
libesp_hw_support.a 1.42MB
libmdns.a 1.39MB
libesp_system.a 1.35MB
libbootloader_support.a 1.12MB
libfreertos.a 1.1MB
libspiffs.a 1.07MB
libwifi_provisioning.a 1008KB
libfatfs.a 1002KB
libmbedx509.a 874KB
libconsole.a 828KB
libbootloader_support.a 827KB
libcbor.a 751KB
libvfs.a 747KB
libprotobuf-c.a 667KB
libheap.a 662KB
libnewlib.a 655KB
libesp_http_server.a 647KB
libopenssl.a 646KB
libesp_lcd.a 621KB
libprotocomm.a 603KB
libesp_wifi.a 591KB
libmqtt.a 591KB
libesp_netif.a 578KB
libsdmmc.a 550KB
libjson.a 541KB
libtcp_transport.a 537KB
libefuse.a 481KB
libefuse.a 480KB
libwear_levelling.a 462KB
libesp_http_client.a 459KB
libesp_hw_support.a 418KB
libesp-tls.a 417KB
libsoc.a 374KB
libsoc.a 373KB
libesp_event.a 352KB
libesp_local_ctrl.a 323KB
libesp_serial_slave_link.a 314KB
libmicro-ecc.a 314KB
libesp_hid.a 308KB
libunity.a 303KB
libpthread.a 270KB
libesp_eth.a 264KB
libesp_ringbuf.a 244KB
libesp_websocket_client.a 223KB
libesp_gdbstub.a 220KB
libapp_trace.a 210KB
libtcpip_adapter.a 209KB
libespcoredump.a 201KB
libesp_timer.a 197KB
libprotocol_examples_common.a 188KB
libapp_update.a 187KB
libesp_pm.a 167KB
libhal.a 154KB
libesp_https_ota.a 140KB
libesp_adc_cal.a 137KB
libmain.a 124KB
libmbedtls.a 123KB
liblog.a 119KB
liblog.a 111KB
libcxx.a 103KB
libesp_phy.a 102KB
libesp_common.a 69KB
libspi_flash.a 57KB
libriscv.a 44KB
libesp_rom.a 40KB
libesp_rom.a 40KB
libjsmn.a 38KB
libcmock.a 36KB
libmain.a 26KB
libesp_system.a 23KB
libesp_common.a 22KB
libesp_ipc.a 11KB
app-flash_args 70B
sntp.bin 643KB
bootloader.bin 19KB
partition-table.bin 3KB
CMakeDetermineCompilerABI_CXX.bin 1KB
CMakeDetermineCompilerABI_CXX.bin 1KB
CMakeDetermineCompilerABI_C.bin 1KB
CMakeDetermineCompilerABI_C.bin 1KB
.bin_timestamp 88B
.bin_timestamp 71B
bootloader-complete 0B
bootloader-configure 0B
bootloader-done 0B
bootloader-download 0B
共 1283 条
- 1
- 2
- 3
- 4
- 5
- 6
- 13
source03
- 粉丝: 1
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0