error handling and cleanup
This commit is contained in:
43
main/wifi.c
43
main/wifi.c
@ -1,3 +1,8 @@
|
||||
/**
|
||||
* @file wifi.c
|
||||
* @brief Implementation of WiFi station mode module.
|
||||
*/
|
||||
|
||||
#include "wifi.h"
|
||||
|
||||
#include "esp_timer.h"
|
||||
@ -12,12 +17,19 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
/** @brief Event bit for successful connection. */
|
||||
#define WIFI_CONNECTED_BIT BIT0
|
||||
|
||||
/** @brief Event bit for connection failure. */
|
||||
#define WIFI_FAIL_BIT BIT1
|
||||
|
||||
/** @brief Maximum connection retry attempts. */
|
||||
#define MAX_RETRY_COUNT 10
|
||||
|
||||
/** @brief Delay between retries in milliseconds. */
|
||||
#define RETRY_DELAY_MS 1000
|
||||
|
||||
static const char *TAG = "smart-oil-heater-control-system-wifi";
|
||||
static const char *TAG = "wifi";
|
||||
|
||||
static EventGroupHandle_t s_wifi_event_group;
|
||||
static int s_retry_num = 0;
|
||||
@ -26,13 +38,13 @@ static bool s_initial_connect = true;
|
||||
static void event_handler(void *arg, esp_event_base_t event_base,
|
||||
int32_t event_id, void *event_data);
|
||||
|
||||
void initWifi(void)
|
||||
esp_err_t initWifi(void)
|
||||
{
|
||||
s_wifi_event_group = xEventGroupCreate();
|
||||
if (s_wifi_event_group == NULL)
|
||||
{
|
||||
ESP_LOGE(TAG, "xEventGroupCreate() failed!");
|
||||
return;
|
||||
ESP_LOGE(TAG, "Failed to create event group");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
ESP_ERROR_CHECK(esp_netif_init());
|
||||
@ -73,10 +85,10 @@ void initWifi(void)
|
||||
|
||||
ESP_ERROR_CHECK(esp_wifi_start());
|
||||
|
||||
ESP_ERROR_CHECK(esp_wifi_set_max_tx_power(78)); // Set max power to 19.5 dBm (78 in units of 0.25 dBm)
|
||||
ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_MIN_MODEM)); // Use power-saving mode
|
||||
ESP_ERROR_CHECK(esp_wifi_set_max_tx_power(78));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_MIN_MODEM));
|
||||
|
||||
ESP_LOGI(TAG, "wifi_init_sta finished.");
|
||||
ESP_LOGI(TAG, "WiFi init finished, waiting for connection...");
|
||||
|
||||
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
|
||||
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
|
||||
@ -86,21 +98,32 @@ void initWifi(void)
|
||||
|
||||
if (bits & WIFI_CONNECTED_BIT)
|
||||
{
|
||||
ESP_LOGI(TAG, "Connected to ap SSID:%s", CONFIG_SSID);
|
||||
ESP_LOGI(TAG, "Connected to AP SSID:%s", CONFIG_SSID);
|
||||
}
|
||||
else if (bits & WIFI_FAIL_BIT)
|
||||
{
|
||||
ESP_LOGI(TAG, "Failed to connect to SSID:%s", CONFIG_SSID);
|
||||
ESP_LOGE(TAG, "Failed to connect to SSID:%s", CONFIG_SSID);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGE(TAG, "Unexpected event");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
// Mark initial connection phase complete - do NOT delete the event group
|
||||
s_initial_connect = false;
|
||||
|
||||
ESP_LOGI(TAG, "Initialized successfully");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief WiFi event handler.
|
||||
* @param arg User argument (unused).
|
||||
* @param event_base Event base.
|
||||
* @param event_id Event ID.
|
||||
* @param event_data Event data.
|
||||
*/
|
||||
static void event_handler(void *arg, esp_event_base_t event_base,
|
||||
int32_t event_id, void *event_data)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user