network — Network Configuration

This module provides network drivers and routing configuration. Hardware-specific network drivers are provided in this module for configuring hardware network interfaces. Then, network services provided by configured interfaces can be used through the usocket module.

Functions

network.phy_mode([mode])

Set the PHY mode. Defined mode constants are as follows:

  • mode

    • MODE_11B – IEEE 802.11b, 1

    • MODE_11G – IEEE 802.11g, 2

    • MODE_11N – IEEE 802.11n, 4

WLAN Class

This class provides a driver for the Wi-Fi network processor in ESP32. Usage example:

import network
# enable station interface and connect to WiFi access point
nic = network.WLAN(network.STA_IF)
nic.active(True)
nic.connect('your-ssid', 'your-password')
# now use sockets as usual

Constructors

class network.WLAN(interface_id)

Create a WLAN network interface object.

  • interface_id

    • network.STA_IF Station, also known as client, connects to upstream Wi-Fi access points

    • network.AP_IF Acts as a hotspot, allowing other Wi-Fi clients to connect. Hotspot mode allows users to configure their device as an access point, enabling wireless connections between multiple devices without relying on an external router network.

The availability of the following methods depends on the interface type. For example, only the STA interface can connect() to an access point.

Methods

WLAN.active(is_active)

When called with an argument, sets whether the interface is active. When called without arguments, queries the current state. After activating Wi-Fi, power consumption increases. When not using Wi-Fi, you can use active to truly shut down the physical layer wireless.

  • is_active

    • True Activate the network interface

    • False Deactivate the network interface

WLAN.connect(ssid, password)

Connect to the specified wireless network using the given password.

  • ssid: Wi-Fi name

  • password: Wi-Fi password

WLAN.disconnect()

Disconnect from the currently connected wireless network.

WLAN.scan([ssid, bssid, channel, RSSI, authmode, hidden])

Scan for available wireless networks (scan is only performed on the STA interface), returning a list of tuples with Wi-Fi access point information.

  • ssid Service set identifier.

  • bssid Hardware address of the access point, returned as a bytes object in binary form. You can use ubinascii.hexlify() to convert it to ASCII format.

  • channel Channel

  • RSSI Signal strength

  • authmode

    • AUTH_OPEN = 0

    • AUTH_WEP = 1

    • AUTH_WPA_PSK = 2

    • AUTH_WPA2_PSK = 3

    • AUTH_WPA_WPA2_PSK = 4

    • AUTH_MAX = 6

  • hidden

    • False Visible

    • True Hidden

WLAN.status()

Return the current status of the wireless connection.

  • STAT_IDLE – No connection, no activity, -1000

  • STAT_CONNECTING – Connecting, -1001

  • STAT_WRONG_PASSWORD – Failed due to wrong password, -202

  • STAT_NO_AP_FOUND – Failed because no access point replied, 201

  • STAT_GOT_IP – Connection successful, -1010

  • STAT_ASSOC_FAIL – 203

  • STAT_BEACON_TIMEOUT – Timeout, -200

  • STAT_HANDSHAKE_TIMEOUT – Handshake timeout, -204

WLAN.isconnected()
  • In STA mode, returns True if connected to a Wi-Fi access point and has a valid IP address, otherwise returns False.

  • In AP mode, returns True when a station connects, otherwise returns False.

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

When called without arguments, returns a 4-tuple (ip, subnet_mask, gateway, DNS_server).

  • ip: IP address

  • subnet_mask: Subnet mask

  • gateway: Gateway

  • DNS_server: DNS server

When called with arguments, configures a static IP. For example:

wlan.ifconfig(('192.168.0.4', '255.255.255.0', '192.168.0.1', '8.8.8.8'))
wlan.config('param')
wlan.config(param=value, ...)

Get or set general network interface parameters. These methods allow using additional parameters beyond the standard IP configuration (as handled by wlan.ifconfig()). These include network-specific and hardware-specific parameters. For setting parameters, keyword argument syntax should be used, and multiple parameters can be set at once.

mac

MAC address (bytes)

essid

WiFi access point name (string)

channel

WiFi channel (integer)

hidden

Whether ESSID is hidden (boolean)

authmode

Authentication mode supported (enumeration, see module constants)

password

Access password (string)

For querying, the parameter name should be referenced as a string, and only one parameter can be queried at a time:

# Set WiFi access point name (formally known as ESSID) and WiFi channel
ap.config(essid='My AP', channel=11)
# Queey params one by one
print(ap.config('essid'))
print(ap.config('channel'))

Following are commonly supported parameters (availability of a specific parameter
depends on network technology type, driver, and MicroPython port).

Examples

STA mode, connecting to a Wi-Fi network:

import network

SSID = "yourSSID"                  #Wi-Fi name
PASSWORD = "yourPASSWD"            #Wi-Fi password

wlan = network.WLAN(network.STA_IF)  #Create WLAN object
wlan.active(True)                  #Activate interface
wlan.scan()                        #Scan access points
wlan.isconnected()                 #Check if station is connected to AP
wlan.connect(SSID, PASSWORD)       #Connect to AP
wlan.config('mac')                 #Get interface MAC address
wlan.ifconfig()                    #Get interface IP/netmask/gw/DNS address

Access point mode:

import network

ap = network.WLAN(network.AP_IF)     #Create access point interface
ap.active(True)                      #Activate interface
ap.config(essid='micropython',password=b"micropython",channel=11,authmode=network.AUTH_WPA_WPA2_PSK)  #Configure access point