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

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

  1. Pingback: ESP32 Arduino: HTTPS GET REQUEST | techtutorialsx

  2. Pingback: ESP32 Arduino: HTTPS GET REQUEST | techtutorialsx

  3. Pingback: ESP32 Arduino: HTTPS GET Request – Arduino Boards News

  4. Pingback: ESP32 Arduino: HTTPS GET Request – Arduino Boards News

  5. Pingback: ESP32 Arduino: Asynchronous HTTP webserver | techtutorialsx

  6. Pingback: ESP32 Arduino: Asynchronous HTTP webserver | techtutorialsx

  7. Pingback: ESP32 Arduino async HTTP server: Serving HTML | techtutorialsx

  8. Pingback: ESP32 Arduino async HTTP server: Serving HTML | techtutorialsx

  9. Pingback: ESP32 Arduino async HTTP server: Serving a HTML page from PROGMEM | techtutorialsx

  10. Pingback: ESP32 Arduino async HTTP server: Serving a HTML page from PROGMEM | techtutorialsx

  11. Pingback: ESP32 Arduino HTTP server: route not found handling | techtutorialsx

  12. Pingback: ESP32 Arduino HTTP server: route not found handling | techtutorialsx

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

    1. Hi!

      I don’t have code for that specific scenario you are trying to achieve.

      Nonetheless, the WiFiManager solution seems to be a good starting point since it supports similar functionality:
      https://github.com/zhouhan0126/WIFIMANAGER-ESP32

      I think it doesn’t cover the full scenario you are describing, but it should be a good example.

      Hope this helps 🙂

      Best regards,
      Nuno Santos

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

    1. Hi!
      I don’t have code for that specific scenario you are trying to achieve.
      Nonetheless, the WiFiManager solution seems to be a good starting point since it supports similar functionality:
      https://github.com/zhouhan0126/WIFIMANAGER-ESP32
      I think it doesn’t cover the full scenario you are describing, but it should be a good example.
      Hope this helps 🙂
      Best regards,
      Nuno Santos

  15. Pingback: ESP32 HTTP web server: Serving external JavaScript file – techtutorialsx

  16. Pingback: ESP32 HTTP web server: Serving external JavaScript file – techtutorialsx

  17. Pingback: ESP32 Arduino: HTTP PUT request – techtutorialsx

  18. Pingback: ESP32 Arduino: HTTP PUT request – techtutorialsx

  19. Pingback: ESP8266 Arduino: HTTP PUT request – techtutorialsx

  20. Pingback: ESP8266 Arduino: HTTP PUT request – techtutorialsx

Leave a Reply

%d bloggers like this: