• haas506 2.0开发教程-sntp(仅支持2.2以上版本)


    haas506 2.0开发教程-sntp

    1.网络校时

    案例说明

    • 使用sntp进行网络校时,获取服务器时间。
    • 网络连接(插入SIM卡)
    • 新版本校时可以添加参数,无参数是使用默认服务器。

    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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 输出:
    network register successed
    当前时间: (2021, 11, 25, 2, 33, 36, 4, 328)
    
    • 1
    • 2

    2.Class-sntp

    settime
    网络校时

    sntp - 简单网络时间协议

    • 模块功能: 用于跨广域网或局域网同步时间的协议,具有较高的精确度(几十毫秒)。

    • 注意事项: 需要确保网络连接成功,请使用下面的示例代码连接网络:

    settime - 网络校时

    • 函数功能: 网络校时

    • 注意事项: 需确保此网络已经连接

    • 函数原型:

    sntp.settime(ntpserver)

    • 参数说明:
    参数说明
    无参数表示使用默认服务器校时
    ntpserver填入校时服务器地址
    • 返回值: 1成功,0失败
  • 相关阅读:
    Could not find androidx.camera:camera-view
    Linux之shell条件测试
    vue3学习(十三)---全局定义事件及编写vue插件
    PRT(Precomputed Radiance Transfer【2002】)原理实现
    学C的第一天(初识C)
    pdf文件过大如何缩小上传?pdf压缩跟我学
    面试中的自我激励:如何展示你的内在驱动力
    怎么将图片旋转45度?
    css样式在弹性盒元素在必要的时候拆行
    深度学习零基础学习之路——第四章 UNet-Family中Unet、Unet++和Unet3+的简介
  • 原文地址:https://blog.csdn.net/w_hizyf_m/article/details/125410243