class WLAN – control built-in WiFi interfaces 类WLAN -控制内置WiFi接口 ==============================================

This class provides a driver for WiFi network processors. Example usage:: 这个类为WiFi网络处理器提供驱动程序。用法例子:

import network
# enable station interface and connect to WiFi access point
# 启用工作站接口并连接到WiFi接入点
nic = network.WLAN(network.STA_IF)
nic.connect('your-ssid', 'your-password')
# now use sockets as usual
# 现在像往常一样使用套接字

Constructors 构造函数 ———— .. class:: WLAN(interface_id)

Create a WLAN network interface object. Supported interfaces are network.STA_IF (station aka client, connects to upstream WiFi access points) and network.AP_IF (access point, allows other WiFi clients to connect). Availability of the methods below depends on interface type. For example, only STA interface may WLAN.connect() to an access point. 创建一个WLAN网络接口对象。支持的接口是 ``network.STA_IF``(站即客户端,连接到上游WiFi接入 点)和``network.AP_IF``(接入点,允许其他WiFi客户端 连接)。下面方法的可用性取决于接口类型。 例如,只有STA接口可以`WLAN.connect()`到一个接入点。

Methods 方法 ——-[is_active])

Activate (“up”) or deactivate (“down”) network interface, if boolean argument is passed. Otherwise, query current state if no argument is provided. Most other methods require active interface. 激活 (“up”) 或停用 (“down”) 网络接口,如果布尔值 参数传递。否则,如果没有参数,查询当前状态 提供。大多数其他方法都需要活动接口。

WLAN.connect(ssid=None, password=None, \*, bssid=None)

Connect to the specified wireless network, using the specified password. If bssid is given then the connection will be restricted to the access-point with that MAC address (the ssid must also be specified in this case). 使用指定的密码连接到指定的无线网络。 如果*bssid*给定,则连接将被限制为 存取点的MAC地址(*ssid*也必须被指定 在这种情况下)。


Disconnect from the currently connected wireless network. 断开当前连接的无线网络。


Scan for the available wireless networks. 扫描可用的无线网络。

Scanning is only possible on STA interface. Returns list of tuples with the information about WiFi access points: 扫描只能在STA接口上进行。返回元组的列表 WiFi接入点信息:

(ssid, bssid, channel, RSSI, authmode, hidden)

bssid is hardware address of an access point, in binary form, returned as bytes object. You can use ubinascii.hexlify() to convert it to ASCII form. 是一个访问点的硬件地址,以二进制形式,返回为 字节对象。可以使用’ ubinascii.hexlify() ‘将其转换为ASCII格式。

There are five values for authmode: authmode有5个值:

  • 0 – open

  • 1 – WEP

  • 2 – WPA-PSK

  • 3 – WPA2-PSK

  • 4 – WPA/WPA2-PSK

and two for hidden: 第二个是隐藏的:

  • 0 – visible

  • 1 – hidden


Return the current status of the wireless connection. 返回无线连接的当前状态。

When called with no argument the return value describes the network link status. The possible statuses are defined as constants: 当不带参数调用时,返回值描述网络链接状态。 可能的状态被定义为常量:

  • STAT_IDLE – no connection and no activity,

  • STAT_IDLE – 没有联系,没有活动,

  • STAT_CONNECTING – connecting in progress,

  • STAT_CONNECTING – 连接在进步,

  • STAT_WRONG_PASSWORD – failed due to incorrect password,

  • STAT_WRONG_PASSWORD – 因密码错误而失败,

  • STAT_NO_AP_FOUND – failed because no access point replied,

  • STAT_NO_AP_FOUND – 失败是因为没有接入点回答,

  • STAT_CONNECT_FAIL – failed due to other problems,

  • STAT_CONNECT_FAIL – 由于其他问题而失败,

  • STAT_GOT_IP – connection successful.

  • STAT_GOT_IP – 连接成功。

When called with one argument param should be a string naming the status parameter to retrieve. Supported parameters in WiFI STA mode are: 'rssi'. 当用一个参数调用时,*param*应该是一个命名状态的字符串 参数检索。WiFI STA模式下支持的参数为:'rssi'


In case of STA mode, returns True if connected to a WiFi access point and has a valid IP address. In AP mode returns True when a station is connected. Returns False otherwise. 在STA模式下,如果连接到WiFi接入,返回``True`` 点,并有一个有效的IP地址。在AP模式下返回True 站连接。返回``False``。

WLAN.ifconfig([(ip, subnet, gateway, dns)])

Get/set IP-level network interface parameters: IP address, subnet mask, gateway and DNS server. When called with no arguments, this method returns a 4-tuple with the above information. To set the above values, pass a 4-tuple with the required information. For example:: 获取/设置IP级网络接口参数:IP地址、子网掩码、 网关和DNS服务器。如果不带参数调用该方法,则返回 一个包含上述信息的4元组。要设置上述值,请传递a 包含所需信息的4元组。例如:

nic.ifconfig(('', '', '', ''))
WLAN.config(param=value, ...)

Get or set general network interface parameters. These methods allow to work with additional parameters beyond standard IP configuration (as dealt with by WLAN.ifconfig()). These include network-specific and hardware-specific parameters. For setting parameters, keyword argument syntax should be used, multiple parameters can be set at once. For querying, parameters name should be quoted as a string, and only one parameter can be queries at time:: 获取或设置一般网络接口参数。这些方法允许工作 使用超出标准IP配置的附加参数(由 WLAN.ifconfig())。这些包括特定于网络的和特定于硬件的 参数。对于设置参数,应该使用关键字参数语法, 可同时设置多个参数。对于查询,参数名称应该 被引用为字符串,在时间只能查询一个参数:

# Set WiFi access point name (formally known as ESSID) and WiFi channel
# 设置WiFi接入点名称(正式名称为ESSID)和WiFi通道
ap.config(essid='My AP', channel=11)
# Query params one by one
# 查询参数一个一个

Following are commonly supported parameters (availability of a specific parameter depends on network technology type, driver, and MicroPython port). 以下是通常支持的参数(特定参数的可用性) 取决于网络技术类型、驱动程序和“MicroPython端口”)。




MAC address (bytes)


WiFi access point name (string)


WiFi channel (integer)


Whether ESSID is hidden (boolean)


Authentication mode supported (enumeration, see module constants)


Access password (string)


The DHCP hostname to use