ESP32 Essential Guide – Network Section – WiFi Chapter – 0 – 1 – How to Connect ESP32 to WiFi

Home / Tutorial / ESP32 Tutorials / ESP32 Essential Guide / Network / WiFi Chapter

AvantMaker.com Article ID
E32-T-EEG-N-W-0-1

1 – Introduction

The ESP32 Development Board (ESP32) stands out from traditional development boards, such as the Arduino Uno, due to its built-in WiFi capabilities. This allows the ESP32 to connect to wireless networks, making it an excellent choice for IoT projects.

In this article, we’ll walk you through the steps to connect, disconnect, and reconnect the ESP32 to a WiFi network. By the end, you’ll have a solid starting point for adding WiFi features to your projects.

2 – Connect ESP32 to WiFi

To get started, we’ll use a simple example code that connects the ESP32 to a WiFi network. Please copy and paste the code below into your Arduino IDE. We’ll provide more details about how it works in the sections that follow, so you can understand each step clearly.

2-1 Example Code for Connecting ESP32 to WiFi

This code connects the ESP32 to a WiFi network using basic credentials and checks if the connection is successful.

/*
 * Author: Avant Maker
 * Date: February 7, 2025
 * Version: 1.0
 * Description: This code demonstrates How to Connect ESP32 to WiFi. It includes connecting to a WiFi network with predefined SSID and password. The program waits until the connection is successfully established before proceeding. Once connected, it prints a confirmation message to the serial monitor.
 * License: MIT 
 * Code Source:  This example code is sourced from the Comprehensive Guide to the ESP32 Arduino Core Library, accessible on AvantMaker.com. For additional code examples and in-depth documentation related to the ESP32 Arduino Core Library, please visit:
 *
 * https://avantmaker.com/references/esp32-arduino-core-index/
 *
 * AvantMaker.com, your premier destination for all things DIY, AI, IoT, Smart Home, and STEM projects. We are dedicated to empowering makers, learners, and enthusiasts with the resources they need to bring their innovative ideas to life.
 */

#include <WiFi.h>

//Change the following WiFi connection information to match your WiFi network settings. Otherwise, your ESP32 will fail to connect to the WiFi network.
const char* ssid = "Your WiFi SSID";           // Your WiFi SSID
const char* password = "Your WiFi password";   // Your WiFi password

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

    // Attempt to connect to Wi-Fi
    WiFi.begin(ssid, password); 

    // Wait until the connection is successful
    while (WiFi.status() != WL_CONNECTED) {
        delay(1000);
        Serial.println("Connecting to Wi-Fi...");
    }
    Serial.println("Connected to Wi-Fi");
}

void loop() {
    // Your code here
}

2-2 Connecting to WiFi Example Code Explanation

The code above is a straightforward way to get your ESP32 online. It starts by setting up the WiFi credentials, attempts to connect to the network, and waits until the connection is established. Once connected, it lets you know through the Serial Monitor—a handy tool for seeing what’s happening inside your ESP32. Think of it like the ESP32 sending you a text message to say, “Hey, I’m on the WiFi now!”

Let’s break down some of the key parts of this code to give you a clearer picture of what’s going on.

Key Code 1: Include the WiFi Library

#include <WiFi.h>

The WiFi library is like the toolbox for your ESP32’s WiFi features. By including it at the start of the code, you’re giving the ESP32 access to all the methods—like connecting and checking status—that make WiFi work. Without this line, the rest of the code wouldn’t know how to talk to the WiFi hardware.

Key Code 2: Provide WiFi Credentials

const char* ssid = "Your WiFi SSID";
const char* password = "Your WiFi password";

These two lines are where you tell the ESP32 which WiFi network to join. The ssid is the name of your WiFi network, and the password is, well, the password! You’ll need to replace the placeholder text with your own details. For example, if your WiFi network is named WiFi_for_ESP32_Tutorial and the password is AvantMaker_Is_The_Best, you’d update the lines to look like this:

const char* ssid = "WiFi_for_ESP32_Tutorial";
const char* password = "AvantMaker_Is_The_Best";

Since most of the code in this section revolves around WiFi, we won’t keep reminding you to update these credentials for every example. Just make sure to swap them out with your own whenever you see them—don’t forget!

Key Code 3: Attempt to Connect to WiFi with ESP32 WiFi Library’s begin MethodWiFi.begin(ssid, password);

WiFi.begin(ssid, password);

The WiFi.begin() method is the ESP32’s way of saying, “Let’s get connected to WiFi!” It takes the SSID and password you provided and starts the process of linking up with your WiFi network. If you’re curious about all the ways you can use this method—like connecting to a specific channel or access point—we’ve got a detailed reference page packed with info and extra examples. Check it out here:

ESP32 Arduino Core – WiFi Library – begin

Key Code 4: Check Connection Condition with ESP32 WiFi Library’s status Method

while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to Wi-Fi...");
}

This chunk of code is all about patience. The WiFi.status() method checks how the WiFi connection is going and returns a value to let you know the current state. Here, we’re specifically looking for WL_CONNECTED, which means the ESP32 has successfully joined the network. Until that happens, the while loop keeps things on hold, printing “Connecting to Wi-Fi…” to the Serial Monitor every second so you know it’s still trying.

