【得捷电子Follow me第1期】-3- 同步网络时间
[复制链接]
本帖最后由 慕容雪花 于 2023-6-6 10:34 编辑
本文主要介绍
- 树莓派PICO如何联网
- 通过网络获取网络时间
- 如何通过OLED把联网时间显示出来
一、树莓派PICO如何联网
比较重要的两个库是time和network,要在代码中首先import过来:
import time
import network
接下来要选择PICO-W的联网模式,STA_IF表示PICO-W作为station,连接到附近的AP(access point)也就是家里的路由器。
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
access_points = wlan.scan()
for AP in access_points:
print(AP)
wlan.connect("TP-LINK_xxx", "xxxx")
while not wlan.isconnected() and wlan.status() >= 0:
print("Waiting to connect:")
led.toggle()
time.sleep(1)
led.off()
print(wlan.isconnected())
print("Pico-W IP addr is: " + wlan.ifconfig()[0])
联网成功后,可以在控制台看到相应的IP地址等信息。
(b'WaKuWaKu', b'\x00K\xf3n\xc5\x8f', 8, -71, 7, 2)
(b'ComeOn', b'\x14\xe9\xb27\x84M', 1, -88, 7, 1)
True
Pico-W IP addr is: 192.168.0.101
二、如何获取网络时间
micropython有一个开源的获取网络时间的库ntptime.py。这个库在Github上有源代码,可以拷贝到本地然后重命名即可在自己对代码中作为库来用。注意本文选用了aliyun的服务器,比较稳定。
import utime
try:
import usocket as socket
except:
import socket
try:
import ustruct as struct
except:
import struct
# The NTP host can be configured at runtime by doing: ntptime.host = 'myhost.org'
# host = "pool.ntp.org"
host = 'ntp1.aliyun.com'
# The NTP socket timeout can be configured at runtime by doing: ntptime.timeout = 2
timeout = 1
def time_v2():
NTP_QUERY = bytearray(48)
NTP_QUERY[0] = 0x1B
addr = socket.getaddrinfo(host, 123)[0][-1]
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
s.settimeout(timeout)
res = s.sendto(NTP_QUERY, addr)
msg = s.recv(48)
finally:
s.close()
val = struct.unpack("!I", msg[40:44])[0]
EPOCH_YEAR = utime.gmtime(0)[0]
if EPOCH_YEAR == 2000:
# (date(2000, 1, 1) - date(1900, 1, 1)).days * 24*60*60
NTP_DELTA = 3155673600
elif EPOCH_YEAR == 1970:
# (date(1970, 1, 1) - date(1900, 1, 1)).days * 24*60*60
NTP_DELTA = 2208988800
else:
raise Exception("Unsupported epoch: {}".format(EPOCH_YEAR))
return val - NTP_DELTA + 8*3600
# There's currently no timezone support in MicroPython, and the RTC is set in UTC time.
def settime():
print("Inside settime, using Host: %s", host)
t = time_v2()
import machine
tm = utime.gmtime(t)
machine.RTC().datetime((tm[0], tm[1], tm[2], tm[6] + 1, tm[3], tm[4], tm[5], 0))
在程序中,还要定一个获取时间点函数:
def get_time():
print("同步時間")
print()
print("同步前本地時間:%s" %str(time.localtime()))
print("##")
ntptimeVer2.host = 'ntp1.aliyun.com'
ntptimeVer2.settime() # 修改設備時間
print("同步后本地時間:%s" %str(time.localtime()))
print("##################################################")
rtc = RTC()
print(rtc.datetime())
mytime = rtc.datetime()
mytime2='%d-%d-%d %s %d:%d:%d'%(mytime[0],mytime[1],mytime[2],week[mytime[3]], mytime[4],mytime[5],mytime[6])
print(mytime2)
控制台输出窗口数据:
同步時間
同步前本地時間:(2023, 6, 6, 10, 7, 36, 1, 157)
##
Inside settime, using Host: %s ntp1.aliyun.com
同步后本地時間:(2023, 6, 6, 10, 7, 37, 1, 157)
##################################################
(2023, 6, 6, 1, 10, 7, 37, 0)
2023-6-6 Tue 10:7:37
2023-06-06 Tue 10:07:37
2023-06-06 Tue 10:07:38
2023-06-06 Tue 10:07:39
2023-06-06 Tue 10:07:40
2023-06-06 Tue 10:07:41
2023-06-06 Tue 10:07:42
2023-06-06 Tue 10:07:43
2023-06-06 Tue 10:07:44
2023-06-06 Tue 10:07:45
三、如何通过OLED把时间显示在屏幕上
有了上篇文章中介绍的驱动OLED屏幕的方法,把时间显示在SSD1315屏幕上是非常简单的事情了。
while True:
mytime = RTC().datetime()
mytime2='%d-%02d-%02d %s %02d:%02d:%02d'%(mytime[0],mytime[1],mytime[2],week[mytime[3]], mytime[4],mytime[5],mytime[6])
print(mytime2)
oled.fill(0)
oled.text("using ntptime", 0, 0)
oled.text("Time: UTC+8", 0, 16)
oled.text(mytime2[:14], 0, 32)
oled.text(mytime2[15:], 0, 48)
oled.show()
led.toggle()
time.sleep(1)
最终的效果如下所示,可以看到电脑上的时间与OLED屏幕上的时间是一样的。
a9dcfa68748a0a1002bf59153a19873c
|