How to Connect ESP32 to Wi-Fi
In this tutorial, you'll learn about How to Connect ESP32 to Wi. We cover key concepts, practical examples, and best practices.
The Problem
Your ESP32 cannot connect to Wi-Fi. The serial monitor shows [W][WiFiClient.cpp:XXX] connect(): hostname not found, Connecting . . . . . forever, or WiFi status: 6 (WL_DISCONNECTED). Without a working Wi-Fi connection, the ESP32 cannot send sensor data or serve web pages.
Quick Fix
Fix 1: Wrong SSID or Password
WRONG — hardcoding credentials with typos:
const char* ssid = "MyNetwork"; // wrong case: "Mynetwork"
const char* password = "password123"; // wrong password
RIGHT — verify credentials match the router exactly:
#include <WiFi.h>
const char* ssid = "MyNetwork";
const char* password = "password123";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.print("Connecting to ");
Serial.println(ssid);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected! IP: ");
Serial.println(WiFi.localIP());
}
// Output:
// Connecting to MyNetwork
// .........
// Connected! IP: 192.168.1.42
Fix 2: AP Not Found
WRONG — the ESP32 only scans 2.4 GHz networks (no 5 GHz support):
// ESP32 does NOT support 5 GHz Wi-Fi
// If your router uses the same SSID for 2.4 GHz and 5 GHz, this works
// But if the 2.4 GHz radio is disabled, the ESP32 cannot connect
RIGHT — ensure 2.4 GHz band is enabled on the router:
# Check available networks with an ESP32 scan:
#include <WiFi.h>
void setup() {
Serial.begin(115200);
int n = WiFi.scanNetworks();
Serial.println("Networks found:");
for (int i = 0; i < n; i++) {
Serial.print(WiFi.SSID(i));
Serial.print(" (");
Serial.print(WiFi.RSSI(i));
Serial.println(" dBm)");
}
}
// Output:
// Networks found:
// MyNetwork (-45 dBm)
// GuestWiFi (-62 dBm)
Fix 3: Connection Timeout
WRONG — waiting forever without a timeout:
while (WiFi.status() != WL_CONNECTED) {
delay(500);
// (infinite loop if connection never succeeds)
}
RIGHT — add a timeout:
int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 20) {
delay(500);
Serial.print(".");
attempts++;
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("Connected!");
} else {
Serial.println("Failed to connect. Restarting...");
ESP.restart();
}
Fix 4: Automatic Reconnection
WRONG — not handling disconnections:
// (ESP32 loses connection and never reconnects)
RIGHT — add reconnection logic in loop:
void loop() {
if (WiFi.status() != WL_CONNECTED) {
Serial.println("Reconnecting...");
WiFi.disconnect();
WiFi.reconnect();
delay(5000);
}
// main logic here
}
Fix 5: Power Saving Mode Disconnects
WRONG — default power saving turns off Wi-Fi periodically:
// (ESP32 disconnects from AP every few seconds to save power)
RIGHT — disable power saving mode:
WiFi.setSleep(WIFI_PS_NONE);
Fix 6: Static IP Configuration
IPAddress local_IP(192, 168, 1, 100);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress primaryDNS(8, 8, 8, 8);
if (!WiFi.config(local_IP, gateway, subnet, primaryDNS)) {
Serial.println("Failed to configure Static IP");
}
WiFi.begin(ssid, password);
Use DodaTech's Wi-Fi Analyzer to scan channels, measure signal strength, and recommend optimal router placement for ESP32 deployments.
Prevention
- Use 2.4 GHz band only (ESP32 does not support 5 GHz).
- Add connection timeout logic to prevent infinite loops.
- Implement auto-reconnection in the main loop.
- Keep credentials in a separate config file or use WiFiManager.
- Test with a known-working network during development.
Common Mistakes with connect wifi
- Using
foldlinstead offoldl'causing stack overflow on large lists - Forgetting
deriving (Show, Eq)on custom data types needed for debugging - Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
These mistakes appear frequently in real-world ESP32 code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro