Using ESP32, the most commonly used feature is its WIFI functionality. esptutorial (esptutorial.com) shares how to connect ESP32 to WIFI (join a WIFI access point/wireless router), and after a successful connection, obtain the IP address and check the WIFI strength.

Program Highlights

The steps to connect ESP32 to WIFI are roughly as follows:

  1. First, use WiFi.mode() to configure the ESP32 WIFI mode. Note that in most programs, the WIFI mode is set first, so it is defined as a priority. However, in this example, the WIFI mode is not set, and it can still connect to the WIFI access point. After a successful connection, the WIFI mode is WIFI_STA.

  2. Start connecting to the WIFI access point using WiFi.begin(ssid, pwd), where the WIFI name (SSID) and WIFI password are configured.

  3. Determine the WIFI connection status by checking the return value of WiFi.status() and wait for a successful connection.

  4. After a successful connection, you can use WiFi.localIP() to check the IP address.

  5. After a successful connection, you can use WiFi.RSSI() to check the WIFI strength.

Reference for Wifi methods:
https://lingshunlab.com/book/esp32/esp32-wifi-reference

Program Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// welcome to lingshunlab.com
// 详细说明请参考:https://lingshunlab.com/book/esp32/esp32-connecte-wifi-and-get-ip-address-rssi

#include <WiFi.h>

const char* ssid = "lingshunlab"; // Change this to your WIFI name (SSID)
const char* password = "1234567890"; // Change this to your WIFI password

void setup()
{
Serial.begin(115200);

delay(10);

// Start connecting to WIFI

Serial.println();
Serial.println();
Serial.print("WIFI Mode: ");
Serial.println(WiFi.getMode()); // Display the current WIFI mode
Serial.print("Connecting to ");
Serial.println(ssid);


WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) { // Wait for connection to WIFI until successful, then exit the loop
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected."); // WIFI is connected
Serial.println("IP address: ");
Serial.println(WiFi.localIP()); // Display the IP address after connecting to WIFI
Serial.println(WiFi.RSSI()); // Display the WIFI strength after connecting to WIFI
Serial.print("WIFI Mode: ");
Serial.println(WiFi.getMode()); // Display the current WIFI mode
}

void loop(){
}

After uploading the program, open the serial monitor to view the following information:

esp32-connecte-wifi-and-get-ip-address-rssi-serial-output