This class provides a driver for the WiFi network processor in the module. Example usage:
import network import time # setup as a station wlan = network.WLAN(mode=network.WLAN.STA) wlan.connect('your-ssid', auth=(network.WLAN.WPA2, 'your-key')) while not wlan.isconnected(): time.sleep_ms(50) print(wlan.ifconfig()) # now use socket as usual
import machine from network import WLAN # configure the WLAN subsystem in station mode (the default is AP) wlan = WLAN(mode=WLAN.STA) # go for fixed IP settings (IP, Subnet, Gateway, DNS) wlan.ifconfig(config=('192.168.0.107', '255.255.255.0', '192.168.0.1', '192.168.0.1')) wlan.scan() # scan for available networks wlan.connect(ssid='mynetwork', auth=(WLAN.WPA2, 'my_network_key')) while not wlan.isconnected(): pass print(wlan.ifconfig())
Create a WLAN object, and optionally configure it. See init for params of configuration.
The WLAN constructor is special in the sense that if no arguments besides the id are given, it will return the already existing WLAN instance without re-configuring it. This is because WLAN is a system feature of the WiPy. If the already existing instance is not initialised it will do the same as the other constructors an will initialise it with default values.
Set or get the WiFi network processor configuration.
modecan be either
WLAN.STA, WLAN.APor WLAN.STA_AP.
ssidis a string with the ssid name. Only needed when mode is
authis a tuple with (sec, key). Security can be
WLAN.WEP, WLAN.WPAor WLAN.WPA2. The key is a string with the network password. If
WLAN.WEPthe key must be a string representing hexadecimal values (e.g. ‘ABC1DE45BF’). Only needed when mode is WLAN.AP.
channela number in the range 1-11. Only needed when mode is
antennaselects between the internal and the external antenna. Can be either
WLAN.INT_ANT, WLAN.EXT_ANT. With our development boards it defaults to using the internal antenna, but in the case of an OEM module, the antenna pin (P12) is not used, so it’s free to be used for other things.
power_saveenables or disables power save functions in STA mode.
For example, you can do:
# create and configure as an access point wlan.init(mode=WLAN.AP, ssid='wipy-wlan', auth=(WLAN.WPA2,'www.wipy.io'), channel=7, antenna=WLAN.INT_ANT)
# configure as an station wlan.init(mode=WLAN.STA)
Disables the WiFi radio.
wlan.connect(ssid, * , auth=None, bssid=None, timeout=None, ca_certs=None, keyfile=None, certfile=None, identity=None)
Connect to a wifi access point using the given SSID, and other security parameters.
authis a tuple with (sec, key). Security can be None,
WLAN.WEP, WLAN.WPA, WLAN.WPA2or WLAN.WPA2_ENT. The key is a string with the network password. If sec is WLAN.WEPthe key must be a string representing hexadecimal values (e.g. ‘ABC1DE45BF’). If sec is WLAN.WPA2_ENTthen the auth tuple can have either 3 elements: (sec, username, password), or just 1: (sec,). When passing the 3 elemnt tuple, the keyfile and certifle arguments must not be given.
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.
ca_certsis the path to the CA certificate. This argument is not mandatory. keyfile is the path to the client key. Only used if username and password are not part of the auth tuple.
certfileis the path to the client certificate. Only used if username and password are not part of the auth tuple.
identityis only used in case of
Performs a network scan and returns a list of named tuples with (ssid, bssid, sec, channel, rssi). Note that channel is always None since this info is not provided by the WiPy.
Disconnect from the wifi access point.
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,
id is 0, the configuration will be get/set on the Station interface. When id is 1 the configuration will be done for the AP interface.
With no parameters given eturns a 4-tuple of (
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 instance:
wlan.ifconfig(config=('192.168.0.4', '255.255.255.0', '192.168.0.1', '184.108.40.206'))
Get or set the WLAN mode.
Get or set the SSID when in AP mode.
Get or set the authentication type when in AP mode.
Get or set the channel (only applicable in AP mode).
Get or set the antenna type (external or internal).
Get a 6-byte long
bytes object with the WiFI MAC address.
WLAN network security