ESP32 Useful Wi-Fi Library Functions (Arduino IDE)

--Resource --

1. Library Include


  #include < WiFi.h >

2. ESP32 Wi-Fi Modes


    WiFi.mode(WIFI_STA)     # station mode: the ESP32 connects to an access point
    WiFi.mode(WIFI_AP)      # access point mode: stations can connect to the ESP32
    WiFi.mode(WIFI_STA_AP)  # access point and a station connected to another access point

3. Set the ESP32 as an Access Point


    WiFi.mode(WIFI_AP)
    WiFi.softAP(ssid, password);
    WiFi.softAP(const char* ssid, const char* password, int channel, int ssid_hidden, int max_connection)
 
    # ssid: name for the access point – maximum of 63 characters;
    # password: minimum of 8 characters; set to NULL if you want the access point to be open
    # channel: Wi-Fi channel number (1-13)
    # ssid_hidden: (0 = broadcast SSID, 1 = hide SSID)
    # max_connection: maximum simultaneous connected clients (1-4)

4. Scan Wi-Fi Networks


    #include "WiFi.h"

    void setup() {

        Serial.begin(115200);
        WiFi.mode(WIFI_STA);
        WiFi.disconnect();
        delay(100);

        Serial.println("Setup done");
    }   


    void loop() {

        Serial.println("scan start");

        int n = WiFi.scanNetworks();
        Serial.println("scan done");

        if (n == 0) {
             Serial.println("no networks found");
        } else {
            Serial.print(n);
            Serial.println(" networks found");
            for (int i = 0; i < n; ++i) {
                Serial.print(i + 1);
                Serial.print(": ");
                Serial.print(WiFi.SSID(i));
                Serial.print(" (");
                Serial.print(WiFi.RSSI(i));
                Serial.print(")");
                Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN)?" ":"*");
                delay(10);
            }
        }
        Serial.println("");

        // Wait a bit before scanning again
        delay(5000);
    }

5. Connect to a Wi-Fi Network



    const char* ssid = "REPLACE_WITH_YOUR_SSID";
    const char* password = "REPLACE_WITH_YOUR_PASSWORD";

    void initWiFi() {
        WiFi.mode(WIFI_STA);
        WiFi.begin(ssid, password);
        Serial.print("Connecting to WiFi ..");
        while (WiFi.status() != WL_CONNECTED) {
          Serial.print('.');
          delay(1000);
        }
        Serial.println(WiFi.localIP());
      }

6. Get Wi-Fi Connection Status


   
    0	WL_IDLE_STATUS          temporary status assigned when WiFi.begin() is called
    1	WL_NO_SSID_AVAIL        when no SSID are available
    2	WL_SCAN_COMPLETED       scan networks is completed
    3	WL_CONNECTED            when connected to a WiFi network
    4	WL_CONNECT_FAILED       when the connection fails for all the attempts
    5	WL_CONNECTION_LOST      when the connection is lost
    6	WL_DISCONNECTED         when disconnected from a network 


7. Get WiFi Connection Strength


    #include "WiFi.h"

        // Replace with your network credentials (STATION)
        const char* ssid = "REPLACE_WITH_YOUR_SSID";
        const char* password = "REPLACE_WITH_YOUR_PASSWORD";
        
        void initWiFi() {
          WiFi.mode(WIFI_STA);
          WiFi.begin(ssid, password);
          Serial.print("Connecting to WiFi ..");
          while (WiFi.status() != WL_CONNECTED) {
            Serial.print('.');
            delay(1000);
          }
          Serial.println(WiFi.localIP());
        }
        
        void setup() {
          Serial.begin(115200);
          initWiFi();
          Serial.print("RRSI: ");
          Serial.println(WiFi.RSSI());
        }
        
        void loop() {
          // put your main code here, to run repeatedly:
        }

8. Set a Static ESP32 IP Address


    // Load Wi-Fi library
    #include <WiFi.h>

    // Replace with your network credentials (STATION)
    const char* ssid = "REPLACE_WITH_YOUR_SSID";
    const char* password = "REPLACE_WITH_YOUR_PASSWORD";

    // Set your Static IP address
    IPAddress local_IP(192, 168, 1, 184);
    // Set your Gateway IP address
    IPAddress gateway(192, 168, 1, 1);
    
    IPAddress subnet(255, 255, 0, 0);
    IPAddress primaryDNS(8, 8, 8, 8);       // optional
    IPAddress secondaryDNS(8, 8, 4, 4);     // optional

    // inside Void setup
    // Configures static IP address
    if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
        Serial.println("STA Failed to configure");
    }

    // Connect to Wi-Fi network with SSID and password
    Serial.print("Connecting to ");
    Serial.println(ssid);
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }

    // Print local IP address
    Serial.println("");
    Serial.println("WiFi connected.");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());

9. Disconnect & Reconnect


    Reconnect method 1
    WiFi.reconnect();

    Reconnect method 2
    WiFi.disconnect();
    WiFi.begin(ssid, password);
    

10.Static IP of ESP32 in AP mode


    #include "WiFi.h"

    void setup() {
      Serial.begin(115200);
      delay(1000);
      Serial.print("Setting AP (Access Point)…");
      WiFi.mode(WIFI_AP_STA);  // AP + STA required because of wifi scan during AP
      const char* ssid = "MyESP32";
      const char* password = "abcdefghijk";
      if (!WiFi.softAP(ssid, password)) {
        Serial.println("AP Failed");
        return;
      }
      delay(2000);
      // Set static IP
      IPAddress AP_LOCAL_IP(192, 168, 1, 160);
      IPAddress AP_GATEWAY_IP(192, 168, 1, 4);
      IPAddress AP_NETWORK_MASK(255, 255, 255, 0);
      if (!WiFi.softAPConfig(AP_LOCAL_IP, AP_GATEWAY_IP, AP_NETWORK_MASK)) {
        Serial.println("AP Config Failed");
        return;
      }
      // Show the local IP
      Serial.print("AP IP address: ");
      IPAddress IP = WiFi.softAPIP();
      Serial.println(IP);
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
    
    }