micropython连接到wifi上面
import network,time,ujson,urequests,ntptime
from machine import RTC
nic=network.WLAN(network.STA_IF)
nic.active(True)
#nic.connect('ZCWH','00004157')
nic.connect('028sd.com','88884444')
nic.connect('028sd.com','88884444')
connecti=10
while not(nic.isconnected() or connecti==0):
print("正在连接",connecti)
time.sleep(1)
connecti-=1
if(nic.isconnected()):
print("连接成功")
gettext=urequests.get('http://apikey.net')
#print(gettext.text[1:])
parsed = ujson.loads(gettext.text[1:])
#处理IP地址和时间
IPadd=parsed['data']['IP_Address']
nowtime=parsed['data']['Current-Time']
rtc = RTC()
rtcdate=list(map(int, str.split(str.split(nowtime,' ')[0],'-')))
rtctime=list(map(int, str.split(str.split(nowtime,' ')[1],':')))
print(IPadd)
print(nowtime)
print(rtcdate+[5]+rtctime+[0])
rtc.datetime((rtcdate+[5]+rtctime+[0])) # set a specific date and time
print(rtc.datetime())
exec(open('./oled.py').read(),globals())
else:
print("连接超时..")我写的比较多一些。就是连接到wifi后从网络读取时间写到rtc里面然后在切换到oled上面显示出来 。




支付宝打赏
微信打赏 