ESP32: How to Connect to Wi-Fi

Temp

📡 The ESP32 is a powerful, Wi-Fi-enabled microcontroller that’s perfect for IoT applications. Whether you’re building a smart home device or a weather station, getting it connected to Wi-Fi is your first step toward unlocking its potential. This tutorial will guide you through everything you need to connect your ESP32 to a Wi-Fi network using the Arduino IDE.


🧰 What You’ll Need

  • ESP32 Development Board (any variant)
  • Micro-USB cable
  • Arduino IDE (installed and configured for ESP32)
  • Wi-Fi credentials (SSID & Password)

🔧 Step 1: Set Up the Arduino IDE for ESP32

If you haven’t already configured the Arduino IDE for ESP32 development:

  1. Open the Arduino IDE
  2. Go to File > Preferences
  3. In the Additional Boards Manager URLs field, paste:
    https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/packageesp32index.json
  4. Go to Tools > Board > Boards Manager
  5. Search for ESP32 and install the latest package

🌐 Step 2: Connect to Wi-Fi – Basic Code Example

Here’s a minimal code example to connect your ESP32 to a Wi-Fi network:

#include <WiFi.h>

const char* ssid = "Your_SSID";
const char* password = "Your_PASSWORD";

void setup() {
  Serial.begin(115200);
  delay(1000);
  Serial.println("\nConnecting to WiFi...");

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("\nWiFi connected!");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  // Nothing needed here for now
}

⚠️ Replace Your_SSID and Your_PASSWORD with your actual Wi-Fi credentials.


🧪 Step 3: Upload and Monitor

  1. Plug in your ESP32 via USB
  2. Select the correct board under Tools > Board (e.g., “ESP32 Dev Module”)
  3. Choose the correct COM port
  4. Click Upload
  5. Open the Serial Monitor at 115200 baud to view connection status and IP address

🔍 Troubleshooting Connection Issues

Issue Possible Fix
Stuck on “Connecting…” Check SSID/password spelling, or network security
Doesn’t show up in Serial Select correct COM port under Tools > Port
Upload fails Hold BOOT button while uploading (on some boards)

🧠 Bonus: Wi-Fi Status Handling

Add robust connection checking:

if (WiFi.status() != WL_CONNECTED) {
  Serial.println("WiFi Disconnected. Trying to reconnect...");
  WiFi.begin(ssid, password);
}

Or use event handlers for smarter reconnection:

WiFi.onEvent([](WiFiEvent_t event) {
  Serial.println("WiFi disconnected, reconnecting...");
  WiFi.begin(ssid, password);
}, WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);

📲 Where to Go From Here

Once connected, your ESP32 can:
– 🌐 Serve web pages (HTTP server)
– ☁️ Send data to APIs (REST, MQTT)
– 📊 Push sensor data to cloud platforms like Blynk or ThingSpeak


🎯 Final Thoughts

Connecting your ESP32 to Wi-Fi is the gateway to building real-world IoT projects. With just a few lines of code, you can get your device online and ready for remote interaction, data logging, or smart automation.

📡 Now that your ESP32 is Wi-Fi-enabled, the sky (and the cloud) is the limit!