ESP32 WiFi Library –WiFiMulti.setStrictMode

Home / References / ESP32 Library / WiFi API / WiFiMulti

Description

The setStrictMode() method in the ESP32 WiFiMulti class controls the behavior of the network connection process. When strict mode is enabled by calling setStrictMode(true), the ESP32 will stop attempting to connect to additional networks as soon as it successfully establishes a connection with the first available network from the list. This ensures faster connection establishment by avoiding unnecessary attempts on other networks, even if they might have stronger signal quality.

If strict mode is disabled (setStrictMode(false)), the ESP32 may continue searching for and attempting to connect to other networks in the list after establishing a connection. This can be useful in scenarios where you want the device to potentially switch to a better network if the current connection is lost or suboptimal. By using setStrictMode(), you can tailor the connection behavior to prioritize either speed (strict mode) or flexibility (non-strict mode).

Syntax and Usage

The WiFiMulti.setStrictMode method can be used with the following syntax:

WiFiMulti.setStrictMode(bool mode);

The method requires a single boolean argument to enable or disable strict mode.

Argument(s)

  • mode: A bool value. Set to true to enable strict mode, or false to disable it.

Return Value

The WiFiMulti.setStrictMode method does not return any value. It simply sets the strict mode behavior of the WiFiMulti library.

Example Codes

The setStrictMode() method in the ESP32 WiFiMulti class controls the behavior of the network connection process. When strict mode is enabled by calling setStrictMode(true), the ESP32 will stop attempting to connect to additional networks as soon as it successfully establishes a connection with the first available network from the list. This ensures faster connection establishment by avoiding unnecessary attempts on other networks, even if they might have stronger signal quality.

If strict mode is disabled (setStrictMode(false)), the ESP32 may continue searching for and attempting to connect to other networks in the list after establishing a connection. This can be useful in scenarios where you want the device to potentially switch to a better network if the current connection is lost or suboptimal. By using setStrictMode(), you can tailor the connection behavior to prioritize either speed (strict mode) or flexibility (non-strict mode).

/*
 * Author: Avant Maker
 * Date: February 18, 2025
 * Version: 1.0
 * Description: This example code demonstrates how to 
 * use WiFiMulti.setStrictMode method to enable strict mode.
 * The ESP32 will only connect to the networks that 
 * have been explicitly added.
 *
 * License: MIT 
 * 
 * Code Source: This example code is sourced from the Comprehensive 
 * Guide to the ESP32 Arduino Core Library, accessible on 
 * AvantMaker.com. For additional code examples and in-depth 
 * documentation related to the ESP32 Arduino Core Library, 
 * please visit:
 *
 * https://avantmaker.com/references/esp32-arduino-core-index/
 *
 * AvantMaker.com, your premier destination for all things 
 * DIY, AI, IoT, Smart Home, and STEM projects. We are dedicated 
 * to empowering makers,learners, and enthusiasts with the resources
 *  they need to bring their innovative ideas to life.
 */

#include <WiFi.h>
#include <WiFiMulti.h>

WiFiMulti WiFiMulti;

void setup() {
    Serial.begin(115200);

    // Add multiple WiFi networks
    WiFiMulti.addAP("Network1", "password1");
    WiFiMulti.addAP("Network2", "password2");

    // Enable strict mode
    WiFiMulti.setStrictMode(true);

    Serial.println("Connecting to WiFi...");
    if (WiFiMulti.run() == WL_CONNECTED) {
        Serial.println("Connected to WiFi!");
        Serial.print("IP Address: ");
        Serial.println(WiFi.localIP());
    } else {
        Serial.println("Failed to connect to WiFi.");
    }
}

void loop() {
    // Your code here
}

AvantMaker Tutorial Suggestion

If you want to learn more about the ESP32’s WiFi capabilities, we’ve created a beginner-friendly guide that explains them in detail. Just click the link below, and you’ll be teleported to that page. It will clear up any confusion you may have while connecting your ESP32 to WiFi or setting it up as a WiFi access point (AP).

ESP32 Essential Guide – Network Section – WiFi Chapter

ESP32 Library Index

ESP32 Arduino Core Library


FAQ

Ready to experiment and explore more about ESP32? Visit our website’s All About ESP32 Resources Hub, packed with tutorials, guides, and tools to inspire your maker journey. Experiment, explore, and elevate your skills with everything you need to master this powerful microcontroller platform!

error: Content is protected !!