ESP32: Connecting to a WiFi network

Introduction

The objective of this post is to explain how to connect the ESP32 to a WiFi network, using the Arduino IDE. Luckily for those of us who have prior experience with the ESP8266 Arduino IDE libraries, the procedure is very similar.

If you haven’t yet installed the ESP32 Arduino IDE support, please check here how to do it.

If you prefer, you can check a video tutorial at my YouTube channel.

The tests shown below were performed on a ESP32-E FireBeetle board from DFRobot.

The code

Since for this simple example we will just connect to a WiFi network, we will do all the coding in the setup function.

First of all, we need to include the WiFi.h library, which will allow us to connect to the network. You can check the implementation of this library here. It’s interesting to note that the developers opted for a more generic library name, as opposed to the ESP8266, where the library was called ESP8266WiFi.h.

Nevertheless, as we will see latter, the functionality is also exposed as an extern variable called WiFi, in this case of class WiFiClass.

#include "WiFi.h"

For keeping our code easy to edit, we will declare two global variables, for holding both the name of the WiFi network where we want to connect to, and its password. Please use the credentials of your network.

const char* ssid = "yourNetworkName";
const char* password =  "yourNetworkPass";

Now, we will specify the setup function, where we will actually connect to the WiFi network. But first, we will open a serial connection, so we can output the result of the program.

Then, we call the begin method on the WiFi object, passing as arguments the SSID (network name) and password variables specified early. This will start the connection to the network.

Serial.begin(115200);
WiFi.begin(ssid, password);

After that, we will do a while loop until the connection is effectively established. To do so, we can call the status method on the WiFi object and wait for the result to match the WL_CONNECTED enum. Between each iteration, we introduce a small delay, to avoid a constant poll.

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

After the loop, the ESP32 should be successfully connected to the WiFi network. Check the full source code bellow.

#include "WiFi.h"

const char* ssid = "yourNetworkName";
const char* password =  "yourNetworkPass";

void setup() {

  Serial.begin(115200);

  WiFi.begin(ssid, password);

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

  Serial.println("Connected to the WiFi network");

}

void loop() {}

Testing the code

To test the code, just upload it to the board and open the serial console. You should get a result similar to figure 1.

ESP32 Connecting to WiFi Network
Figure 1 – ESP32 connection to WiFi network.

Related Posts

58 thoughts on “ESP32: Connecting to a WiFi network”

  1. Hi, is this code reconnecting after the router has been switched off maybe for some time? Would be a bummer, if one would need to physically reset ESP32 after such an condition.

    1. Hi,
      this code will not automatically reconnect as it is located excusively in the setup function.
      You can however move it to the Loop an place it inside an if(!WL_CONNECTED) expression.
      I’m sure Antepher has a tutorial for this as well:)

Leave a Reply to antepherCancel reply

Discover more from techtutorialsx

Subscribe now to keep reading and get access to the full archive.

Continue reading