haas506开发教程-高级组件库-sntp
- 1.网络校时
- 2.Class-sntp
- 3.总结
1.网络校时
- 案例说明
使用sntp进行网络校时 - main.py
import utime as time
import network
import sntp
g_connect_status = False
def on_4g_cb(args):
global g_connect_status
pdp = args[0]
netwk_sta = args[1]
if netwk_sta == 1:
g_connect_status = True
else:
g_connect_status = False
def connect_network():
global net,on_4g_cb,g_connect_status
net = network.NetWorkClient()
g_register_network = False
if net._stagecode is not None and net._stagecode == 3 and net._subcode == 1:
g_register_network = True
else:
g_register_network = False
if g_register_network:
net.on(1,on_4g_cb)
net.connect(None)
else:
print('network register failed')
while True:
if g_connect_status:
print('network register successed')
break
time.sleep_ms(20)
if __name__=='__main__':
#先连上网
connect_network()
#校时
sntp.settime()
#获取当前时间
t=time.localtime()
#获取到的时间格式是 (年,月,日,时,分,秒,周日,年日)
print("当前时间:",t)
#按照一定格式输出时间
#t_time="{:04d}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}".format(t[0],t[1],t[2],t[3],t[4],t[5])
#print("t_time:",t_time)
- 日志输出
network register successed
当前时间: (2021, 11, 25, 2, 33, 36, 4, 328)
2.Class-sntp
sntp
settime
网络校时
- sntp
- sntp.settime()
- 作用:网络校时
- 返回:1成功
3.总结
本节介绍了如何使用haas506的sntp模块,可以使用sntp进行网络校时。