当前位置:网站首页>Haas506 2.0 development tutorial -sntp (only versions above 2.2 are supported)

Haas506 2.0 development tutorial -sntp (only versions above 2.2 are supported)

2022-06-23 06:38:00 Zhiyunfu

haas506 2.0 Development tutorial -sntp

1. Network timing

Case description

  • Use sntp Conduct network timing , Get server time .
  • network connections ( Insert SIM card )
  • Parameters can be added to the new version , No parameter is to use the default server .

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 to the Internet first 
    connect_network()
    # Timing 
    sntp.settime('ntp.aliyun.com')
    # Get the current time 
    t=time.localtime()
    # The obtained time format is  ( year , month , Japan , when , branch , second , Sunday , Year day )
    print(" current time :",t)
    # Output time in a certain format 
    #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)
  • Output :
network register successed
 current time : (2021, 11, 25, 2, 33, 36, 4, 328)

2.Class-sntp

settime
Network timing

sntp - Simple network time protocol

  • Module function : A protocol for synchronizing time across a wide area network or local area network , High accuracy ( Tens of milliseconds ).

  • matters needing attention : You need to ensure that the network connection is successful , Please use the following example code to connect to the network :

settime - Network timing

  • The functionality : Network timing

  • matters needing attention : Make sure this network is connected

  • The function prototype :

sntp.settime(ntpserver)

  • Parameter description :
Parameters explain
No parameter Indicates that the default server is used for timing
ntpserver Fill in the time server address
  • Return value : 1 success ,0 Failure
原网站

版权声明
本文为[Zhiyunfu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230512037272.html