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(...)
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 WLAN¶
This class provides a driver for WiFi network processor in the WiPy. Example usage:
import network
# setup as a station
nic = network.WLAN(mode=WLAN.STA)
nic.connect('your-ssid', security=WLAN.WPA2, key='your-key')
while not nic.isconnected():
pyb.delay(50)
print(nic.ifconfig())
# now use socket as usual
...
Constructors¶
-
class
network.WLAN(..) Create a WLAN object, and optionally configure it. See
iwconfigfor params of configuration.
Methods¶
-
network.iwconfig(*, mode, ssid, security, key, channel, antenna)¶ Set or get the WiFi network processor configuration.
Arguments are:
modecan be eitherWLAN.STAorWLAN.AP.ssidis a string with the ssid name. Only needed when mode isWLAN.AP.securitycan beWLAN.OPEN,WLAN.WEP,WLAN.WPAorWLAN.WPA2. Only needed when mode isWLAN.AP.keyis a string with the network password. Not needed when mode isWLAN.STAor security isWLAN.OPEN. IfsecurityisWLAN.WEPthe key must be a string representing hexadecimal values (e.g. ‘ABC1DE45BF’).channela number in the range 1-11. Only needed when mode isWLAN.AP.antennaselects between the internal and the external antenna. Can be eitherWLAN.INTERNALorWLAN.EXTERNAL.
For example, you can do:
# create and configure as an access point nic.iwconfig(mode=WLAN.AP, ssid='wipy-wlan', security=WLAN.WPA2, key='www.wipy.io', channel=7, antenna=WLAN.INTERNAL)
or:
# configure as an station nic.iwconfig(mode=WLAN.STA)
With no arguments given, the current configuration is returned as a namedtuple that looks like this:
(mode=2, ssid='wipy-wlan', security=2, key='www.wipy.io', channel=5, antenna=0)
-
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.
keyis always a string, but ifsecurityisWLAN.WEPthe key must be a string representing hexadecimal values (e.g. ‘ABC1DE45BF’).bssidis the MAC address of the AP to connect to. Useful when there are several APs with the same ssid.timeoutis the maximum time in milliseconds to wait for the connection to succeed.
-
wlan.scan() Performs a network scan and returns a list of named tuples with (ssid, bssid, security, channel, rssi). Note that channel is always
Nonesince this info is not provided by the WiPy.
-
wlan.disconnect() Disconnect from the wifi access point.
-
wlan.isconnected()¶ In case of STA mode, returns
Trueif connected to a wifi access point and has a valid IP address. In AP mode returnsTruewhen a station is connected. ReturnsFalseotherwise.
-
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(('192.168.0.4', '255.255.255.0', '192.168.0.1', '8.8.8.8'))
-
wlan.mac()¶ Returns a 6-byte long bytes object with the MAC address.
-
wlan.connections()¶ Returns a list of the devices currently connected. Each item in the list is a tuple of
(ssid, mac).
-
wlan.callback(wakes)¶ Create a callback to be triggered when a WLAN event occurs during
pyb.Sleep.SUSPENDEDmode. Events are triggered by socket activity or by WLAN connection/disconnection.wakescan only bepyb.Sleep.SUSPENDED.
Returns a callback object.