keyestudio
Share your creative ideas with us here
By ran | 20 September 2024 | 0 Comments

HOW to Use WIFI (AP Mode) with ESP32

Description

In this project, we are going to learn the WiFi AP mode of the ESP32.

Components

image-20230519093919133

image-20230519093929509

USB Cable x1

ESP32*1

Wiring Diagram

Plug the ESP32 mainboard to the USB port of your PC

image-20230519093658504

Component Knowledge

AP Mode:

When setting AP mode, a hotspot network will be created, waiting for other WiFi devices to connect. As shown below;

Take the ESP32 as the hotspot, if a phone or PC needs to communicate with the ESP32, it must be connected to the ESP32’s hotspot. Communication is only possible after a connection is established via the ESP32.

Test Code

import network #Import network module.

#Enter correct router name and password.
ssidAP         = 'ESP32_Wifi' #Enter the router name
passwordAP     = '12345678'  #Enter the router password

local_IP       = '192.168.1.147'
gateway        = '192.168.1.1'
subnet         = '255.255.255.0'
dns            = '8.8.8.8'

#Set ESP32 in AP mode.
ap_if = network.WLAN(network.AP_IF)

def AP_Setup(ssidAP,passwordAP):
    ap_if.ifconfig([local_IP,gateway,subnet,dns])
    print("Setting soft-AP  ... ")
    ap_if.config(essid=ssidAP,authmode=network.AUTH_WPA_WPA2_PSK, password=passwordAP)
    ap_if.active(True)
    print('Success, IP address:', ap_if.ifconfig())
    print("Setup End\n")

try:
    AP_Setup(ssidAP,passwordAP)
except:
    print("Failed, please disconnect the power and restart the operation.")
    ap_if.disconnect()

Test Result

You can modify the AP name and password or keep them unchanged.

Click “Run current script”, the code will start executing. Open the AP function of the ESP32, the Shell monitor will print the information

Turn on your phone’s WiFi search function, then you can see the ssid_AP which is called “ESP32_Wifi” in this code. You can enter the password “12345678” to connect it, or you can modify its AP name and password by code.

Leave a Reply

Your email address will not be published.Required fields are marked. *
Name
E-mail
Content
Verification code