esp32 beginner 15 min

ESP32: connect to Wi-Fi and stay connected

The minimum code to bring up Wi-Fi on an ESP32, with reconnect logic and the most common reasons it silently fails.

Code available for: ESP32 Arduino
Published Aug 5, 2026

Bringing up Wi-Fi on an ESP32 is two lines of code. Keeping it connected when the router decides to be difficult is a different problem.

This tutorial is short on purpose. The whole thing is the code below plus a checklist of what to try when it does not work.

The minimum viable code

#include <WiFi.h>

const char* ssid     = "your-wifi-ssid";
const char* password = "your-wifi-password";

void setup() {
  Serial.begin(115200);
  delay(1000);   // let Serial settle
  WiFi.begin(ssid, password);

  Serial.print("Connecting");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  Serial.print("Connected. IP: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  // nothing to do
}

Upload, open Serial Monitor at 115200. You should see the dots, then an IP.

The reconnect logic

The minimum version above does not reconnect if the router drops the connection. For any real project, add this:

#include <WiFi.h>

const char* ssid     = "your-wifi-ssid";
const char* password = "your-wifi-password";

unsigned long lastReconnectAttempt = 0;
const unsigned long RECONNECT_INTERVAL = 30000;   // 30 seconds

void connectWifi() {
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  WiFi.mode(WIFI_STA);
}

void setup() {
  Serial.begin(115200);
  delay(1000);
  connectWifi();
}

void checkWifi() {
  if (WiFi.status() != WL_CONNECTED) {
    unsigned long now = millis();
    if (now - lastReconnectAttempt > RECONNECT_INTERVAL) {
      lastReconnectAttempt = now;
      Serial.println("Wi-Fi lost, reconnecting...");
      connectWifi();
    }
  }
}

void loop() {
  checkWifi();
  // do your stuff
}

The 30-second interval matters. If you hammer WiFi.begin() every time loop() runs and Wi-Fi is not connected, you can wedge the chip.

Why your ESP32 will not connect

In rough order of how often I see them:

  1. Wrong SSID or password. Typos. Capitalization matters. Underscores vs. spaces. The Serial Monitor will show the connection attempt but not the reason it failed.
  2. 5GHz Wi-Fi. The ESP32 only does 2.4GHz. If your router has both bands with the same SSID, force the device onto 2.4GHz in the router settings.
  3. Captive portal. Coffee shop Wi-Fi and most guest networks. Use your phone hotspot or home Wi-Fi for testing.
  4. WPA3. Some routers default to WPA3-only. The ESP32 does WPA2. Change the router to WPA2 or WPA2/WPA3 mixed.
  5. AP isolation. On some routers, devices on Wi-Fi cannot talk to each other. This does not affect outbound connections but it will break your “browser on phone connects to ESP32” tests.
  6. MAC filtering. Some routers have it on. Add the ESP32’s MAC to the allow list, or turn MAC filtering off (the right answer for a home network).

You can see the ESP32’s MAC by adding this to setup():

Serial.print("MAC: ");
Serial.println(WiFi.macAddress());

How to debug a stuck connection

If you are staring at “Connecting…” with no dots and no errors, add this:

Serial.print("Status: ");
Serial.println(WiFi.status());

WiFi.status() returns:

  • 0: WL_IDLE_STATUS
  • 1: WL_NO_SSID_AVAIL (SSID not found)
  • 2: WL_SCAN_COMPLETED
  • 3: WL_CONNECTED
  • 4: WL_CONNECT_FAILED (wrong password)
  • 5: WL_CONNECTION_LOST
  • 6: WL_DISCONNECTED

Print it every few seconds. The number tells you where it is getting stuck.

Static IP vs. DHCP

Most projects use DHCP. If you need a static IP (e.g. for a known URL), configure it before WiFi.begin():

IPAddress localIP(192, 168, 1, 42);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress dns(8, 8, 8, 8);

WiFi.config(localIP, gateway, subnet, dns);
WiFi.begin(ssid, password);

I only do this for the “I want this device to always be at the same URL” case. For everything else, DHCP + mDNS is fine.

The mDNS trick (so you can use http://esp32.local)

After WiFi.begin(), enable mDNS:

if (MDNS.begin("esp32")) {
  Serial.println("mDNS started. Try http://esp32.local");
}

Now any device on the network can reach your ESP32 at http://esp32.local instead of fishing the IP out of the Serial Monitor. This is the single biggest quality-of-life improvement for ESP32 projects.

When the router is the bottleneck

If your ESP32 works at home but not at a different location, the problem is almost always the router. Two settings to check:

  • Beacon interval. Most home routers are fine. Some enterprise gear pushes 100ms beacons which can confuse older ESP32 firmwares.
  • DTIM interval. Higher DTIM = more battery life for clients but worse for ESP32 deep sleep wakeup latency.

You usually do not need to change these. But if you are deploying ESP32s across multiple sites and some of them are flaky, these are the levers.

What to build next

  • The MQTT tutorial publishes your first data.
  • The web server tutorial serves it to the LAN.
  • The book IoT with ESP32 bundles the foundations.