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.


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 a 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 Connect to WiFi Network

Figure 1 – ESP32 connection to WiFi network.


Related Posts

Advertisements
This entry was posted in ESP32 and tagged , , , , , , . Bookmark the permalink.

6 Responses to ESP32: Connecting to a WiFi network

  1. Pingback: ESP32: Publishing messages to MQTT topic | techtutorialsx

  2. Pingback: ESP32: Subscribing to MQTT topic | techtutorialsx

  3. Pingback: ESP32: Sending JSON messages over MQTT | techtutorialsx

  4. Pingback: ESP32: HTTP GET Requests | techtutorialsx

  5. Pingback: ESP32: HTTP POST Requests | techtutorialsx

  6. Pingback: ESP32 Arduino: Getting started with WiFi | techtutorialsx

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s