Question
Answer and Explanation
The WiFiManager library, commonly used in ESP32 and ESP8266 projects, closes its web server for several key reasons, primarily related to security, resource management, and the intended workflow of network configuration. Here’s a breakdown:
1. Security:
- Preventing Unauthorized Access: Once the device is configured and connected to a Wi-Fi network, leaving the configuration portal active poses a significant security risk. Attackers could potentially access the server to gain control over the device, change Wi-Fi settings, or upload malicious firmware. Closing the server minimizes this vulnerability.
- Reducing Attack Surface: By closing the server, the number of open ports and services exposed by the device is reduced, effectively decreasing the "attack surface".
2. Resource Management:
- Memory Conservation: Running a web server consumes RAM and processor resources, which are limited in microcontrollers like the ESP32 and ESP8266. Closing the server frees up these resources, allowing the device to focus on its primary function. It avoids unnecessary resource drain when the server is no longer needed.
- Power Efficiency: Actively running a web server can increase power consumption. Closing the server when it’s not in use improves the device's overall power efficiency, particularly important for battery-powered applications.
3. Intended Workflow:
- Configuration Completion: The primary goal of WiFiManager is to facilitate the initial Wi-Fi network setup for a device. Once the device connects to a Wi-Fi network and stores the relevant information (SSID, password etc.), the server is no longer required and can be safely closed.
- Operational Mode Transition: After the configuration process, the device should transition into its normal operational mode, which doesn't typically require a continuously running configuration server. The server is meant for initial setup or reconfiguration, not ongoing operation.
4. Implementation Details
- Internal Logic: The WiFiManager library includes logic to close the server automatically once the Wi-Fi connection is successful. This often involves the use of specific functions that handle the server lifecycle, ensuring it is active only when needed.
- `WiFiManager::autoConnect()` Method: Many projects using WiFiManager utilize a method like `WiFiManager::autoConnect()`, which handles the server and connection process. Once a connection is made or after a timeout the function will either close the portal or proceed with normal operations after saving the connection credentials to non volatile memory.
In summary, WiFiManager closes its server for critical security reasons, resource efficiency, and because it aligns with the designed workflow of initial network configuration. If you require ongoing server functionality, a separate web server must be created and managed. Do not reuse the WiFiManager's webserver for that purpose as it's not intended to be a long-running web server.