python - 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
只是好奇为什么会有这样的差异。
解决方案
推荐阅读
- kubernetes - 无法部署 Kubernetes Ingress apiVersion network.k8s.io/v1
- javascript - 回调函数和参数
- python - 使用 Backgroundscheduler 定期更新烧瓶中的值
- google-play-games - 如何在关于此游戏的更多信息中添加“使用 google play 游戏”?
- python - 如何使用 boto3 连接到 Amazon Keyspaces?
- typescript - 打字稿:有没有办法在不转换为类类型的情况下显示属性和方法?
- python - 动态 div 样式阻止 BeatifulSoup 使代码持久化
- css - 将 Illustrator 文件的每一层导出为 SVG,保持相对位置和大小
- javascript - 单击反应js中搜索字段中的搜索图标后,选项未填充到下拉列表中
- python - 如何旋转和扩展数据框列