ESP32: Connecting to a WiFi network

The objective of this post is to explain how to connect the ESP32 to a WiFi network, using the Arduino IDE.

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 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

27 Replies to “ESP32: Connecting to a WiFi network”

  1. Currently I use ESP32 in STA mode, as a client while using an external AP device to exchange data with other clients. I want to use ESP32 AP_STA mode first to set up a softAP with SSID and Passwd, then connect the same ESP32 as STA to this softAP (in the same ESP32), then connect other clients to this ESP32 softAP also (using the same channel), thus I can remove the external AP device. Anyone has a solution and sample code?

    Best

  2. Currently I use ESP32 in STA mode, as a client while using an external AP device to exchange data with other clients. I want to use ESP32 AP_STA mode first to set up a softAP with SSID and Passwd, then connect the same ESP32 as STA to this softAP (in the same ESP32), then connect other clients to this ESP32 softAP also (using the same channel), thus I can remove the external AP device. Anyone has a solution and sample code?
    Best

  3. How can i connect my esp32 to a wifi network that requires me to fill a authentication webpage after connecting to it? Thank you.

    1. Hi!

      Unfortunately I don’t know how to do it since I never investigated such scenario.

      My recommendation is to ask around the ESP32 forum, since someone there probably already tested it:
      https://esp32.com/

      Let us know if you find a solution 🙂

      Best regards,
      Nuno Santos

  4. How can i connect my esp32 to a wifi network that requires me to fill a authentication webpage after connecting to it? Thank you.

    1. Hi!
      Unfortunately I don’t know how to do it since I never investigated such scenario.
      My recommendation is to ask around the ESP32 forum, since someone there probably already tested it:
      https://esp32.com/
      Let us know if you find a solution 🙂
      Best regards,
      Nuno Santos

  5. Have a nice day.
    I wonder if I can do the same Internet connection like the above example but using the ethernet cable instead. My ESP32 board has RJ45 with LAN8720. I’ve searched a lot for this case but found nothing useful. Thanks for your help.

    1. Hi!

      Thanks and likewise 🙂

      Unfortunately I haven’t yet tested connections with LAN so I cannot help on this subject :/

      My recommendation is to search / ask around the ESP32 forum, where someone may be able to help 🙂

      https://esp32.com/

      Best regards,
      Nuno Santos

  6. Have a nice day.
    I wonder if I can do the same Internet connection like the above example but using the ethernet cable instead. My ESP32 board has RJ45 with LAN8720. I’ve searched a lot for this case but found nothing useful. Thanks for your help.

    1. Hi!
      Thanks and likewise 🙂
      Unfortunately I haven’t yet tested connections with LAN so I cannot help on this subject :/
      My recommendation is to search / ask around the ESP32 forum, where someone may be able to help 🙂
      https://esp32.com/
      Best regards,
      Nuno Santos

  7. Hi there,

    At the moment, I’m using the ESP32 to control relay modules over the Internet. I can connect my board to the WiFi network successfully. What I want now is to use the ethernet cable instead of WiFi (my board has RJ45 with LAN8720) but I cannot find any example useful. Could you please give me some suggestion or examples?

    Thanks for your help.

    Have a nice day

  8. Hi there,
    At the moment, I’m using the ESP32 to control relay modules over the Internet. I can connect my board to the WiFi network successfully. What I want now is to use the ethernet cable instead of WiFi (my board has RJ45 with LAN8720) but I cannot find any example useful. Could you please give me some suggestion or examples?
    Thanks for your help.
    Have a nice day

  9. Hi can we not use WiFi.status() to check if the server closed the connection?
    In below code it not works..
    do {
    ws = WiFi.status();
    Serial.print(“client socket status=”); Serial.println(ws);
    delay(1000);
    } while (ws == 3);

    How can I check if the server closed the loop? Thx

  10. Hi can we not use WiFi.status() to check if the server closed the connection?
    In below code it not works..
    do {
    ws = WiFi.status();
    Serial.print(“client socket status=”); Serial.println(ws);
    delay(1000);
    } while (ws == 3);
    How can I check if the server closed the loop? Thx

Leave a Reply to Mateus Cancel reply