Embedded Systems

ESP32 Beginner Course #9: WiFi Basics and Network Connection

Connect the ESP32 to a WiFi network, understand SSID and password settings, print connection status, use reconnect logic, and find the board IP address.

ESP32WiFiIoTArduino IDENetworking

Introduction

The ESP32 is not just a microcontroller. It also has built-in WiFi.

That single feature is one of the reasons the ESP32 is so popular. It can read sensors, control hardware, and communicate over a network without needing an extra WiFi module.

In this lesson, we will connect the ESP32 to a WiFi network and print its local IP address in Serial Monitor.

What you will learn

By the end of this tutorial, you should understand:

  • Why WiFi is useful in ESP32 projects
  • What SSID and password mean
  • How to include the ESP32 WiFi library
  • How to connect to a router
  • How to print connection status
  • How to find the ESP32 local IP address
  • How to add simple timeout and reconnect logic
  • Common WiFi troubleshooting steps

Why WiFi matters

WiFi allows the ESP32 to communicate without a USB cable.

With WiFi, the ESP32 can:

  • Send sensor values to a dashboard
  • Receive commands from a browser
  • Talk to MQTT brokers
  • Send data to cloud services
  • Control devices from a phone
  • Receive firmware updates over the network

Before building those projects, we need one basic skill: connect to WiFi reliably.

SSID and password

The SSID is the name of your WiFi network.

The password is the password for that network.

In code, they are stored as text strings:

const char* ssid = "YOUR_WIFI_NAME";
const char* password = "YOUR_WIFI_PASSWORD";

Be careful when sharing code online. Do not publish your real WiFi password.

Hardware required

  • ESP32 DevKit
  • USB cable
  • WiFi network
  • Computer with Arduino IDE

No external components are required for this lesson.

Basic WiFi connection code

#include <WiFi.h>

const char* ssid = "YOUR_WIFI_NAME";
const char* password = "YOUR_WIFI_PASSWORD";

void setup()
{
    Serial.begin(115200);

    WiFi.begin(ssid, password);
    Serial.print("Connecting to WiFi");

    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()
{
}

Upload the sketch and open Serial Monitor at 115200 baud.

If the connection succeeds, you should see the ESP32 print its IP address.

Code explanation

The ESP32 WiFi library is included with:

#include <WiFi.h>

The connection starts with:

WiFi.begin(ssid, password);

Then the program waits while the board is not connected:

while (WiFi.status() != WL_CONNECTED)

Each dot printed to Serial Monitor means the ESP32 is still trying to connect.

After connection, this line prints the local IP address:

Serial.println(WiFi.localIP());

That IP address identifies the ESP32 on your local network.

Add a timeout

In real projects, it is better not to wait forever.

Use a timeout so the program can show an error if WiFi fails:

int attempts = 0;

while (WiFi.status() != WL_CONNECTED && attempts < 20)
{
    delay(500);
    Serial.print(".");
    attempts++;
}

if (WiFi.status() == WL_CONNECTED)
{
    Serial.println();
    Serial.println("WiFi connected");
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
}
else
{
    Serial.println();
    Serial.println("WiFi connection failed");
}

This makes debugging easier and prevents the program from getting stuck forever.

Simple reconnect check

For long-running projects, WiFi may disconnect. You can check the connection in loop():

void loop()
{
    if (WiFi.status() != WL_CONNECTED)
    {
        Serial.println("WiFi lost. Reconnecting...");
        WiFi.disconnect();
        WiFi.begin(ssid, password);
    }

    delay(5000);
}

This is a basic reconnect pattern. More advanced projects may use timers, state machines, or event callbacks.

Common WiFi mistakes

If the ESP32 does not connect:

  • Check SSID spelling
  • Check password spelling
  • Remember that both are case-sensitive
  • Make sure the ESP32 is close enough to the router
  • Use a 2.4 GHz network if your board does not support 5 GHz
  • Make sure the USB cable and power source are stable

WiFi can draw more current than a simple LED project, so weak power can cause unstable behavior.

Real engineering use cases

WiFi is the foundation for:

  • ESP32 web servers
  • Sensor dashboards
  • MQTT devices
  • Home automation
  • Remote monitoring
  • Cloud-connected devices
  • OTA firmware updates

This lesson is intentionally simple because every later IoT project depends on this connection step.

Engineering challenge

Modify the code to print:

  • WiFi connected message
  • IP address
  • Signal strength using WiFi.RSSI()
  • A warning if WiFi fails to connect

Next lesson

Next, we will use this WiFi connection to build a simple ESP32 web server. You will open a browser and control an LED from a web page hosted by the ESP32.