Blog Datasheets Home About me Clients My work Services Contact

G2Labs Grzegorz Grzęda

Deep Dive into ESP32 WiFi Connectivity: Understanding WiFi Modes and Configurations

April 25, 2024

Deep Dive into ESP32 WiFi Connectivity: Understanding WiFi Modes and Configurations

Welcome to another blog post on ESP32 WiFi connectivity! In this post, we will take a deep dive into understanding WiFi modes and configurations on the popular ESP32 microcontroller. We will explore the various WiFi modes, explain their differences, and provide practical examples to showcase their usage.

Introduction to ESP32 WiFi Modes

ESP32 microcontrollers offer three primary WiFi modes: Station (STA) mode, Access Point (AP) mode, and Soft Access Point (SoftAP) mode. Each mode serves a different purpose in establishing WiFi connectivity and has its own unique configurations.

Station (STA) Mode

The Station mode allows the ESP32 to connect to an existing WiFi network infrastructure as a client. This mode enables the ESP32 to access the internet or communicate with other devices on the network. It is the most common mode used in IoT applications.

To configure the ESP32 in Station mode, we need the SSID (WiFi network name) and the corresponding password. Here’s an example using the Arduino framework:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include <WiFi.h>

const char* ssid = "YourWiFiNetwork";
const char* password = "YourWiFiPassword";

void setup() {
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  
  // Connected to WiFi
  Serial.println("Connected to WiFi!");
}

void loop() {
  // Your code here
}

The code above attempts to connect the ESP32 to a WiFi network using the provided SSID and password. It continuously checks the connection status until successfully connected.

Access Point (AP) Mode

In Access Point mode, the ESP32 acts as a WiFi network access point itself. Other devices can connect to it to access services or exchange data. This mode is useful when we need the ESP32 to host a local network for direct communication with other devices.

Here’s an example of configuring the ESP32 in Access Point mode:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include <WiFi.h>

const char* ssid = "MyESP32AP";
const char* password = "MyPassword";
const int channel = 7;

void setup() {
  WiFi.softAP(ssid, password, channel);
  
  // Access Point started
  Serial.println("Access Point started");
}

void loop() {
  // Your code here
}

The code above sets up the ESP32 as an Access Point with the provided SSID, password, and optional channel number. Other devices can now connect to this network, allowing for direct communication with the ESP32.

Soft Access Point (SoftAP) Mode

Soft Access Point mode is a combination of Station and Access Point modes. It allows the ESP32 to connect to an existing WiFi network while simultaneously acting as an access point for other devices to connect to. This mode is particularly useful for applications where the ESP32 needs to act as a bridge between connected devices and an external network.

To configure the ESP32 in SoftAP mode, we need the SSID and password for the existing network we want the ESP32 to connect to, as well as an SSID and password for the SoftAP network. Here’s an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <WiFi.h>

const char* staSsid = "YourWiFiNetwork";
const char* staPassword = "YourWiFiPassword";
const char* apSsid = "MyESP32AP";
const char* apPassword = "MyPassword";

void setup() {
  WiFi.mode(WIFI_MODE_STA);
  WiFi.begin(staSsid, staPassword);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  
  WiFi.softAP(apSsid, apPassword);
  
  // WiFi connection and SoftAP started
  Serial.println("Connected to WiFi and SoftAP started");
}

void loop() {
  // Your code here
}

The code above connects the ESP32 to an existing WiFi network in Station mode using the provided SSID and password. Additionally, it starts a SoftAP with the specified SSID and password. This allows other devices to connect to the SoftAP network while the ESP32 remains connected to the existing WiFi network.

Conclusion

Understanding and utilizing the various WiFi modes and configurations available on the ESP32 microcontroller is essential for developing connected IoT applications. In this blog post, we explored the Station, Access Point, and Soft Access Point modes, examining their use cases and providing example code snippets to get you started.

Whether you need your ESP32 to connect as a client, host a local network, or act as a bridge, knowing how to configure WiFi modes on the ESP32 will empower you to develop robust and versatile WiFi-enabled projects.

Have fun experimenting with ESP32 WiFi connectivity! Stay tuned for more exciting articles on programming and IoT.

Happy coding!


➡️ Linux 'rm' command


⬅️ Linux 'mkdir' command


Go back to Posts.