【mircopython】esp32网络AP和STA模式使用

文章介绍了如何在Micropython环境下配置ESP32的AP和STA模式,包括设置SSID、密码和认证模式。此外,还提到了在网络连接状态下,如何使用upip模块来安装额外的库,如micropython-ulogger。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

AP模式的使用

    ap = network.WLAN(network.AP_IF)
    ap.config(essid='esp32',password='12345678',authmode=network.AUTH_WPA_WPA2_PSK)
    ap.active(True)   

其中,config可以配置的参数如下(谁能想到micropython官网的提供的参数是错的,害我找了好半天,xox):

mac

MAC address (bytes)

essid

WiFi access point name (string)

channel

WiFi channel (integer)

hidden

Whether ESSID is hidden (boolean)

authmode

Authentication mode supported (enumeration, see module constants)

password

Access password (string)

STA的使用:

wlan = network.WLAN(network.STA_IF) # create station interface
wlan.active(True)       # activate the interface
wlan.scan()             # scan for access points
wlan.isconnected()      # check if the station is connected to an AP
wlan.connect('ssid', 'key') # connect to an AP

官方给了个推荐写法:

def do_connect():
    import network
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    if not wlan.isconnected():
        print('connecting to network...')
        wlan.connect('ssid', 'key')
        while not wlan.isconnected():
            pass
    print('network config:', wlan.ifconfig())

--ps顺便将记录下有网的情况下,可以使用upip进行安装相关库:

import upip
upip.install("micropython-ulogger")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值