Reach a server

SNTP time sync

Where time comes from when there is no RTC chip. Logs and TLS depend on it.

Reach a server Time syncSNTPTLSRTC ESP32 Wi-Fi parts

Official path: examples/protocols/sntp

01 Overview

SNTP (Simple Network Time Protocol) lets the ESP32 fetch accurate UTC time over Wi-Fi after connecting, eliminating the need for an external RTC chip.

Use when: timestamps, TLS certificate validation, scheduled jobs, data logging, or any application requiring accurate time.

Skip when: fully offline calendar accuracy is required (add a battery-backed RTC).

How it works: The ESP32 connects to an NTP server, sends a request, and parses the response to obtain the current time.

Advantages: No extra hardware, low cost, high accuracy (typically within milliseconds).

Caveat: Depends on network connectivity; time cannot be updated offline, so plan for offline scenarios.

02 Hardware

  • MCU: ESP32 series (e.g., ESP32, ESP32-S3), with built-in Wi-Fi.
  • Crystal: Use an external 40 MHz crystal; accuracy is sufficient for SNTP.
  • RTC option: Optional external RTC (e.g., DS3231) to maintain time across power loss, but not required.
  • Power: Ensure stable voltage for Wi-Fi operation to avoid RF interference.
  • Antenna: Ensure proper antenna connection for reliable network connectivity.
  • Layout: Place crystal close to the chip with short traces to avoid high-frequency interference.
  • Connectors: If using an external RTC, reserve an I2C interface.

03 Software flow

1. Initialize NVS and Wi-Fi, then connect to the network.

2. Configure SNTP server addresses (e.g., pool.ntp.org) and start the SNTP service.

3. Wait for time synchronization to complete (poll with esp_sntp_get_time() or use event callbacks).

4. Set the timezone (e.g., via setenv("TZ", "CST-8", 1) and tzset()).

5. Only after time is valid, proceed with TLS connections or scheduled tasks.

6. Implement retry on failure; never use unsynchronized time for business logic.

7. Periodically resync to correct drift.

04 Core points

  • TLS failures are sometimes a clock, not a certificate. Certificate validation depends on current time; wrong time causes TLS handshake failures.
  • Custom work: RTC+SNTP mix, timezone, offline policy.
  • Note: SNTP uses UDP port 123; ensure network allows it.
  • Don't copy SNTP code from other chips directly; adapt to ESP-IDF APIs.
  • After sync, update the system time to ensure accurate log timestamps.

中文

连上服务器

SNTP 对时

没有 RTC 芯片时,时间从网上来。日志和证书都靠它。

连上服务器 对时SNTPTLSRTC ESP32 Wi-Fi parts

官方路径: examples/protocols/sntp

01 项目概述

SNTP(简单网络时间协议)让 ESP32 在联网后自动获取准确的 UTC 时间,无需外部 RTC 芯片。

适用场景: 日志时间戳、TLS 证书校验、定时任务、数据记录等需要准确时间的应用。

不适用场景: 完全离线且必须保持准确年历(此时应添加带电池的 RTC 芯片)。

工作原理: ESP32 通过 Wi-Fi 连接 NTP 服务器,发送请求并解析响应,得到当前时间。

优势: 无需额外硬件,成本低,时间精度高(通常毫秒级)。

注意: 依赖网络连接,断网时无法更新时间,需考虑离线策略。

02 项目硬件描述

  • MCU: ESP32 系列(如 ESP32、ESP32-S3 等),内置 Wi-Fi。
  • 晶振: 使用外部 40MHz 晶振,精度足够满足 SNTP 需求。
  • RTC 选项: 可选外部 RTC 芯片(如 DS3231)以在断电后保持时间,但非必需。
  • 电源: 确保 Wi-Fi 工作电压稳定,避免射频干扰。
  • 天线: 确保 Wi-Fi 天线连接良好,以获得稳定网络连接。
  • 布局: 晶振靠近芯片,走线短,避免高频干扰。
  • 连接器: 若使用外部 RTC,需预留 I2C 接口。

03 项目软件流程描述

1. 初始化 NVS 和 Wi-Fi,连接网络。

2. 配置 SNTP 服务器地址(如 pool.ntp.org),并启动 SNTP 服务。

3. 等待时间同步完成(可通过 esp_sntp_get_time() 轮询或事件回调)。

4. 设置时区(如通过 setenv("TZ", "CST-8", 1) 和 tzset())。

5. 在时间有效后,再进行 TLS 连接或定时任务。

6. 实现失败重试机制,避免使用未同步的时间。

7. 可定期重新同步以校正漂移。

04 项目核心点

  • TLS 失败有时是时间错,不是证书错。 证书校验依赖当前时间,时间不对会导致 TLS 握手失败。
  • 可定制: RTC 与 SNTP 混合、时区、离线策略。
  • 注意: SNTP 使用 UDP 端口 123,需确保网络允许。
  • 不要直接复制其他芯片的 SNTP 代码, 需适配 ESP-IDF 的 API。
  • 时间同步后应更新系统时间, 否则日志时间戳可能不准确。