We all know that ESP32 is really strong and fun to mess around with as of now. Today, we are going to discover how ESP32 can be use with Wi-fi and web server.

WiFi functionality on ESP32

The ESP32 supports both Wi-Fi receive and transmit modes, allowing the device to communicate and transfer data with other devices wirelessly. With its Wi-Fi capabilities, the ESP32 can be used in various applications such as remote control, monitoring, and smart home automation systems.

They support a lot of Wi-fi modes includes: station mode, access point mode and station + accesspoint mode.

Let us explore one by one

Station (STA) mode on ESP32:

In this mode, the ESP32 connects to an existing Wi-Fi network, acting as a client device. The ESP32 can send and receive data over this network, similar to devices such as smartphones and laptops.

image.png

Here is how to set up station mode on ESP32

#include <WiFi.h> // Include the WiFi library
const char* ssid = "DTM E-SMART"; // WiFi network name
const char* password = "0919890938"; // WiFi password

void setup(){
Serial.begin(115200); 
WiFi.mode(WIFI_STA); // Set WiFi mode to STA (Station)
WiFi.begin(ssid, password); // Connect to the WiFi network
Serial.println("\\nConnecting to WiFi Network .."); 
while(WiFi.status() != WL_CONNECTED){
Serial.print("."); 
delay(100); 
}
Serial.println("\\nConnected to the WiFi network"); 
Serial.print("Local ESP32 IP: "); 
Serial.println(WiFi.localIP()); // Print the connected IP address
}

void loop(){
// Do Nothing
}

Access Point (AP) mode on ESP32:

The ESP32 creates its own Wi-Fi access point, allowing other devices to connect to it. This mode is useful when you want the ESP32 to act as a router or a mobile hotspot.

image.png

Here is how to set up access point mode on ESP32:

#include <WiFi.h> // Include the WiFi library
const char* ssid = "ESP32_AP"; // The name of the Wi-Fi network to broadcast
const char* password = "12345678"; // The Wi-Fi password

void setup() {
Serial.begin(115200); 
WiFi.mode(WIFI_OFF); // Turn off Wi-Fi before initialization
delay(100); 
WiFi.mode(WIFI_AP); // Set Wi-Fi mode to AP (Access Point)
delay(1000); 
if(!WiFi.softAP(ssid,password)){ // Start broadcasting the Wi-Fi network
Serial.println("Wifi AP fail!"); 
while(1); 
}
Serial.println("WiFi AP started!"); 
Serial.print("IP host:"); 
Serial.println(WiFi.softAPIP()); // Print the host IP address
}

void loop() {
// Do Nothing
}

Station + Access Point (STA+AP) on ESP32:

This is a combined mode that allows the ESP32 to both connect to an existing Wi-Fi network (as a client device) and create its own Wi-Fi access point. This mode offers high flexibility in communication and data transmission.

#include <WiFi.h> // Include the WiFi library
const char* ssid = "DTM E-SMART"; // WiFi access point name
const char* password = "0919890938"; // WiFi password

const char* ssidAP = "ESP32_AP"; // Name of the WiFi network to broadcast
const char* passwordAP = "12345678"; // Password for the broadcasted WiFi network

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

WiFi.mode(WIFI_AP_STA); // Set WiFi mode to STA+AP

WiFi.begin(ssid, password); // Connect to WiFi
Serial.println("\\nConnecting to WiFi Network .."); 
while(WiFi.status() != WL_CONNECTED){
Serial.print("."); 
delay(100); 
}
Serial.println("\\nConnected to the WiFi network"); 
Serial.print("Local ESP32 IP: "); 
Serial.println(WiFi.localIP()); // Print the IP address after connecting to WiFi

int channel = WiFi.channel(); // Read the connected WiFi channel
// Broadcast WiFi on the connected channel
if(!WiFi.softAP(ssidAP, passwordAP, channel)){
Serial.println("Soft AP creation failed."); 
while(1); 
}
Serial.println("\\nSuccessfully created soft AP"); 
Serial.print("AP IP address: "); 
Serial.println(WiFi.softAPIP()); // Print the host IP address
}

void loop(){
// Do Nothing
}