633 views|0 replies

188

Posts

0

Resources
The OP
 

【Digi-Key Follow me Issue 1】Task 3 Network Time Synchronization [Copy link]

 

First, let's connect to the network.

You need to wait for a while when you connect for the first time after booting up. If the connection is established, the IP address will be printed, as shown in the figure below.

The code is as follows:

import network
import time

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect('---', '---')
while not wlan.isconnected() and wlan.status() >= 0:
    print("Waiting to connect:")
time.sleep(1)
print(wlan.ifconfig())

Second: Get the time through the network and display it

实验五

The code is as follows:

import network
import time
from time import sleep
from machine import RTC
import ntptime
from machine import Pin, I2C
from SSD1306 import SSD1306_I2C
import framebuf
# import ufont

WIDTH  = 128                                            # oled display width
HEIGHT = 64                                             # oled display height

i2c = I2C(1)     # Init I2C using I2C0 defaults, SCL=Pin(GP9), SDA=Pin(GP8), freq=400000



oled = SSD1306_I2C(WIDTH, HEIGHT, i2c)                  # Init oled display
ssid='xxxxx'
passwd='xxxxxx'

wlan = None
max_wait = 10

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

# 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)
    
if wlan.status() != 3:
    raise RuntimeError('network connection failed')
else:
    print('connected')
    status = wlan.ifconfig()
    print('ip = ' + status[0])

def sync_ntp():
     ntptime.NTP_DELTA = 3155644800   # 可选 UTC+8偏移时间(秒),不设置就是UTC0
     ntptime.host = 'ntp1.aliyun.com'  # 可选,ntp服务器,默认是"pool.ntp.org"
     ntptime.settime()   # 修改设备时间,到这就已经设置好了



oled.fill(0)  # clear
oled.text("NTP Time:", 0, 0)

oled.show()

while True:
    rtc = RTC()
    print(rtc.datetime())
    oled.fill(0)  # clear
    oled.text("NTP Time:", 0, 0)
    #s = ','.join(str(i) for i in rtc.datetime())
    s = str(rtc.datetime()[0]) + '-' + str(rtc.datetime()[1]) + '-' + str(rtc.datetime()[2])
    oled.text(s, 10, 16)
    s = str(rtc.datetime()[4]) + '-' + str(rtc.datetime()[5]) + '-' + str(rtc.datetime()[6])
    oled.text(s, 10, 32)
    s = "IP:" + str(status[0])
    oled.text(s, 0, 48)
    oled.show()
    sleep(1)

postscript:

When SSD1306 imported the library at the beginning, it could not be recognized, which caused it to fail to compile. However, after reinstalling the system several times, it was finally able to compile. It may be because it was installed once through the software--tools--package management. I am just guessing and have never figured it out. If any netizens can help me, please leave a message, thank you.

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