Learn / Communication / Wi-Fi and Bluetooth

Connecting an ESP32 to Wi-Fi

ReviewedBeginnerFreeAbout 6 min
NRF24
In short

Connecting an ESP32 to Wi-Fi: the short answer

4 GHz Wi-Fi radio. 4 GHz name. Example: −78 dBm is too weak for reliable use. Each 3 dB is double or half the power, so a better antenna or placement makes a big difference.

Level
Beginner
Time
About 6 min
Topic
Communication
You will learn to

Connect an ESP32 to your Wi-Fi, serve a simple web page and recover automatically when the connection drops.

Before this: Getting Started with the ESP32

The ESP32 has a 2.4 GHz Wi-Fi radio. It cannot see 5 GHz networks, so if your router has separate bands, connect to the 2.4 GHz name. In station mode the ESP32 joins your router like a phone: WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); then wait until WiFi.status() == WL_CONNECTED and print WiFi.localIP(). Keep the password out of code you share by placing it in a separate secrets.h file that you do not upload to GitHub.

Once connected, the ESP32 can be a small web server. The WebServer library lets you write server.on("/", handleRoot); where handleRoot sends an HTML page with a reading in it. Open the IP address in your phone's browser on the same network. Registering a name with MDNS.begin("plant") lets you open http://plant.local instead of remembering the IP, though some Android phones do not support .local names.

Wi-Fi drops happen: the router restarts, the signal fades or the power flickers. Never assume the connection lasts. In loop(), check WiFi.status() every few seconds and call WiFi.reconnect() if it is lost, without blocking your sensor code. Use non-blocking timing with millis() instead of long delay() calls. In access point mode (WiFi.softAP) the ESP32 creates its own network, which is useful for setup screens where a user enters their home Wi-Fi details.

What happens when you call WiFi.begin()

The ESP32 scans for your network name, authenticates with the password, then asks the router for an IP address by DHCP. Each step can fail for its own reason: wrong name or password, a 5 GHz-only network, weak signal, or a router that limits the number of devices. WiFi.status() returns different codes for these, and printing it tells you where it failed.

Names are case sensitive and spaces matter. A hotspot on a phone must be set to 2.4 GHz, and some routers use a combined name for both bands that confuses older chips.

Serving a page, safely

The WebServer library handles routes with server.on(). Call server.handleClient() often in loop(); a long delay() stops pages from loading. Keep the page small: build the HTML as a short string, and refresh data with a few lines of JavaScript that fetch a small JSON route instead of reloading everything.

A page served by the ESP32 is plain HTTP, readable by anyone on the same network. Do not show passwords or put controls on it that could do harm, such as switching mains, without a login, and do not open its port to the internet through your router.

Making it recover from failures

Check the connection with a timer rather than a delay: every few seconds, if WiFi.status() is not WL_CONNECTED, call WiFi.reconnect(). Give each attempt time to finish before trying again. After a long failure, a restart (ESP.restart()) is a reasonable last step for an unattended device.

Show the state locally: a status LED that blinks differently when connected, searching, or failed, so you can tell at a glance without a serial cable. Store readings while offline and send them when the link returns.

Power, signal and set-up screens

Wi-Fi bursts draw 150 to 300 mA for milliseconds. A weak USB cable or a thin battery supply makes the voltage dip, which shows up as random reboots when Wi-Fi starts (a brown-out message in the log). A 100 to 470 µF capacitor near the module and a decent supply fix most of it.

To let a user enter their own Wi-Fi details, start in access point mode with a simple page, save the details to flash with the Preferences library, then switch to station mode. The WiFiManager library packages this.

Common mistakes
  • Putting the password in a sketch that goes on GitHub.
  • Using delay() in loop() and wondering why the web page stalls.
  • Powering the board from a weak supply and blaming the code for the resets.
Worked example

Is the Wi-Fi signal good enough?

  1. WiFi.RSSI() returns signal strength in dBm, for example −58.
  2. Around −30 to −60 dBm is strong, −60 to −70 is fine, −70 to −80 is weak and below −80 is unreliable.
  3. The device reads −78 dBm in the garden, so pages load slowly and it disconnects often.
  4. Fix: move the router or use an ESP32 board with an external antenna, such as the WROOM-32U.

Result. −78 dBm is too weak for reliable use. Each 3 dB is double or half the power, so a better antenna or placement makes a big difference.

Try it

Connect an ESP32 to your Wi-Fi and serve a page showing the uptime in seconds and the signal strength from WiFi.RSSI(). Restart your router while it runs and make sure the ESP32 reconnects on its own and the page works again.

Check yourself

3 quick questions

  1. Why can't the ESP32 find your home network?

  2. Which RSSI value is the strongest signal?

  3. Why avoid long delay() calls in a Wi-Fi project?

Common questions

What will I learn in "Connecting an ESP32 to Wi-Fi"?

Connect an ESP32 to your Wi-Fi, serve a simple web page and recover automatically when the connection drops.

Is the Wi-Fi signal good enough?

WiFi.RSSI() returns signal strength in dBm, for example −58. Around −30 to −60 dBm is strong, −60 to −70 is fine, −70 to −80 is weak and below −80 is unreliable. The device reads −78 dBm in the garden, so pages load slowly and it disconnects often. Fix: move the router or use an ESP32 board with an external antenna, such as the WROOM-32U. −78 dBm is too weak for reliable use. Each 3 dB is double or half the power, so a better antenna or placement makes a big difference.

Why can't the ESP32 find your home network?

The router is on 5 GHz only. The ESP32 supports 2.4 GHz Wi-Fi only.

Which RSSI value is the strongest signal?

−45 dBm. Closer to zero is stronger.

Why avoid long delay() calls in a Wi-Fi project?

They block reconnect checks and web requests. Non-blocking code keeps the server and reconnect logic running.