WiFi Manager
Relevant source files
The following files were used as context for generating this wiki page:The WiFi Manager is a fundamental component of the HomeSecurity system, responsible for establishing and maintaining the device's connection to a wireless network. This connectivity is critical for the system's core IoT functionality, enabling it to communicate with cloud services like Firebase for sending real-time push notifications to the user's mobile application.
The manager handles the initial connection during system startup and includes a robust auto-reconnection mechanism. This is implemented as a dedicated FreeRTOS task, ensuring the device can recover from network interruptions without manual intervention. A visual indicator, the white LED, provides immediate feedback on the WiFi connection status.
Sources: HomeSecurity/README.md, HomeSecurity/src/main.cpp:2,9, HomeSecurity/src/wifi_manager.cpp:30-33
Configuration
The WiFi Manager's behavior is configured through constants and variables defined directly within the source code. The primary settings are the network credentials (SSID and password) and a connection timeout.
Sources: HomeSecurity/src/wifi_manager.cpp:4-6
| Parameter | Type | Default Value | Description | File |
|---|---|---|---|---|
WIFI_SSID | char* | "IonutMoto" | The SSID of the target WiFi network. | wifi_manager.cpp:5 |
WIFI_PASSWORD | char* | "12345678" | The password for the target WiFi network. | wifi_manager.cpp:6 |
WIFI_CONNECTION_TIMEOUT | unsigned long | 15000 (ms) | The maximum time to wait for a WiFi connection before failing. | wifi_manager.cpp:4 |
Configuration Snippet
The credentials must be hardcoded before compiling and uploading the firmware.
// File: HomeSecurity/src/wifi_manager.cpp
const unsigned long WIFI_CONNECTION_TIMEOUT = 15000; // 15 secunde
char* WIFI_SSID = "IonutMoto";
char* WIFI_PASSWORD = "12345678";
Sources: HomeSecurity/src/wifi_manager.cpp:4-6
Connection Logic
The connection process is managed by the connectToWiFi function and a background FreeRTOS task, WiFiCheckTask.
Initial Connection
During the system's setup() sequence, the connectToWiFi function is called to establish the initial network connection. The function attempts to connect using the configured credentials and will time out after 15 seconds if unsuccessful. Upon a successful connection, it prints a confirmation to the serial monitor and activates the white LED as a visual indicator.
Sources: HomeSecurity/src/main.cpp:9, HomeSecurity/src/wifi_manager.cpp:8-27
The following diagram illustrates the initial connection flow.
Status Indication
A solid white LED indicates that the ESP32 is successfully connected to the WiFi network. This LED is controlled by the turnWhiteLedOn() function, which is called by connectToWiFi upon success. The LED is mapped to GPIO pin 13.
Sources: HomeSecurity/include/led.h:7,18, HomeSecurity/src/wifi_manager.cpp:26
Auto-Reconnection Task
To ensure continuous connectivity, a dedicated FreeRTOS task named WiFiCheckTask runs in the background. This task periodically checks the WiFi connection status. If the connection is lost (WiFi.status() != WL_CONNECTED), it automatically calls the connectToWiFi function to re-establish the link.
This task is created in main.cpp with a priority level of 2 and a stack size of 2048 bytes. It performs its check every 500 milliseconds.
Sources: HomeSecurity/src/main.cpp:11, HomeSecurity/src/wifi_manager.cpp:29-38
The diagram below shows the logic of the auto-reconnection task.
API Reference
The WiFi Manager exposes its functionality through functions declared in wifi_manager.h.
| Function | Description | Source File |
|---|---|---|
connectToWiFi(char* ssid, char* password) | Attempts to connect to the specified WiFi network with a timeout. Turns on the white LED on success. | wifi_manager.cpp:8 |
WiFiCheckTask(void* parameter) | A FreeRTOS task that continuously monitors the WiFi connection and attempts to reconnect if it is lost. | wifi_manager.cpp:29 |
Sources: HomeSecurity/include/wifi_manager.h:7-8
System Integration
The WiFi Manager is initialized early in the boot process and is a prerequisite for any cloud-based operations, such as sending notifications via Firebase. The WiFiCheckTask runs concurrently with other system tasks, ensuring network stability does not block sensor monitoring or alert logic.
This sequence diagram shows the initialization and parallel execution of the WiFi task.