761 views|4 replies

12

Posts

0

Resources
The OP
 

[Digi-Key Follow me Issue 1] Task 3: Synchronize network time [Copy link]

 

This post contains information about connecting to the network and synchronizing network time. The code is an example of live streaming.

Connect to the Internet

Connect to the network first

import network
import time
from machine import RTC
import ntptime
import urequests


wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect('wifi/热点名称', 'wifi/热点密码')

max_wait = 10
while max_wait > 0:
    if wlan.status() < 0 or wlan.status() >= 3:
        break
    max_wait -= 1
    print('等待连接')
    time.sleep(1)

if wlan.status() != 3:
    raise RuntimeError('联网失败')
else:
    print('网络已连接')
    status = wlan.ifconfig()
    print( 'ip = ' + status[0] )


r = urequests.get("https://www.digikey.cn/zh")
print(r.status_code)
print(r.headers)
r.close()

After running,

will appear

At this point the board has been successfully connected to the network.

Synchronize network time

After successfully connecting to the network, you can start synchronizing the network time.

import network
import time
from machine import RTC
import ntptime
import urequests


wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect('wifi/热点名称', 'wifi/热点密码')

max_wait = 10
while max_wait > 0:
    if wlan.status() < 0 or wlan.status() >= 3:
        break
    max_wait -= 1
    print('等待连接')
    time.sleep(1)

if wlan.status() != 3:
    raise RuntimeError('联网失败')
else:
    print('网络已连接')
    status = wlan.ifconfig()
    print( 'ip = ' + status[0] )

def show_local_time(timezone=8):
    rtc = RTC()
    now = time.time()
    now += timezone * 3600
    t = time.localtime(now)
    print(f'{t[0]}-{t[1]:02d}-{t[2]:02d} {t[3]:02d}:{t[4]:02d}:{t[5]:02d}')

rtc = RTC()
rtc.datetime((2020, 1, 1, 3, 9, 0, 0, 0))
print('校时前系统时间:')
show_local_time()
print('开始NTP校时. . .')
ntptime.host = 'cn.pool.ntp.org'
ntptime.settime()
print(f'校时后系统时间:')
show_local_time()

After running, it will appear

At this point, the network time synchronization has been successfully completed.

This post is from DigiKey Technology Zone

Latest reply

Congratulations on completing your homework: synchronizing network time. It seems to be quite easy.   Details Published on 2023-7-1 09:05
 
 

6748

Posts

2

Resources
2
 
Synchronizing the network clock means getting data from a server, right?
This post is from DigiKey Technology Zone

Comments

Yes, yes.  Details Published on 2023-7-1 00:15
 
 
 

12

Posts

0

Resources
3
 
wangerxian posted on 2023-6-30 16:14 Synchronizing the network clock means getting data from a server, right?

Yes, yes.

This post is from DigiKey Technology Zone
 
 
 

6822

Posts

11

Resources
4
 

Congratulations on completing your homework: synchronizing network time. It seems to be quite easy.

This post is from DigiKey Technology Zone

Comments

Congratulations, it is really easy to follow the live teacher.  Details Published on 2023-7-3 19:04
 
 
 

12

Posts

0

Resources
5
 
lugl4313820 posted on 2023-7-1 09:05 Congratulations on completing the assignment: synchronizing network time. It seems to be quite easy.

Congratulations, it is really easy to follow the live teacher.

This post is from DigiKey Technology Zone
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list