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()

class WINC – wifi shield driver

The WINC class is used for controlling the wifi shield.

Example usage:

import network

wlan = network.WINC()
wlan.connect("SSID", "KEY")

wlan.ifconfig()

Constructors

class network.WINC(mode=MODE_STATION)

Creates a winc driver object and connects to the wifi shield which uses I/O pins P0, P1, P2, P3, P6, P7, and P8.

mode controls the mode the WINC module works in:

  • network.WINC.MODE_STATION

    The module connects to an access point as a client. This is the default mode.

  • network.WINC.MODE_AP

    The module will create an AP (Access Point) and accept connections from a client.

    Note1: The start_ap() function must be called after setting AP mode to configure the AP.

    Note2: The WINC1500 has some limitations in its AP implementation:

    • Only one client can connect at a time.
    • Only OPEN or WEP security are supported.
    • There’s a bug in the FW, when the client disconnects any bound sockets are lost (they just stop working). As a workaround, set a timeout for the server socket to force it to raise an exception and then reopen it (See the example script).
  • network.WINC.MODE_P2P

    Enable Peer-to-Peer mode, also known as WiFiDirect. This mode is similar to AP, it allows two devices to connect and exchange data directly. Note: This mode is Not implemented Yet.

  • network.WINC.MODE_FIRMWARE:

    This mode enables WiFi module firmware update. Note: Do NOT use unless you know what you’re doing, modules are shipped with the latest FW update there’s No need to update the FW.

Methods

winc.connect(ssid, key=None, security=WPA_PSK)

Connect to a wifi network with ssid ssid using key key with security security.

After connecting to the network use the usocket module to open TCP/UDP ports to send and receive data.

Note

This function takes a little while to return.

winc.start_ap(SSID, key=None, security=OPEN, channel=1)

When running in AP mode this function must be called after creating a WINC object to configure and start the AP .

  • SSID: The AP SSID (must be set)
  • Key: The AP encryption key. A Key is required only if security is WEP.
  • security: AP security mode. (Only network.WINC.OPEN or network.WINC.WEP are supported).
  • channel: WiFi channel, change this if you have another AP running at the same channel.
winc.disconnect()

Disconnect from the wifi network.

winc.isconnected()

Returns True if connected to an access point and an IP address has been obtained.

winc.ifconfig()

Returns a tuple containing:

  • [0]: RSSI - received signal strength indicator (int)
  • [1]: Authorization Type (see constants)
  • [2]: Set Service Identifier String (SSID)
  • [3]: MAC Address String (XX:XX:XX:XX:XX:XX) (BSSID)
  • [4]: IP Address String (XXX.XXX.XXX.XXX)

While connected to the network.

winc.scan()

Returns a list of tuples containing:

  • [0]: Channel Number (int)
  • [1]: RSSI - received signal strength indicator (int)
  • [2]: Authorization Type (see constants)
  • [3]: MAC Address String (XX:XX:XX:XX:XX:XX) (BSSID)
  • [4]: Set Service Identifier String (SSID)

You don’t need to be connected to call this.

winc.rssi()

Returns the received signal strength indicator (int) of the currently connected network.

winc.fw_version()

Returns a tuple containing the wifi shield firmware version number.

  • [0]: Firmware Major Version Number (int)
  • [1]: Firmware Minor Version Number (int)
  • [2]: Firmware Patch Version Number (int)
  • [3]: Driver Major Version Number (int)
  • [4]: Driver Minor Version Number (int)
  • [5]: Driver Patch Version Number (int)
  • [6]: Hardware Revision Number - Chip ID (int)
winc.fw_dump()

Dumps the wifi shield firmware to a binary at “/firmware/fw_dump.bin”

winc.fw_update()

Programs the wifi shield with binary image found at “/firmware/m2m_aio_3a0.bin”.

winc.connected_sta()

This method returns the connected client IP.

winc.wait_for_sta(timeout)

This function blocks and waits for a client to connect. If timeout is 0 this will block forever.

Constants

winc.OPEN

For connecting to an open wifi network.

Note

Insecure.

winc.WEP

For connecting to a WEP based password protected network.

Note

Insecure.

winc.WPA_PSK

For connecting to a WPA/PSK based password protected network.

Note

For networks that need a password for all users.

winc.802_1X

For connecting to a 802.1X based password protected network.

Note

For networks that need a separate password per user.