class network.WLAN(interface_id)

    interface_id:
        network.STA_IF — Client, connected to the Wi-Fi access point
        network.AP_IF — access point, permit other Wi-Fi clients to connect

Define WLAN

import network

wlan = network.WLAN(network.STA_IF)    #Create WLAN object

Functions

1. wlan.active(is_active)

Function Explanation: With parameters, for whether the interface is activated, without parameters for the query current state.

    is_active: activated or not
        True — activate ("up") internet interface
        False — stop ("down") internet interface

2. wlan.scan()

Function Explanation: Scan available wireless networks (scans only on the STA interface) and returns a tuple list of information about WiFi access points.

    (ssid, bssid, channel, RSSI, authmode, hidden)
        bssid: the hardware address of the access point, returned as a byte object in binary form. You can convert it to ASCII format using ubinascii.hexlify()
       authmode:
            AUTH_OPEN = 0
            AUTH_WEP = 1
            AUTH_WPA_PSK = 2
            AUTH_WPA2_PSK = 3
            AUTH_WPA_WPA2_PSK = 4
            AUTH_MAX = 6 
        hidden:
            False — visible
            True — invisible

3. wlan.isconnected()

Function Explanation: Check if the site is connected to the AP. In STAT mode, return True if it is connected to a WiFi access point and has a valid IP address, otherwise return to False. In AP mode, return True when the site is connected, or False otherwise.

4. wlan.connect(ssid, password)

Function Explanation: Connected Wi-Fi.

    ssid: WiFi name
    password: WiFi password

5. wlan.config(essid, channel)

Function Explanation: Get the MAC address of the interface or set the Wi-Fi access point name and Wi-Fi chnnel.

    ssid: WiFi name
    channel: WiFi channel

6. wlan.ifconfig([(ip, subnet, gateway, dns)])

Function Explanation: Return to a four tuple (ip, subnet__mask, gateway, DNS_server) without specified parameter.

ip: IP address 
subnet_mask: subnet mask
gateway: gateway
DNS_server: DNS server

Configure static when with parameters.

E.g.

wlan.ifconfig(config = ('192.168.0.4', '255.255.255.0', '192.168.0.1', '8.8.8.8')

7. wlan.disconnect()

Function Explanation: Disconnect the current Wi-Fi.

8. wlan.status()

Function Explanation: Return to the current Wi-Fi status.

Sample Codes

1. Connect Wi-Fi as a Client

import network

SSID = "yourSSID"                  #WiFi name 
PASSWORD = "yourPASSWD"            #WiFi password

wlan = network.WLAN(network.STA_IF)  #Create WLAN object
wlan.active(True)                  #Activate the interface
wlan.scan()                        #Scan access point
wlan.isconnected()                 #Check whether the site is connected to the AP
wlan.connect(SSID, PASSWORD)       #Connect to AP
wlan.config('mac')                 #Get the MAC address of the interface
wlan.ifconfig()                    #Get the IP/netmask/gw/DNS address of the interface

2. Open Wi-FI as Access Point

import network

ap = network.WLAN(network.AP_IF)     #eate an access point interface
ap.active(True)                      #Activate the interface
ap.config(essid='ESP-AP',channel=1)  #Set the ESSID of the access point, and the WIFI channel

3. Connect to the local Wi-Fi

import network

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
  print('connecting to network...')
  wlan.connect('SSID', 'PASSWORD')   #Connect to AP
     #'SSID': WiFi name
     #'PASSWORD': WiFi password
  while not wlan.isconnected():
    pass
print('network config:', wlan.ifconfig())

results matching ""

    No results matching ""