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 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 initialize it with default values.
Set or get the WiFi network processor configuration.
Arguments are:
mode
WLAN.STA
WLAN.AP
ssid
WLAN.STA_AP
auth
(sec, key)
None
WLAN.WEP
WLAN.WPA
WLAN.WPA2
WLAN.WPA2_ENT
(sec, username, password)
channel
antenna
WLAN.INT_ANT
WLAN.EXT_ANT
WLAN.MAN_ANT
P12
power_save
hidden
True
bandwidth
WLAN.HT20
WLAN.HT40
max_tx_pwr
WLAN.max_tx_power()
country
WLAN.country()
protocol
WLAN.wifi_protocol()
Disables the WiFi radio.
Connect to a WiFi Access Point using the given SSID, and other parameters
bssid
b'\xff\xff\xff\xff\xff\xff'
timeout
ca_certs
keyfile
username
password
certfile
identity
hostname
The ESP32 only handles certificates with pkcs8 format (but not the “Traditional SSLeay RSAPrivateKey” format). The private key should be RSA coded with 2048 bits at maximum.
pkcs8
Performs a network scan and returns a list of named tuples with (ssid, bssid, sec, channel, rssi). When no config args passed scan will be performed with default configurations.
Note: For Fast scan mode ssid/bssid and channel should be
show_hidden
type
WLAN.SCAN_ACTIVE
WLAN.SCAN_PASSIVE
scantime
(min,max)
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, False otherwise.
False
When 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.
id
Get or set the interface configuration.
Optionally specify the configuration parameter:
config='dhcp'
config=(ip, nm, gw, dns)
For example: eth.ifconfig(config=('192.168.0.4', '255.255.255.0', '192.168.0.1', '8.8.8.8')).
eth.ifconfig(config=('192.168.0.4', '255.255.255.0', '192.168.0.1', '8.8.8.8'))
Get or set the WLAN mode.
Get or set the SSID (Set SSID of AP).
In case if mode = WLAN.STA this method can get the ssid of AP the board is connected to.
In case of mode = WLAN.AP this method can get the ssid of the board’s own AP.
In case of mode = WLAN.STA_AP this method can get the ssid of board’s own AP plus the AP the STA is connected to in form of a tuple:
Get or set the authentication type when in AP mode.
WLAN.SEC_CHN_POS_ABOVE
WLAN.SEC_CHN_POS_BELOW
WLAN.SEC_CHN_NONE
Get or set the antenna type (external or internal). Value can be:
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.
when no arguments are passed a 6-byte long bytes tuple object with the WiFI MAC address of both Wifi Station mode and Acces Point mode
bytes
mac: a 6 bytes bytearray mac address
mac
mode: The Interface to set the given MAC address to WLAN.STA or WLAN.AP
Ex: To set the mac address of Wifi Station mode:
wlan.mac(bytearray([0xAE, 0x77, 0x88, 0x99, 0x22, 0x44]), WLAN.STA)
Note: STA and AP cannot have the Same Mac Address
Set the bandwidth of the wifi, either 20 MHz or 40 MHz can be configured, use the following:
Set the Host name of the device connecting to the AP in case of Wifi mode=WLAN.STA, in case of mode=WLAN.AP this is the name of the host hosting the AP. Max length of name string is 32 Bytes
mode=WLAN.STA
mode=WLAN.AP
Gets an info list of all stations connected to the board’s AP.
Info returned is a list of tuples containning ([mac address of connected STA], [average rssi value], [Wlan protocol enabled by STA]).
Protocol types are either : WLAN.PHY_11_B, WLAN.PHY_11_G, WLAN.PHY_11_N or WLAN.PHY_LOW_RATE
WLAN.PHY_11_B
WLAN.PHY_11_G
WLAN.PHY_11_N
WLAN.PHY_LOW_RATE
This API returns with a list of the devices connected to the SG Wireless board when it is in AP mode. Each element of the returned list is a tuple, containing the MAC address and IP address of the device.
Gets or Sets the maximum allowable transmission power for wifi. This is also related to the country setting.
Packets of different rates are transmitted in different powers according to the configuration in phy init data. This API only sets maximum WiFi transmiting power. If this API is called, the transmiting power of every packet will be less than or equal to the value set by this API.
Values passed in the power argument are mapped to transmit power level in dBm. Possible values are between 8 and 78, where 8 corresponds to 2dBm and 78 to 20dBm. All values in between increase the maximum output power in 0.25dBm increments.
power
Gets or sets Country configuration parameters for wifi.
scahn
nchan
policy
WLAN.COUNTRY_POL_AUTO
WLAN.COUNTRY_POL_MAN
Returns a tuple with (bssid, ssid, primary channel, rssi, Authorization method, wifi standard used) of the connected AP in case of STA mode.
(bssid, ssid, primary channel, rssi, Authorization method, wifi standard used)
Sets or gets Wifi Protocol supported. Sets or gets Wifi Protocol supported in (PHY_11_B,PHY_11_G,PHY_11_N) format. Currently 802.11b or 802.11bg or 802.11bgn mode is available.
PHY_11_B
PHY_11_G
PHY_11_N
Send raw data through the Wifi Interface.
Buffer: Buffer of bytes object Containning Data to be transmitted. Data should not be greater than 1500 nor smaller than 24.
Buffer
interface: The Interface to use for transmitting Data AP or STA in case the mode used is APSTA. other wise the interface currently active will be used.
interface
use_sys_seq: True to use the systems next sequance number for sending the data, False for keeping the sequance number in the given raw data buffer unchanged.
use_sys_seq
Register a user callback function handler to be called once any of the trigger events occures optionally with a passed arg. by default the wlan obj is passed as arg to the handler. To unregister the callback you can call the wlan.callback function with empty handler and arg parameters. Possible triggers:
handler
trigger
arg
wlan.callback
WLAN.EVENT_PKT_MGMT
WLAN.EVENT_PKT_CTRL
WLAN.EVENT_PKT_DATA
WLAN.EVENT_PKT_DATA_MPDU
WLAN.EVENT_PKT_DATA_AMPDU
WLAN.EVENT_PKT_MISC
WLAN.EVENT_PKT_ANY
WLAN.SMART_CONF_DONE
WLAN.SMART_CONF_TIEMOUT
Gets or sets WiFi Promiscuous mode.
Note:
wlan.channel()
Example using promiscuous mode:
from network import WLAN import ubinascii def pack_cb(pack): mac = bytearray(6) pk = wlan.wifi_packet() control = pk.data[0] subtype = (0xF0 & control) >> 4 type = 0x0C & control #print("Control:{}, subtype:{}, type:{}".format(control, subtype, type)) if subtype == 4: for i in range (0,6): mac[i] = pk.data[10 + i] print ("Wifi Node with MAC: {}".format(ubinascii.hexlify(mac))) wlan = WLAN(mode=WLAN.STA, antenna=WLAN.EXT_ANT) wlan.callback(trigger=WLAN.EVENT_PKT_MGMT, handler=pack_cb) wlan.promiscuous(True)
This function will return an integer object as mask for triggered events.
This function will return a tuble with Wifi packet info captured in promiscuous mode.
This function is used to set the filter mask for Wifi control packets in promiscuous mode. Possible filters:
WLAN.FILTER_CTRL_PKT_ALL
WLAN.FILTER_CTRL_PKT_WRAPPER
WLAN.FILTER_CTRL_PKT_BAR
WLAN.FILTER_CTRL_PKT_BA
WLAN.FILTER_CTRL_PKT_PSPOLL
WLAN.FILTER_CTRL_PKT_CTS
WLAN.FILTER_CTRL_PKT_ACK
WLAN.FILTER_CTRL_PKT_CFEND
WLAN.FILTER_CTRL_PKT_CFENDACK
To get the current Filter mask, call the function with empty args.
Start SmartConfig operation, the smartConfig is a provisioning technique that enables setting Wifi credentials for station mode wirelessly via mobile app.
wlan.smartConfig()
Get the password of AP the Device is connected to.
SMART_CONF_DONE
SMART_CONF_TIEMOUT