我正在使用MicroPython中的utime模块。我注意到,在转换为历元时间时,MicroPython中ESP32上的utime.time()和普通Python中的time.time()之间存在时间差。我还使用DS3231 RTC板,该板连接到ESP32,以使我的时间持久化
代码如下:
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时间戳。当地时间可以
当我将utime.time()
与Python的time.time()
模块进行比较时,即使我将DS3231设置为计算机上的精确时间,也会出现差异。产出如下。在运行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
只是好奇为什么会有这样的差异
目前没有回答
相关问题 更多 >
编程相关推荐