1. Connect to a network via WiFi

In order to develop an IoT application using MQTT with an ESP32, the first thing we need to do is to connect the device to a network. Although this can be done in several ways, the most convenient way is to do it to a WiFi network.

Let's do it in Wokwi!

Warning

As we are working in wokwi remotely, the ESP32 will have to connect to a wifi network accessible from the wokwi servers (i.e., you cannot connect the device to your WiFi network). Wokwi simulates a WiFi network with full internet access and provides a virtual WiFi access point called Wokwi-GUEST. It is an open access point - no password is required. For more info about network access in wokwi, check this link.

  1. Open a new project in wokwi and load the following diagram.json.

    Show diagram.json
    diagram.json
    {
      "version": 1,
      "author": "Juan M. Gandarias",
      "editor": "wokwi",
      "parts": [
        {
          "type": "board-esp32-devkit-c-v4",
          "id": "esp",
          "top": 0,
          "left": 0,
          "attrs": {}
        }
      ],
      "connections": [
        [
          "esp:TX",
          "$serialMonitor:RX",
          "",
          []
        ],
        [
          "esp:RX",
          "$serialMonitor:TX",
          "",
          []
        ]
      ],
      "dependencies": {}
    }
    

  2. Run the following code

    wifi_connect.ino
    #include <WiFi.h> // (1)!
    
    #define WIFI_SSID "Wokwi-GUEST" // (2)!
    #define WIFI_PASSWORD "" // (3)!
    #define WIFI_CHANNEL 6 // (4)!
    
    void ConnectWiFi() // (5)!
    {
      WiFi.begin(WIFI_SSID, WIFI_PASSWORD, WIFI_CHANNEL); // (6)!
      Serial.print("Connecting to WiFi ");
      Serial.print(WIFI_SSID);
      while (WiFi.status() != WL_CONNECTED) // (7)!
      {
        delay(100);
        Serial.print(".");
      }
      Serial.println(" Connected!");
    
      Serial.print("IP address: ");
      Serial.println(WiFi.localIP());
    }
    
    void setup(void) 
    {
      Serial.begin(115200);
      ConnectWiFi(); // (8)!
    }
    
    void loop()
    {
      delay(10); // (9)!
    }
    
    1. The ESP32 WiFi library is based on the Arduino WiFi library and enables network connection (local and Internet) via WiFi.
    2. Here we define the name of the wifi network to which we want the device to connect. If you want to use the ESP32 in the real world, you need to enter the name of your local network
    3. Introduce the password of the network
    4. This is required in wokwi only. According to the documentation: We specify the WiFi channel number (6) when calling WiFi.begin(). This skips the WiFi scanning phase and saves about 4 seconds when connecting to the WiFi.
    5. This function is used to connect to the WiFi network (you can reuse it in your projects).
    6. This method initializes the WiFi library's network settings. If you don't want to use WIFI_CHANNEL (i,e., in real world project), you can delete the third argument of the function.
    7. Wait until the device has connected to the network, when it's connected, it will display it through the serial port.
    8. The function ConnectWiFi() is called from the setup() so that the device connects to the network at the beginning of the program.
    9. This scripts doesn't do anything else, so there's nothing inside the loop(). Don't forget the delay(10) to speed up the simulation in wokwi (this is not needed in real world applications)
  3. If you have done it correctly, you should see the following

CONGRATULATIONS!

Now you're able to connect an ESP32 to a WiFi network