WiFi
WiFi¶
This module implements a generic wifi interface. To function correctly it needs a wifi driver to be loaded, so that the module can use the driver to access the underlying hardware.
The link between the wifi module and the wifi driver is established without the programmer intervention by the driver itself.
This module defines the following constants:
WIFI_OPEN
= 0; Open Wifi networkWIFI_WEP
= 1; Wifi Network secured with WEPWIFI_WPA
= 2; Wifi Network secured with WPAWIFI_WPA2
= 3; Wifi Network secured with WPA2WIFI_WPA_WPA2
= 4; Wifi Network secured with WPA or WPA2WIFI_WPA2_ENTERPRISE
= 5; Wifi Network secured with WPA2 enterpriseWIFI_WPA3
= 6; Wifi Network secured with WPA3WIFI_WPA2_WPA3
= 7; Wifi Network secured with WPA2 or WPA3
Exception¶
exception WifiException
¶
Generic exception
exception WifiBadPassword
¶
The configured password is not correct.
exception WifiBadSSID
¶
The configured SSID is not available between visible WiFi networks.
Functions¶
function configure
¶
configure(ssid="", password="", security=WPA_WPA2, dhcp=True, ip="", mask="", gateway="", dns="8.8.8.8", timeout=10000, ent_user="", ent_pwd="", hostname=""))
ssid
is the WiFi name to associate to.password
the the shared secret for thessid
network.security
is the encryption type to be used.
If dhcp
is True (the default) other following arguments are ignored. When dhcp
is False, the other arguments are:
ip
: is the IP address.mask
: the net mask expressed as A.B.C.D dotted address.gateway
: the gateway to be used as default router.-
dns
: the Domain Name Server to be used for name resolution. Default is "8.8.8.8", the Google DNS. -
timeout
: Connection timeout in milliseconds.WifiException
is raised if connection do not succeed during this time. Default value 10000 ms.
If the security
is WPA2_ENTERPRISE (a.k.a. WPA-802.1X), the following parameters are used to specify the Extensible Authentication Protocol (EAP) attributes. Only PEAP is supported. If the security
is set to other values, the following parameters are ignored.
ent_user
: the username to be used for the authentication. Default value is empty string.-
ent_pwd
: the password to be used for the authentication. Default value is empty string. -
hostname
: hostname associated with the interface. When thehostname
is empty string, the dcn (Device Common Name) is used as hostname. Default value "".
function start
¶
start()
configure()
function. function stop
¶
stop()
function resolve
¶
resolve(host)
host
to its IP address by using the configured DNS server and returning a string with the result. function info
¶
info()
Bool
: DHCP enabled (True) or disabled (False)String
: IP addressString
: netmaskString
: gatewayString
: DNSString
: MAC address
function ap_info
¶
ap_info()
String
: The SSID of the APInteger
: The channel numberInteger
: The signal strengthInteger
: The security mode as described above in the security parameter ofconfigure
function.String
: The BSSID of the AP in hexadecimal notation (e.g.: 10:20:30:40:50:60).
If the WiFi interface is not associated with any AP, the PeripheralError exception will be raised.
function scan
¶
scan(ssid=None)
ssid
is the target ssid to scan. IfNone
all the found AP will be saved, otherwise only the ones matching withssid
.
function get_ap_num
¶
get_ap_num()
function get_ap_records
¶
get_ap_records(n)
n
is the number of APs tuples to be returned.
Return a list of tuples with the following format (ssid
, channel
, rssi
, auth mode
, bssid
). The tuple is composed by the following elements:
String
: The SSID of the APInteger
: The channel numberInteger
: The signal strengthInteger
: The security mode as described above in the security parameter ofconfigure
function.String
: The BSSID of the AP in hexadecimal notation (e.g.: 10:20:30:40:50:60).
function get_rssi
¶
get_rssi(ssid=None)
rssi
of the specified ssid
. If ssid
is None
the rssi of the currently associeted net is returned, otherwise a scan is executed before returning the rssi
, this can require some seconds. function scan_and_get
¶
scan_and_get(ssid=None)
ssid
is the target ssid to scan. IfNone
all the found AP will be saved, otherwise only the ones matching withssid
.
Return a list of tuples with the following format (ssid
, channel
, rssi
, auth mode
, bssid
). The tuple is composed by the following elements:
String
: The SSID of the APInteger
: The channel numberInteger
: The signal strengthInteger
: The security mode as described above in the security parameter ofconfigure
function.String
: The BSSID of the AP in hexadecimal notation (e.g.: 10:20:30:40:50:60).
Examples¶
Using the wifi module is very easy:
from bsp import board
from networking import wifi
board.init()
board.summary()
try:
# Configure WiFi to use dhcp with a specific network
wifi.configure(ssid="My-Network",password="My-Password")
# Start the interface
wifi.start()
# Print the ip, gateway, mask, dns and mac address
print(wifi.info())
# Try resolving some hostname via dns
ip=wifi.resolve("www.zerynth.com")
print("resolved",ip)
# sleep a little bit
sleep(5000)
# disable wifi
wifi.stop()
except WifiBadPassword:
print("Bad Password")
except WifiBadSSID:
print("Bad SSID")
except WifiException:
print("Generic Wifi Exception")
except Exception as e:
raise e
while True:
sleep(1000)
More configuration is also available:
from bsp import board
from networking import wifi
board.init()
board.summary()
try:
# Configure WiFi to use dhcp with a specific network and security
wifi.configure(ssid="My-Network",password="My-Password", security=wifi.WPA)
# Start the interface
wifi.start()
# Print the ip, gateway, mask, dns and mac address
print(wifi.info())
# Try resolving some hostname via dns
ip=wifi.resolve("www.zerynth.com")
print("resolved",ip)
# sleep a little bit
sleep(5000)
# disable wifi
wifi.stop()
except WifiBadPassword:
print("Bad Password")
except WifiBadSSID:
print("Bad SSID")
except WifiException:
print("Generic Wifi Exception")
except Exception as e:
raise e
while True:
sleep(1000)
Using WiFi with WPA2 Enterprise
from bsp import board
from networking import wifi
board.init()
board.summary()
try:
# Configure WiFi to use dhcp with a specific network and security
print("configuring...")
ssid = "WiFi Enterprise"
wifi.configure(
ssid=ssid,
security=wifi.WPA2_ENTERPRISE,
ent_user="test_usr", ent_pwd="test_pwd",
timeout=10000)
# Start the interface
print("connecting...")
wifi.start()
print("connected...")
# Print the ip, gateway, mask, dns and mac address
print("info...")
print(wifi.info())
# Try resolving some hostname via dns
ip=wifi.resolve("www.zerynth.com")
print("resolved", ip)
# sleep a little bit
sleep(5000)
# disable wifi
wifi.stop()
except Exception as e:
print(e)
while True:
sleep(1000)