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

32 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

    Liked by 2 people

    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

      Like

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

    Liked by 1 person

    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

      Like

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

    Like

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

    Like

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

    Like

  6. All this is really nice of youre lucky enough to have an Arduino. But I’m using the ESP32 microcomputer running MicroPython, and looking for a way I can SSH into it so I can directly connect to it through an SSH client.

    This was my original search phrase I typed into DuckDuckGo search engine.
    isn’t anyone using the ESP32, or are they only using REPL.

    Isn’t there an SSH server that runs on the ESP32?

    John

    Like

  7. Is it possible to make this device only by using ESP 32 DOIT DEVKIT V1 ?? instead of using both Arduino and ESP8266.. IF YES THEN PLEASE SHARE THE CODE WITH US

    Like

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

    Like

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

      Like

Leave a reply to antepher Cancel reply

Reply faster with our mobile app
Get notifications and reply in seconds right from your phone.
Try the Jetpack app