时间同步 校准
官方同步库 ntptime
def sync_ntp():
"""通过网络校准时间"""
import ntptime
ntptime.NTP_DELTA = 3155644800 # 可选 UTC+8偏移时间(秒),不设置就是UTC0
ntptime.host = 'ntp1.aliyun.com' # 可选,ntp服务器,默认是"pool.ntp.org" 这里使用阿里服务器
ntptime.settime() # 修改设备时间,到这就已经设置好了
ESP32 模块
ESP32模块RTC的精度存在一定的缺陷,每过7:45h便会有秒级别的误差溢出,所以esp上,官方建议每隔7小时进行一次时间的校准。
处理
# 定时任务:每个7小时重新同步一次时间
from machine import Timer
timer = Timer(1)
timer.init(period=1000 * 60 * 60 * 7, mode=Timer.PERIODIC, callback=sync_ntp)