1. import ntptime
    2. import log
    3. import utime
    4. import checkNet
    5. '''
    6. 下面两个全局变量是必须有的,用户可以根据自己的实际项目修改下面两个全局变量的值
    7. '''
    8. PROJECT_NAME = "QuecPython_NTP_example"
    9. PROJECT_VERSION = "1.0.0"
    10. checknet = checkNet.CheckNetwork(PROJECT_NAME, PROJECT_VERSION)
    11. # 设置日志输出级别
    12. log.basicConfig(level=log.INFO)
    13. ntp_log = log.getLogger("NtpTime")
    14. if __name__ == '__main__':
    15. stagecode, subcode = checknet.wait_network_connected(30)
    16. if stagecode == 3 and subcode == 1:
    17. ntp_log.info('Network connection successful!')
    18. # 查看默认ntp服务
    19. ntp_log.info(ntptime.host)
    20. # 设置ntp服务
    21. ntptime.sethost('pool.ntp.org')
    22. # 同步ntp服务时间
    23. ntptime.settime()
    24. else:
    25. ntp_log.info('Network connection failed! stagecode = {}, subcode = {}'.format(stagecode, subcode))