network — network configuration

This module provides network drivers and routing configuration. Network drivers for specific hardware are available within this module and are used to configure a hardware network interface. Configured interfaces are then available for use via the socket module. To use this module the network build of firmware must be installed.

For example:

# configure a specific network interface
# see below for examples of specific drivers
import network
nic = network.Driver(...)
print(nic.ifconfig())

# now use socket as usual
import socket
addr = socket.getaddrinfo('micropython.org', 80)[0][-1]
s = socket.socket()
s.connect(addr)
s.send(b'GET / HTTP/1.1\r\nHost: micropython.org\r\n\r\n')
data = s.recv(1000)
s.close()

Functions

network.phy_mode([mode])

Get or set the PHY mode.

If the mode parameter is provided, sets the mode to its value. If the function is called wihout parameters, returns the current mode.

The possible modes are defined as constants:
  • MODE_11B – IEEE 802.11b,
  • MODE_11G – IEEE 802.11g,
  • MODE_11N – IEEE 802.11n.

class WLAN

This class provides a driver for WiFi network processor in the ESP8266. Example usage:

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. Supported interfaces are network.STA_IF (station aka client, connects to upstream WiFi access points) and network.AP_IF (access point, allows other WiFi clients to connect). Availability of the methods below depends on interface type. For example, only STA interface may connect() to an access point.

Methods

wlan.active([is_active])

Activate (“up”) or deactivate (“down”) network interface, if boolean argument is passed. Otherwise, query current state if no argument is provided. Most other methods require active interface.

wlan.connect(ssid, password)

Connect to the specified wireless network, using the specified password.

wlan.disconnect()

Disconnect from the currently connected wireless network.

wlan.mac([address])

Get or set the network interface MAC address.

If the address parameter is provided, sets the address to its value, which should be bytes object of length 6. If the function is called wihout parameters, returns the current address.

wlan.scan()

Scan for the available wireless networks.

Scanning is only possible on STA interface. Returns list of tuples with the information about WiFi access points:

(ssid, bssid, channel, RSSI, authmode, hidden)

There are five values for authmode:

  • 0 – open
  • 1 – WEP
  • 2 – WPA-PSK
  • 3 – WPA2-PSK
  • 4 – WPA/WPA2-PSK

and two for hidden:

  • 0 – visible
  • 1 – hidden
wlan.status()

Return the current status of the wireless connection.

The possible statuses are defined as constants:

  • STAT_IDLE – no connection and no activity,
  • STAT_CONNECTING – connecting in progress,
  • STAT_WRONG_PASSWORD – failed due to incorrect password,
  • STAT_NO_AP_FOUND – failed because no access point replied,
  • STAT_CONNECT_FAIL – failed due to other problems,
  • STAT_GOT_IP – connection susccessful.
wlan.isconnected()

In case of STA mode, returns True if connected to a wifi access point and has a valid IP address. In AP mode returns True when a station is connected. Returns False otherwise.