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.

For example:

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

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

class WLAN

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

import network
# setup as a station
nic = network.WLAN(WLAN.STA)
nic.connect('your-ssid', security=WLAN.WPA_WPA2, key='your-key')
while not nic.isconnected():

# now use socket as usual


class network.WLAN(mode, ssid, *, security=WLAN.OPEN, key=None, channel=5)

Create a WLAN driver object, initialise the WLAN engine in station or AP mode.

Arguments are:

  • mode can be either WLAN.STA or WLAN.AP.
  • ssid is a string with the ssid name. Only needed when mode is WLAN.AP.
  • security can be WLAN.OPEN, WLAN.WEP or WLAN.WPA_WPA2. Only needed when mode is WLAN.AP.
  • key is a string with the WLAN.WPA_WPA2 key or a byte array with the WLAN.WEP key. Not needed when mode is WLAN.STA or security is WLAN.OPEN.
  • channel a number in the range 1-11. Only needed when mode is WLAN.AP.

For example, you can use:

# configure as an access point
nic = network.WLAN(WLAN.AP, 'wipy-wlan', security=WLAN.WPA_WPA2, key='', channel=7)


# configure as an station
nic = network.WLAN(WLAN.STA)


wlan.connect(ssid, *, security=WLAN.OPEN, key=None, bssid=None, timeout=5000)

Connect to a wifi access point using the given SSID, and other security parameters.

  • bssid is the MAC address of the AP to connect to. Useful when there are several APs with the same ssid.
  • timeout is the maximum time in milliseconds to wait for the connection to succeed.

Performs a network scan and returns a list of named tuples with (ssid, bssid, security, channel, rssi). Note that channel is always None since this info is not provided by the WiPy.


Disconnect from the wifi access point.


Returns True if connected to a wifi access point and has a valid IP address, False otherwise.

wlan.ifconfig(['dhcp' or configtuple])

With no parameters given eturns a 4-tuple of (ip, subnet mask, gateway, DNS server).

if 'dhcp' is passed as a parameter then the DHCP client is enabled and the IP params are negotiated with the AP.

if the 4-tuple config is given then a static IP is configured. For example:

nic.ifconfig(('', '', '', ''))

Provides information about the current WLAN configuration. Returns a named tuple of (mode, ssid, security, mac)

  • mode can be either WLAN.STA or WLAN.AP.
  • ssid is a string with our ssid if in AP mode, None oterwise.
  • security security type currently used.
  • mac our MAC address.

Returns a list of the devices currently connected. Each item in the list is a tuple of (ssid, mac).


Selects the antenna type to be used. Must be either WLAN.INT_ANTENNA or WLAN.EXT_ANTENNA.


Create a callback to be triggered when a WLAN event occurs during pyb.Sleep.SUSPENDED mode. Events are triggered by socket activity or by WLAN connection/disconnection.

  • wakes can only be pyb.Sleep.SUSPENDED.

Returns a callback object.



WiFi station mode


WiFi access point mode


open network (no security)


WEP network security


WPA/WPA2 network security


selects the internal antenna


selects the external antenna