The status method is super useful for keeping tabs on your connection, and there’s a lot more to explore about it. We’ve put together a full reference page with all the details at AvantMaker.com—definitely worth a look for deeper insights:

ESP32 Arduino Core – WiFi Library – status

3 – Disconnect the ESP32’s WiFi Connection

Sometimes you need to drop your ESP32’s WiFi connection—maybe to save power or switch networks. To do this, the ESP32 WiFi library offers the disconnect() method. It’s a simple way to tell your ESP32, “Hey, let’s hang up the WiFi call for now,” and end the current connection.

3-1 Disconnect the ESP32’s WiFi Connection Example Code

This code shows how to disconnect the ESP32 from a WiFi network after establishing a connection.

/*
 * Author: Avant Maker
 * Date: February 7, 2025
 * Version: 1.0
 * Description: This code demonstrates how to disconnect ESP32's WiFI Connection.
 * License: MIT 
 * Code Source: This example code is sourced from the Comprehensive Guide to the ESP32 Arduino Core Library, accessible on AvantMaker.com. For additional code examples and in-depth documentation related to the ESP32 Arduino Core Library, please visit:
 *
 * https://avantmaker.com/references/esp32-arduino-core-index/
 *
 * AvantMaker.com, your premier destination for all things DIY, AI, IoT, Smart Home, and STEM projects. We are dedicated to empowering makers, learners, and enthusiasts with the resources they need to bring their innovative ideas to life.
 */

#include <WiFi.h>

const char* ssid = "Your WiFi SSID";           // Your WiFi SSID
const char* password = "Your WiFi password";   // Your WiFi password

void setup() {
  Serial.begin(115200);
  
  // Attempt to connect to Wi-Fi
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  
  Serial.println("Connected to WiFi");
  
  // Wait for 5 seconds
  delay(5000);
  
  // Disconnect with default settings
  if (WiFi.disconnect()) {
    Serial.println("Disconnected from WiFi");
  } else {
    Serial.println("Failed to disconnect from WiFi");
  }
}

void loop() {
  // Your code here
}

3-2 Disconnect WiFi Example Code Explanation

The code above starts by connecting your ESP32 to a WiFi network, just like in the first part of this tutorial.

Then, ESP32 uses the disconnect() method to cut the connection. It’s like flipping a switch to turn off the WiFi connection. The ESP32 informs you whether the disconnection was successful by sending a message to the Serial Monitor.

Let’s zoom in on the key part of this code to see how the disconnection happens.

Key Code: Disconnect WiFi Library

  if (WiFi.disconnect()) {
    Serial.println("Disconnected from WiFi");
  } else {
    Serial.println("Failed to disconnect from WiFi");
  }

This snippet is where the disconnection action happens. The WiFi.disconnect() method tells the ESP32 to end its WiFi connection. It returns true if it succeeds and false if something goes wrong—like if it was already disconnected or hit a glitch. The if statement checks this result and prints a message to the Serial Monitor, so you’ll know right away if it worked. It’s a handy way to confirm the ESP32 is offline.

Please note that this example uses the default settings for disconnect(), keeping things simple. However, if you need more control—such as turning off the WiFi radio completely or clearing saved credentials—you can achieve this by providing custom parameters to the disconnect method. We’ve got all the details on how to do that at AvantMaker.com’s reference page for the disconnect method. Take a look here:

ESP32 Arduino Core – WiFi Library – disconnect

3 – Reconnect the ESP32 to WiFi

Getting your ESP32 back online is just as easy—you can use the begin() method again to reconnect to the network. Please don’t tell me you’ve never heard of begin() method. We just talked about it earlier in this article!

If you need example code to demonstrate how to reconnect the ESP32 to WiFi, sorry—we won’t spoon-feed you the code this time! Consider it a little homework assignment: try adding the reconnection steps to the example code above yourself. This is a great way to practice what you’ve learned so far.

If you get stuck or want to double-check your work, don’t worry—we’ve got you covered. Head over to AvantMaker.com’s disconnect method reference page, where you’ll find an example that includes wifi reconnection process. Here’s the link:

ESP32 Arduino Core – WiFi Library – disconnect

4 Conclusion

In this tutorial article, we’ve tackled the basics of managing the ESP32’s WiFi connection. You’ve learned how to use the WiFi.begin() method to connect to a network and the WiFi.disconnect() method to drop that connection when you’re done. These are the building blocks for controlling when and how your ESP32 goes online.

BUT…

While your ESP32 can now join a WiFi network, it’s not doing much yet. To let your ESP32 know how to complete tasks like sending sensor data or fetching weather updates, we’ll need to dig into some networking basics first. That’s coming up in our next tutorial article: Fetching Essential Online Data with the ESP32.

ESP32 Essential Guide Index

Network Section

Ready to experiment and explore more about ESP32? Visit our website’s All About ESP32 Resources Hub, packed with tutorials, guides, and tools to inspire your maker journey. Experiment, explore, and elevate your skills with everything you need to master this powerful microcontroller platform!

error: Content is protected !!