本站改版新增arduino频道
micropython esp32c3 实现连接路由器,将IP地址等相关信息显示(只支持2.4G网络)
#导入相关模块
import network,time
from machine import SoftI2C,Pin
#WIFI连接函数
def WIFI_Connect():
WIFI_LED=Pin(2, Pin.OUT) #初始化WIFI指示灯
wlan = network.WLAN(network.STA_IF) #STA模式
wlan.active(True) #激活接口
start_time=time.time() #记录时间做超时判断
if not wlan.isconnected():
print('connecting to network...')
wlan.connect('NBWIFI', 'z7758521') #输入WIFI账号密码
while not wlan.isconnected():
#LED闪烁提示
WIFI_LED.value(1)
time.sleep_ms(300)
WIFI_LED.value(0)
time.sleep_ms(300)
#超时判断,15秒没连接成功判定为超时
if time.time()-start_time > 15 :
print('WIFI Connected Timeout!')
break
if wlan.isconnected():
#LED点亮
WIFI_LED.value(1)
#串口打印信息
print('network information:', wlan.ifconfig())
#数据显示
print('IP/Subnet/GW:')
print(wlan.ifconfig()[0])
print(wlan.ifconfig()[1])
print(wlan.ifconfig()[2])
#执行WIFI连接函数
WIFI_Connect()
Copyright © 2014 ESP56.com All Rights Reserved
晋ICP备14006235号-22 晋公网安备14108102001165号
执行时间: 0.0095310211181641 seconds