507|0

7063

帖子

11

TA的资源

版主

楼主
 

【得捷电子Follow me第3期】任务4 联网日历 [复制链接]

 

前面获取的是纽约的时期,这里向大家展示获取东八区的ntp时间,并在OLED屏上动态显示当前时间,代码如下:

import machine 
import time
from machine import Pin, SoftI2C, RTC
import ssd1306py as lcd
from time import sleep
import time
import network
import urequests
import ujson
import ntptime
i2c = SoftI2C(scl=Pin(7), sda=Pin(6))  # Adjust the Pin numbers based on your connections
lcd.init_i2c(7,6,128,64)
lcd.clear()
lcd.text('NtpTime', 0, 16, 32)
lcd.show()

station = network.WLAN(network.STA_IF)
station.active(True)

# Network settings
wifi_ssid = "SSID"
wifi_password = "pwd@"

authmodes = ['Open', 'WEP', 'WPA-PSK' 'WPA2-PSK4', 'WPA/WPA2-PSK']
for (ssid, bssid, channel, RSSI, authmode, hidden) in station.scan():
    print("* {:s}".format(ssid))
    print("   - Channel: {}".format(channel))
    print("   - RSSI: {}".format(RSSI))
    print("   - BSSID: {:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}".format(*bssid))
    print()

# Continually try to connect to WiFi access point
while not station.isconnected():
    # Try to connect to WiFi access point
    print("Connecting...")
    station.connect(wifi_ssid, wifi_password)
    time.sleep(10)

# Display connection details
print("Connected!")
print("My IP Address:", station.ifconfig()[0])


while True:   #时间校准
    try:
        print('time ing')
        ntptime.settime()
        print('time ok')
        break;
    except:
        print('time no')
        time.sleep(1)

while True:
    lcd.clear()
    lcd.text('Ntp Time', 0, 0, 32)
    mytime=time.localtime()
    mydata='%04d-%02d-%02d'%(mytime[0],mytime[1],mytime[2])
    thistime='%02d:%02d:%02d'%(mytime[3]+8,mytime[4],mytime[5])
    print(mydata)
    print(thistime)
    lcd.text(mydata,20,32,16)
    
    lcd.text(thistime,30,48,16)
    lcd.show()
    time.sleep(1)




显示效果:

 

NTP时间

 

点赞 关注
 
 

回复
举报
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/6 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表