network Module API Manual#
Overview#
This module is primarily used to configure and view network parameters. After configuration is complete, the socket module can be used for network communication.
LAN Class#
Reference documentation: Micropython LAN
This class is the configuration interface for wired networks. Example code is as follows:
import network
nic = network.LAN()
print(nic.ifconfig())
# After configuration, you can use socket as usual
...
Constructor#
class
network.LAN()¶Creates a wired Ethernet object.
Methods#
LAN.active([state]) ¶
Activate or deactivate the network interface. Pass the boolean argument
Trueto activate, andFalseto deactivate. If no argument is passed, the current status is returned.LAN.isconnected() ¶
Returns
Trueto indicate that the network is connected, and returnsFalseto indicate that it is not connected.LAN.ifconfig([(ip, subnet, gateway, dns)]) ¶
Get or set IP-level network interface parameters, including IP address, subnet mask, gateway, and DNS server. When called without arguments, 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) ¶
Get or set network interface parameters. Currently, only setting or getting the MAC address is supported. 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"))
WLAN Class#
Reference documentation: Micropython WLAN
This class provides the WiFi network configuration interface. Example code is as follows:
import network
import time
SSID = "TEST"
PASSWORD = "12345678"
sta = network.WLAN(network.STA_IF)
sta.connect(SSID, PASSWORD)
timeout = 10 # Unit: seconds
start_time = time.time()
while not sta.isconnected():
if time.time() - start_time > timeout:
print("Connection timed out")
break
time.sleep(1) # Please wait a moment before connecting
print(sta.ifconfig())
print(sta.status())
# Disconnecting from the network here is just a test. In actual application, you may not need to disconnect
sta.disconnect()
print("Network disconnected")
print(sta.status())
Constructor#
class
network.WLAN(*interface_id*)Creates a WLAN network interface object. Supported interface types include
network.STA_IF(i.e., station mode, connecting to an upstream WiFi access point) andnetwork.AP_IF(i.e., access point mode, allowing other devices to connect). Different interface types have different methods. For example, only STA mode supports connecting to an access point viaWLAN.connect().
Methods#
WLAN.active()
Queries whether the current interface is active
WLAN.connect(ssid=None, key=None, [info = None])
Connects to the specified
ssidorinfo, whereinfois the result returned byscan.Only available in
StamodeWLAN.disconnect()
In
Stamode, disconnects the current WiFi network connection. InApmode, a specifiedmaccan be passed in to disconnect a device.WLAN.scan()
Scans for available WiFi networks. This method is only valid in STA mode. The returned list contains information about each network, for example:
# 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 information about the current network connection. When no parameter is passed, returns the current connection status. For example:
# Check connection status, equivalent to sta.isconnected() print(sta.status()) # Check the signal quality of the connection print(sta.status("rssi"))
Supported configuration parameters include:
In
Stamoderssi: Connection signal qualityap: Name of the connected hotspot
In
Apmodestations: Returns information about connected devices
WLAN.isconnected()
Returns whether it is connected to a hotspot
Only available in
StamodeWLAN.ifconfig([(ip, subnet, gateway, dns)])
Gets or sets the IP-level network interface parameters. When called without parameters, returns a 4-tuple containing the IP address, subnet mask, gateway, and DNS server; when parameters are passed in, sets these values. For example:
sta.ifconfig(('192.168.0.4', '255.255.255.0', '192.168.0.1', '8.8.8.8'))
WLAN.config(param)
Gets or sets the configuration parameters of the network interface. Use keyword argument syntax when setting parameters; pass the parameter name when querying. For example:
# Check the auto_reconnect configuration print(sta.config('auto_reconnect')) # Set auto reconnect sta.config(auto_reconnect = True)
Supported configuration parameters include:
In
Stamodemac:macaddressauto_reconnect: Whether to auto reconnect
In
Apmodessid: Hotspot name. When this parameter is passed in, it will configure and start the hotspot.keyandsecuritycan be used simultaneously to specify the password and security typekey: Hotspot password. In encryption mode, the length should be 8 to 64 characters; omit this parameter in open modesecurity: Hotspot security type.ssidmust be passed in at the same time when configuring. The current value can also be queried viaap.config('security')info: Current hotspot information, can only be queriedcountry: Country code
Common security types in AP mode are as follows. The actual available types depend on the WiFi driver used by the development board. Unsupported types will cause configuration to fail and return
False.SECURITY_OPEN: Open hotspot, no password usedSECURITY_WPA_TKIP_PSK: WPA-PSK (TKIP)SECURITY_WPA2_AES_PSK: WPA2-PSK (AES), recommendedSECURITY_WPA2_MIXED_PSK: WPA2-PSK (AES/TKIP mixed mode)
Create a WPA2 encrypted hotspot:
import network ap = network.WLAN(network.AP_IF) result = ap.config( ssid='CanMV_AP', key='12345678', security=ap.SECURITY_WPA2_AES_PSK, ) print(result) print(ap.config('security'))
When creating an open hotspot, omit
keyand set the security type toSECURITY_OPEN:ap.config(ssid='CanMV_Open', security=ap.SECURITY_OPEN)
When
securityis not specified, passing inkeydefaults toSECURITY_WPA2_AES_PSK; whenkeyis not passed in, it defaults toSECURITY_OPEN.WLAN.stop()
Stops starting the hotspot
Only available in
ApmodeWLAN.info()
Queries the current hotspot information
Only available in
Apmode
