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('ntp.aliyun.com')
#获取当前时间
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)
settime |
---|
网络校时 |
sntp - 简单网络时间协议
模块功能: 用于跨广域网或局域网同步时间的协议,具有较高的精确度(几十毫秒)。
注意事项: 需要确保网络连接成功,请使用下面的示例代码连接网络:
settime - 网络校时
函数功能: 网络校时
注意事项: 需确保此网络已经连接
函数原型:
sntp.settime(ntpserver)
参数 | 说明 |
---|---|
无参数 | 表示使用默认服务器校时 |
ntpserver | 填入校时服务器地址 |