Skip to content

How to Connect ESP32 to Wi-Fi

DodaTech Updated 2026-06-24 3 min read

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

  1. Using foldl instead of foldl' causing stack overflow on large lists
  2. Forgetting deriving (Show, Eq) on custom data types needed for debugging
  3. 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

### Why does my ESP32 keep disconnecting from Wi-Fi?

Power saving mode (default) turns off Wi-Fi periodically. Call WiFi.setSleep(WIFI_PS_NONE) to disable it. Other causes: weak signal, channel interference, or router DHCP lease timeout.

Can the ESP32 connect to enterprise Wi-Fi (WPA2-Enterprise)?

Yes, use the WiFi.begin() variant with identity and CA cert: WiFi.begin(ssid, WPA2_AUTH_PEAP, identity, username, password, ca_cert). Enable WiFi.setAutoReconnect(true) for persistent connections.

What is the maximum Wi-Fi range for ESP32?

ESP32 range is approximately 50 meters indoors and 150 meters outdoors with a standard antenna. Use an external antenna with the IPEX connector on ESP32 boards that support it for extended range.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro