685 views|1 replies

538

Posts

3

Resources
The OP
 

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

 
The pico-w board has a wireless WiFi module, so it can communicate wirelessly by connecting to an external network. This article refers to the pico-w network connection document ( Connecting-to-the-internet-with-pico-w.pdf (20.43 MB, downloads: 3) ) to connect to the network and obtain the network time.

1. Network connection

The network connection is described in detail in the document (Connecting-to-the-internet-with-pico-w.pdf). The following example implements pico-w to connect to WiFi and prints out the network connection status and IP address through the console.

import network
import time
import rp2

ssid = 'CMCC-eP9M'  # wifi ssid
password = 'XXXX' # WiFi password

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)

# wait for connect or fail
max_wait = 10
while max_wait > 0:
    if wlan.status() < 0 or wlan.status() >= 3:
        break
    max_wait -= 1
    print('waiting for connection...')
    time.sleep(1)
    
# Handle connection error
if wlan.status() != 3:
    raise RuntimeError('network connection failed')
else:
    print('connected')
    status = wlan.ifconfig()
    print('ip = ' + status[0])
    print(rp2.country())

import urequests
r = urequests.get('http://date.jsontest.com')
print(r.json())
# print(r.content)
r.close()
# while not wlan.isconnected() and wlan.status() >= 0:
#     print("Waiting to connect:")
#     time.sleep(1)
# 
# print(wlan.ifconfig())

Running results:

2. Use of RTC

The use of RTC is very simple. You can directly set and get the current RTC time through the datetime method.

Directions:

from machine import RTC

rtc = RTC()
rtc.datetime((2023, 6, 18, 6, 20, 36, 0, 0)) # set a specific date and time
datetime = rtc.datetime() # get date and time

print(datetime)

3. NTP network time synchronization

For ntp time synchronization, you can directly use the built-in ntptime library and import it. The sample code is:

import network
import time
import rp2
import ntptime

ssid = 'CMCC-eP9M'
password = 'y634ybku'

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)

# wait for connect or fail
max_wait = 10
while max_wait > 0:
    if wlan.status() < 0 or wlan.status() >= 3:
        break
    max_wait -= 1
    print('waiting for connection...')
    time.sleep(1)
    
# Handle connection error
if wlan.status() != 3:
    raise RuntimeError('network connection failed')
else:
    print('connected')
    status = wlan.ifconfig()
    print('ip = ' + status[0])
    print(rp2.country())

# 时间同步

print("同步前本地时间:%s" %str(time.localtime()))
ntptime.settime()
ntp_time = ntptime.time() + 8 * 60 * 60 # 时区补偿
print("同步后本地时间:%s" %str(time.localtime(ntp_time)))

Running results:

This post is from DigiKey Technology Zone

Latest reply

The built-in ntptime library is convenient for this   Details Published on 2023-6-22 08:24
 
 

6580

Posts

0

Resources
2
 

The built-in ntptime library is convenient for this

This post is from DigiKey Technology Zone
 
 
 

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