首页 > 解决方案 > ESP32 上 Python 和 MicroPython 的时间差

问题描述

我正在使用 MicroPython 中的 utime 模块。我注意到在 MicroPython 中的 ESP32 上的 utime.time() 和普通 Python 中的 time.time() 之间转换为纪元时间时存在时间差。我还在使用连接到 ESP32 的 DS3231 RTC 板来让我的时间持久。

这是代码:

from machine import Pin, ADC, reset, RTC, I2C
import DS3231
import utime

#setup DS3231 RTC
i2c = I2C(sda = Pin(22), scl=Pin(21))
ds = DS3231.DS3231(i2c)
#set time
tm = ds.DateTime() # Time from DS3231 which is accurate to my computer time

#ds.DateTime returns this [2020, 4, 15, 3, 12, 42, 4] which is Y,M,D,day of week,H,M,S
tm_tup = (tm[0], tm[1], tm[2], tm[4], tm[5], 0,0,0) #convert to tuple to set ESP32 clock which is 
#supposed to also set utime
RTC().datetime(tm_tup) #sets time on ESP32
utc  = 946684800 + utime.time() # since utime starts at 1/1/2000 and utc starts 1/1/1970
print(tm)  #time from DS3231 RTC
print(utime.time()) 
print(utc) #added to utime.time() to make UTC

ds.DateTime()函数在列表中返回与我的计算机相同的时间。我将这些值重新排列成一个元组以设置RTC.datetime()应该与 utime 模块同步的值。我真正需要的是一个准确的 UTC 时间戳。当地时间没问题。

即使我将 DS3231 设置为计算机上的确切时间,当我检查utime.time()Python 的模块时也会出现差异。time.time()输出如下。我在运行 ESP32 版本后大约 30 秒运行了 Python 脚本。

ESP32 输出

[2020, 4, 15, 3, 15, 46, 49] #Exact date time from my computer
640389600
1587074400

Python 输出

>>> import time
>>> print (time.time())
1586983668.94813

只是好奇为什么会有这样的差异。

标签: pythonesp32micropython

解决方案


推荐阅读