Network
Module API Manual#
Overview#
This module is primarily used to configure and view network parameters. Once configured, the socket
module can be used for network communication.
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
...
Constructor#
class
network.LAN()
¶Creates a wired Ethernet object.
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"))
WLAN
Class#
Reference documentation: Micropython WLAN
This class serves as the interface for configuring WiFi networks. Example code is as follows:
import network
def sta_test():
sta = network.WLAN(0)
if not sta.active():
sta.active(True)
# Check if STA is active
print(sta.active())
# Check STA status
print(sta.status())
# STA connects to AP
print(sta.connect("Canaan", "Canaan314"))
# Check status
print(sta.status())
# View IP configuration
print(sta.ifconfig())
# Check if connected
print(sta.isconnected())
# Disconnect
print(sta.disconnect())
# Connect to AP
print(sta.connect("Canaan", "Canaan314"))
# Check status
print(sta.status())
sta_test()
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()
.
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 parameters are passed, return 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
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:
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 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:
# View 'auto_reconnect' configuration print(sta.config('auto_reconnect')) # Set up automatic reconnection sta.config(auto_reconnect = True)
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.