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 is 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())
# The disconnection here is only a test. The actual application does 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’s connection.WLAN.scan()
Scans for available WiFi networks. This method is only valid in STA mode, and 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:
# View connection status, equivalent to sta.isconnected() print(sta.status()) # View the signal quality of the connection print(sta.status("rssi"))
The supported configuration parameters include:
In
Stamoderssi: Connection signal qualityap: The 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 IP-level network interface parameters. When called without parameters, returns a four-tuple containing the IP address, subnet mask, gateway, and DNS server; when parameters are passed, 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 configuration parameters of the network interface. Supported parameters include MAC address, SSID, WiFi channel, whether to hide SSID, password, etc. Use keyword argument syntax when setting parameters; when querying parameters, simply pass the parameter name. For example:
# View auto_reconnect configuration print(sta.config('auto_reconnect')) # Set auto-reconnect sta.config(auto_reconnect = True)
The supported configuration parameters include:
In
Stamodemac:macaddressauto_reconnect: Whether to auto-reconnect
In
Apmodeinfo: Current hotspot information, can only be queriedcountry: Country code
WLAN.stop()
Stops enabling the hotspot
Only available in
ApmodeWLAN.info()
Queries current hotspot information
Only available in
Apmode
