ESP32 using MicroPython: WiFi

The ESP32 comes with a WiFi module, so we can easily connect the ESP32 to the network.

Under MicroPython, we can use the networkmodule to configure the network mode of the ESP32:

  • STAAccess WiFi network as a station

  • APAs a hotspot, allowing other devices to access the ESP32

In this article, we will explain these two modes separately.

Hotspot Mode (AP)

Hotspot mode allows users to configure their ESP32 as a hotspot, which enables wireless connections between multiple ESP32 chips without resorting to an external router network.

import network

ap  =  network . wifi ( network . AP_IF )  # Create a hotspot 
ap . active ( True )          # Activate the hotspot 
ap . config ( essid = 'ESP-AP' )  # Configure the essid (ie the hotspot name) for the hotspot

The above three lines of code simply configure your ESP32 as an open AP hotspot:

You can now find it in your phone or computer's WiFi list.

Station Mode (STA)

More often, we will want to connect the ESP32 to a WiFi network.

import network

wifi  =  network . wifi ( network . STA_IF )  # Create a Wifi station 
wifi . active ( True )        # Activate the station 
wifi . scan ()              # Scan for available networks around

Get the mac address of the current esp32

>>> wifi.config('mac')    
b'0\xae\xa4\x84"d'

Check if you are connected to the network

>>> wifi.isconnected()      
False

Connect to a WiFi, essidrefer to WiFi name, passwordrefer to WiFi password

>>> wifi.connect('essid', 'password')

PS: The picture above How_Router_Homeis the name of the WiFi in the author's home and the password of the WiFihow@home

Get information about the current WiFi connection

IP/Subnet Mask/Gateway/DNS

>>> wifi.ifconfig()  
('192.168.0.117', '255.255.255.0', '192.168.0.1', '192.168.0.1')

The output data from left to right correspond to:

  • Internal and external IP

  • subnet mask

  • gateway

  • DNS

Encapsulate a function to connect to WiFi¶

You can write the steps of WiFi connection as a function according to the official website:

def do_connect():
    import network
    wifi = network.WLAN(network.STA_IF)
    wifi.active(True)
    if not wifi.isconnected():
        print('connecting to network...')
        wifi.connect('essid', 'password')
        while not wifi.isconnected():
            pass
    print('network config:', wifi.ifconfig())

A better way to connect to WiFi¶

EMP Project¶

emp-1zlabThe emp_wifi module is encapsulated in it, which can help you remember the existing wifi connection and connect wifi for everyone more conveniently

The above code is a piece of code officially mentioned in the micropython document of ESP 8266, which is used to connect to wifi, but its function is too simple to meet many urgent needs (remember password, switch network, automatically connect), for WiFi connection, the author once wrote an article detailing the correct posture of WiFi connection. For specific content, please read 1Z laboratory - short book topic

network module API documentation¶

kind

class network.WLAN(mode)

mode:model

  • network.STA_IFStation mode, connecting to an upstream WiFi access point

  • network.AP_IFHotspot mode, allowing other WiFi clients to connect

Defining WLAN¶

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

Function¶ _

wlan.active(is_active)

Function description: With parameters, it is to activate WiFi, without parameters, it is to query the current status.

is_active:Activate now

  • TrueActivate network interface

  • FalseDisable network interface

wlan.scan()

Function description: Scan for available wireless networks (only on the STA interface), return a list of tuples with information about WiFi access points.

(ssid,bssid,channel,RSSI,authmode,hidden)
bssid: The hardware address of the access point, returned in binary as a bytes object. It can ubinascii.hexlify()be converted to ASCII format using
authmode:

  • AUTH_OPEN = 0

  • AUTH_WEP = 1

  • AUTH_WPA_PSK = 2

  • AUTH_WPA2_PSK = 3

  • AUTH_WPA_WPA2_PSK = 4

  • AUTH_MAX = 6

hidden

  • Falsevisible

  • Truehide

wlan.isconnected()

Function description: Check whether the station is connected to the AP.
In STA mode, returns True if connected to a WiFi access point and has a valid IP address, otherwise returns False.
In AP mode, returns True when the station is connected, False otherwise.

wlan.connect(essid, password)

Function description: Connect to a wireless network.

essid: WiFi name
password: WiFi password

wlan.config(essid, channel)

Function description: Get the MAC adddress of the interface or set the WiFi access point name and WiFi channel.

essid: WiFi account name
channel: WiFi channel

wlan.ifconfig([(ip,mask, gateway, dns)])

Function description:
With no parameters, returns a 4-tuple (ip, subnet_mask, gateway, DNS_server).

ip:ip address
mask:subnetmask
gateway:gateway

With parameters, configure static IP. E.g:

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

wlan.disconnect()

Function description: Disconnect from the currently connected wireless network.

wlan.status()

Function description: Returns the current state of the wireless connection.

{{o.name}}
{{m.name}}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324113700&siteId=291194637