鹹魚ESP32實例—在屏上顯示wifi信息
我們之前通過esp32獲取了wifi信息,在屏上查看比較方便。後面我們還會用更好的屏做更多的功能。
ZTWF開發板
0.96oled屏幕
示例程序
import network,time
from machine import I2C,Pin
from ssd1306 import SSD1306_I2C
#初始化相關模塊
i2c = I2C(sda=Pin(26), scl=Pin(25))
oled = SSD1306_I2C(128, 64, i2c, addr=0x3c)
def WIFI(uid,password): #wifi賬號和密碼
led=Pin(2,Pin.OUT)
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
stime=time.time()
if not wifi.isconnected():
print('~~~~~~~')
wifi.connect(uid,password)
while not wifi.isconnected(): #檢測狀態是燈光閃爍
led.value(1)
time.sleep_ms(300)
led.value(0)
time.sleep_ms(300)
if time.time()-stime>15: #連接超時就跳出去,不然會無限循環
print('timeout')
break
if wifi.isconnected():
led.value(1)
print(wifi.ifconfig()) #打印4條信息
oled.text('Wifi Information',0,0)
oled.text(wifi.ifconfig()[0],0,20) #截取信息
oled.text(wifi.ifconfig()[1],0,36)
oled.text(wifi.ifconfig()[2],0,54)
oled.show()
WIFI('賬號','****密碼')