ESP32 Wi-Fi and Networking
Wi-Fi is one of the most important ESP32 features because it allows your ESP32 to communicate with phones, computers, routers, web servers, cloud platforms, and other IoT devices.
For example:
Internet │ │ ┌────▼────┐ │ Wi-Fi │ │ Router │ └────┬────┘ │ ┌────────┴────────┐ │ │ ESP32 Phone │ ├── LED ├── Relay ├── Sensor └── Motor
1. What is Wi-Fi?
Wi-Fi is a wireless networking technology that allows devices to communicate over a local network.
Your ESP32 can connect to a router just like your phone or laptop.
Example:
Phone ─────┐ Laptop ────┤ ESP32 ─────┼── Wi-Fi Router Printer ───┘
The router creates a Local Area Network (LAN).
2. ESP32 Wi-Fi Modes
The ESP32 commonly works in two important modes:
ESP32 Wi-Fi │ ┌───────┴────────┐ │ │ Station Mode Access Point │ │ Connects to Creates Router Wi-Fi network
There is also a third useful mode:
STA + AP
where the ESP32 can connect to an existing Wi-Fi network while also creating its own access point.
3. Station Mode — STA
In Station Mode, the ESP32 connects to an existing Wi-Fi network.
For example:
Internet │ Router │ ├──── Laptop ├──── Phone └──── ESP32
The ESP32 receives an IP address from the router.
Example:
ESP32 IP: 192.168.1.105
You can then access the ESP32 from another device on the same network.
4. Basic Wi-Fi Connection
Include:
#include <WiFi.h>
Then:
const char* ssid = "YourWiFi"; const char* password = "YourPassword";
Connect:
WiFi.begin(ssid, password);
Wait for connection:
while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); }
Then display the IP:
Serial.println(WiFi.localIP());
5. Complete Example
#include <WiFi.h> const char* ssid = "YourWiFi"; const char* password = "YourPassword"; void setup() { Serial.begin(115200); WiFi.begin(ssid, password); Serial.print("Connecting"); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(); Serial.println("WiFi Connected!"); Serial.print("IP Address: "); Serial.println(WiFi.localIP()); } void loop() { }
Serial Monitor might show:
Connecting........ WiFi Connected! IP Address: 192.168.1.105
6. What is an IP Address?
An IP address identifies a device on a network.
For example:
Router: 192.168.1.1 Laptop: 192.168.1.10 Phone: 192.168.1.20 ESP32: 192.168.1.105
Think of it like an address for a device.
House Address ↓ Physical location IP Address ↓ Network device
7. Private IP Addresses
In a typical home network, you may see:
192.168.x.x 10.x.x.x 172.16.x.x – 172.31.x.x
For example:
ESP32 = 192.168.1.105
This is normally a private/local IP.
It doesn't automatically mean the ESP32 is directly reachable from the public Internet.
8. DHCP
When your ESP32 connects to a router, the router can automatically assign it an IP address.
This process commonly uses DHCP.
ESP32 │ │ "I need an IP" ▼ Router / DHCP Server │ │ "Use 192.168.1.105" ▼ ESP32
This is why you normally don't need to manually configure an IP.
9. Gateway
The router also acts as the gateway for devices on your network.
Example:
ESP32 192.168.1.105 │ ▼ Gateway 192.168.1.1 │ ▼ Internet
The gateway is generally the device through which traffic leaves the local network.
10. DNS
DNS converts domain names into IP addresses.
For example:
google.com ↓ DNS ↓ IP address
Without DNS, you would often have to use IP addresses instead of domain names.
ESP32 networking can use DNS when communicating with Internet services by hostname.
11. Access Point Mode — AP
Now the opposite situation.
Instead of connecting to your router, the ESP32 creates its own Wi-Fi network.
ESP32 Wi-Fi AP │ ┌─────┼─────┐ │ │ │ Phone Laptop Tablet
For example, ESP32 creates:
SSID: ESP32_Control
Your phone connects directly to it.
12. ESP32 Access Point Example
#include <WiFi.h> const char* ssid = "ESP32_Control"; const char* password = "12345678"; void setup() { Serial.begin(115200); WiFi.softAP(ssid, password); Serial.println("Access Point Started"); Serial.print("ESP32 IP: "); Serial.println(WiFi.softAPIP()); } void loop() { }
The phone can connect to:
ESP32_Control
The ESP32 commonly uses an address such as:
192.168.4.1
in SoftAP mode.
13. STA vs AP
| Feature | Station | Access Point |
|---|---|---|
| ESP32 connects to router | ✅ | ❌ |
| ESP32 creates Wi-Fi | ❌ | ✅ |
| Internet through router | Usually ✅ | Not automatically |
| Phone can connect directly | Not necessarily | ✅ |
| Typical use | IoT/Internet | Local control |
14. STA + AP Mode
You can also use both.
Router │ │ ESP32 / \ / \ Internet AP │ Smartphone
This can be useful when you want:
- Internet connectivity
- Local configuration
- Device setup
- Emergency local control
15. Wi-Fi Scanning
The ESP32 can scan nearby Wi-Fi networks.
ESP32 │ └── Wi-Fi Scan │ ├── Home_WiFi ├── Mobile_Hotspot ├── Office └── ESP32_AP
Example:
#include <WiFi.h> void setup() { Serial.begin(115200); WiFi.mode(WIFI_STA); WiFi.disconnect(); delay(100); Serial.println("Scanning..."); int networks = WiFi.scanNetworks(); Serial.print("Networks found: "); Serial.println(networks); for (int i = 0; i < networks; i++) { Serial.print(i + 1); Serial.print(": "); Serial.print(WiFi.SSID(i)); Serial.print(" | RSSI: "); Serial.println(WiFi.RSSI(i)); } } void loop() { }
You may see:
Networks found: 4 1: HomeWiFi | RSSI: -45 2: MobileHotspot | RSSI: -67 3: OfficeWiFi | RSSI: -78 4: ESP32_AP | RSSI: -82
16. What is RSSI?
RSSI represents received signal strength.
Example:
-40 dBm → Strong -60 dBm → Good -70 dBm → Weaker -80 dBm → Poor
Generally, closer to 0 means stronger signal.
So:
-40 > -70
in signal strength terms.
17. ESP32 Web Control
This is where Wi-Fi becomes extremely useful.
Imagine:
Phone │ │ Wi-Fi ▼ ESP32 Web Server │ ├── Light ├── Fan ├── Door └── Motor
You open:
http://192.168.1.105
and get a webpage:
╔══════════════════════╗ ║ ESP32 CONTROL ║ ╠══════════════════════╣ ║ 💡 LIGHT ON ║ ║ 🌀 FAN OFF ║ ║ 🚪 DOOR OPEN ║ ║ 🚗 MOTOR STOP ║ ╚══════════════════════╝
This is the foundation of an ESP32 web server.
We'll study the actual web server in a later topic.
18. Local Network Communication
Suppose:
ESP32 = 192.168.1.105 Laptop = 192.168.1.10 Phone = 192.168.1.20
They are on the same LAN.
Your phone can communicate with ESP32:
Phone 192.168.1.20 │ │ Wi-Fi ▼ Router │ ▼ ESP32 192.168.1.105
For example:
http://192.168.1.105
19. Local Network vs Internet
This distinction is very important.
Local control
Phone │ Wi-Fi │ ESP32
No Internet is necessarily required.
Internet control
Phone │ Internet │ Cloud / Server │ Internet │ ESP32
This requires a suitable Internet-connected architecture.
20. ESP32 + Internet
Once ESP32 has Internet access, it can communicate with:
ESP32 │ ├── REST API ├── HTTP ├── HTTPS ├── MQTT ├── Firebase ├── ThingSpeak └── Other cloud services
For example:
DHT22 │ ▼ ESP32 │ Wi-Fi │ ▼ Cloud │ ▼ Phone Dashboard
This is a basic IoT architecture.
21. Static IP
Normally DHCP gives your ESP32 an address automatically.
But the address may change.
For example:
Today: 192.168.1.105 After reconnect: 192.168.1.110
If you're building a local web-control system, this can be inconvenient.
A static IP can be configured:
ESP32 ↓ 192.168.1.50
Then you can consistently access:
http://192.168.1.50
However, static IP configuration must be chosen carefully to avoid conflicts with the router's DHCP range.
22. Wi-Fi Connection Handling
Don't always assume Wi-Fi will remain connected.
A better application checks:
if (WiFi.status() == WL_CONNECTED) { // Connected } else { // Disconnected }
For an IoT device:
Wi-Fi │ Connected? / \ YES NO │ │ ▼ ▼ Continue Reconnect
This is especially important for sensors and smart-home devices.
23. Example: Sensor + Wi-Fi
Imagine your DHT22 measures:
Temperature = 29.4°C Humidity = 62%
ESP32:
DHT22 │ ▼ ESP32 │ │ Wi-Fi ▼ Server │ ▼ Dashboard
The dashboard could display:
┌─────────────────────┐ │ SMART HOME │ ├─────────────────────┤ │ Temperature: 29.4°C │ │ Humidity: 62% │ │ Light: ON │ │ Fan: OFF │ └─────────────────────┘
24. Wi-Fi Security
Never hard-code or publish your real Wi-Fi password in public code.
Avoid:
const char* password = "my_real_password";
when uploading code to GitHub.
Instead, use configuration methods or environment/secrets management appropriate to your project.
Also prefer modern secured Wi-Fi networks such as WPA2/WPA3 where available.
25. Common Problems
❌ Wrong SSID
WiFi connection fails
Check spelling and capitalization.
❌ Wrong password
The ESP32 repeatedly tries to connect but never succeeds.
❌ ESP32 connected but webpage doesn't open
Check:
1. ESP32 IP 2. Phone is on same network 3. Firewall/network isolation 4. Correct port 5. Web server actually running
❌ Using old IP
Your router may have assigned a different address.
Always print:
Serial.println(WiFi.localIP());
❌ Weak signal
If RSSI is very low:
ESP32 ←──── far away ────→ Router
connection may become unstable.
🧠 Quick Summary
Remember this architecture:
Wi-Fi Networking │ ┌──────────────┼──────────────┐ │ │ │ STA AP STA + AP │ │ │ Router ESP32 creates Both │ network │ Internet │ Cloud/API
Most important concepts:
- STA → ESP32 connects to Wi-Fi
- AP → ESP32 creates Wi-Fi
- IP address → identifies device on network
- DHCP → automatically assigns IP
- Gateway → route out of local network
- DNS → domain name → IP
- RSSI → Wi-Fi signal strength
- Static IP → manually assigned consistent address
- LAN → local network
- Internet → global network