1、编程实现连接路由器,将IP地址等相关信息通过LCD显示。先看构造函数
2、编写相应的程序
import network,time
from machine import SoftI2C,Pin
from tftlcd import LCD32
WHITE = (255,255,255)
BLACK = (0,0,0)
d = LCD32(portrait = 1)
d.fill(BLACK)
#WIFI连接函数
def WIFI_Connect():
WIFI_LED = Pin(46,Pin.OUT)
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
start_time = time.time()
if not wlan.isconnected():
print('connecting to network...')
wlan.connect('01Studio','8888888')
while not wlan.isconnected():
WIFI_LED.value(1)
time.sleep_ms(300)
WIFI_LED.value(0)
time.sleep_ms(300)
if time.time()-start_time > 15:
print('WIFI Connected Timeout!')
break
if wlan.isconnected():
WIFI_LED.value(1)
print('network information:',wlan.ifconfig())
d.printStr('IP/Subnet/GW:',10,100,WHITE,size=1)
d.printStr(wlan.ifconfig()[0],10,130,WHITE,size=1)
d.printStr(wlan.ifconfig()[1],10,170,WHITE,size=1)
d.printStr(wlan.ifconfig()[2],10,200,WHITE,size=1)
WIFI_Connect()
3、执行代码后显示效果:
注意我用的是LCD屏幕,所以程序略有不同。