Connecting ESP32 to WiFi Network

Connecting ESP32 to WiFi is first step when using ESP32. In all projects of ESP32 Connecting to WiFi and then accessing other thing is must. ESP32 can operate in three different modes: Wi-Fi station, Wi-Fi access point, and both at the same time. Let’s Connect ESP32 DevKit to WiFi.

Things you’ll need

  • ESP32 DevKit
  • A WiFi router/modem, or You can use your mobile hot spot.
  • Your WiFi network name (SSID) and password (WPA). If you need to set up, check or change the WiFi network name or password, you’ll need to check your modem’s setup guide or user manual for instructions on how to do this.
  • A computer, laptop or other device with a built-in WiFi adapter or a plugin USB WiFi adapter.

Station Mode (STA)

In Station Mode (STA), the ESP32 WiFi Module will be connected to a WiFi Network that is already setup by an Access Point, like a WiFi Router.

Soft Access Point (AP)

The second mode of operation is Access Point (AP) Mode. In this mode, the ESP32 DevKit acts as an access point and provide WiFi Network to other stations (like mobile or laptop).

Usually, an access point can provide internet through a wired network to its stations but as there is no wired interface, this Access Point mode is called Soft Access Point.

The ESP32 Module is first setup as Soft AP mode before configuring it in Station Mode. This is helpful when the username (SSID) and password of the WiFi network is unknown.

Soft AP + Station

In the third mode, the ESP32 WiFi Module is configured to act in both Station Mode and Soft AP Mode.

DevKit ESP32 Arduino IDE Code Example

The code to connect to a wireless access point is relatively straightforward: enter the SSID and the password of the network you want to connect to, and call the

function. Then wait for the connection to complete, your ESP8266 is now connected to your Local Area Network.

To connect to a unsecured WiFi or Open WiFi network remove the password field from

WiFi.begin(SSID); //Enter only SSID

Results

To check and test the connecting open serial monitor. After opening serial monitor press reset button of DevKit to see connection process, Note down the IP.

Open Command prompt by pressing Windows+R key then enter cmd press enter. Enter C:/> ping 192.168.43.225 IP which you got from serial monitor.

The ping command sends small packets to the IP address of the ESP32. When the ESP receives such a packet, it sends it back to the sender. Ping is part of the second layer of the TCP/IP stack, the Internet layer. It relies on both the Data Link layer (Wi-Fi) and the Internet Protocol.
You can see that in the example above, we sent packets to the ESP, and we also received response (echo) packets. This tells us that the Data Link, the Wi-Fi connection, and the Internet Protocol are working correctly.

Points to note

  1. To check ESP32 connected to wifi use WiFi.status() != WL_CONNECTED
  2. In loop you don’t need to worry about re-connection in case WiFi Connection lost, ESP32 WiFi Library Takes care of re-connection to WiFi.
  3. In loop() do not use any function that takes longer time, anything that takes longer time will reset the ESP with Error: WDT reset
  4. ESP32 Connects to only 2.4GHz WiFi.

Leave a Reply