2.2 Network
Module API Manual#
1. Overview#
This module is primarily used to configure and view network parameters. Once configured, the socket
module can be used for network communication.
2. LAN
Class#
Reference documentation: Micropython LAN
This class serves as the interface for configuring wired networks. Example code is as follows:
import network
nic = network.LAN()
print(nic.ifconfig())
# Once configured, sockets can be used as usual
...
2.1 Constructor#
class
network.LAN()
¶Creates a wired Ethernet object.
2.2 Methods#
LAN.active([state]) ¶
Activates or deactivates the network interface. Pass a boolean parameter
True
to activate,False
to deactivate. If no parameter is passed, it returns the current state.LAN.isconnected() ¶
Returns
True
if connected to the network,False
otherwise.LAN.ifconfig([(ip, subnet, gateway, dns)]) ¶
Gets or sets the network interface parameters at the IP level, including IP address, subnet mask, gateway, and DNS server. When called without parameters, it returns a tuple containing the above information; to set parameters, pass a tuple containing the IP address, subnet mask, gateway, and DNS. For example:
nic.ifconfig(('192.168.0.4', '255.255.255.0', '192.168.0.1', '8.8.8.8'))
LAN.config(config_parameters) ¶
Gets or sets network interface parameters. Currently, only the MAC address can be set or retrieved. For example:
import network lan = network.LAN() # Set MAC address lan.config(mac="42:EA:D0:C2:0D:83") # Get MAC address print(lan.config("mac"))
3. WLAN
Class#
Reference documentation: Micropython WLAN
This class serves as the interface for configuring WiFi networks. Example code is as follows:
import network
# Enable STA mode and connect to WiFi access point
nic = network.WLAN(network.STA_IF)
nic.active(True)
nic.connect('your-ssid', 'your-password')
# Once configured, sockets can be used as usual
3.1 Constructor#
class
network.WLAN(*interface_id*)
Creates a WLAN network interface object. Supported interface types include
network.STA_IF
(station mode, connecting to an upstream WiFi access point) andnetwork.AP_IF
(access point mode, allowing other devices to connect). Methods differ based on the interface type; for example, only STA mode supports connecting to an access point usingWLAN.connect()
.
3.2 Methods#
WLAN.active()
Queries whether the current interface is active.
WLAN.connect(ssid=None, key=None, [info=None])
Connects to the specified
ssid
orinfo
, whereinfo
is the result returned byscan
.Only available in
STA
mode.WLAN.disconnect()
In
STA
mode, disconnects from the current WiFi network. InAP
mode, a specificmac
can be passed to disconnect a device.WLAN.scan()
Scans for available WiFi networks. This method is only valid in STA mode and returns a list containing information about each network, such as:
# print(sta.scan()) [{"ssid":"XCTech", "bssid":xxxxxxxxx, "channel":3, "rssi":-76, "security":"SECURITY_WPA_WPA2_MIXED_PSK", "band":"2.4G", "hidden":0},...]
WLAN.status([param])
Returns the current network connection status. When no parameter is passed, it returns detailed connection information, including BSSID, frequency, SSID, encryption type, IP address, etc. For example:
In
STA
mode:rssi
: Signal strength of the connection.ap
: Name of the connected hotspot.
In
AP
mode:stations
: Returns information about connected devices.
WLAN.isconnected()
Returns whether the device is connected to a hotspot.
Only available in
STA
mode.WLAN.ifconfig([(ip, subnet, gateway, dns)])
Gets or sets the network interface parameters at the IP level. When called without parameters, it returns a tuple containing the IP address, subnet mask, gateway, and DNS server; passing parameters sets these values. For example:
nic.ifconfig(('192.168.0.4', '255.255.255.0', '192.168.0.1', '8.8.8.8'))
WLAN.config(param)
Gets or sets the network interface configuration parameters. Supported parameters include MAC address, SSID, WiFi channel, whether to hide SSID, password, etc. Use keyword argument syntax to set parameters; to query a parameter, pass the parameter name. For example:
Supported configuration parameters include:
In
STA
mode:mac
: MAC address.auto_reconnect
: Whether to automatically reconnect.
In
AP
mode:info
: Current hotspot information (read-only).country
: Country code.
WLAN.stop()
Stops the hotspot.
Only available in
AP
mode.WLAN.info()
Queries current hotspot information.
Only available in
AP
mode.