Refactor WiFi Manager
This commit is contained in:
@ -2,10 +2,7 @@
|
||||
#include <string.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_http_server.h"
|
||||
#include "esp_task_wdt.h"
|
||||
@ -16,10 +13,7 @@
|
||||
#include "config.h"
|
||||
#include "motor_control.h"
|
||||
#include "state_manager.h"
|
||||
|
||||
// WiFi event group
|
||||
static EventGroupHandle_t s_wifi_event_group;
|
||||
static int s_retry_num = 0;
|
||||
#include "wifi_manager.h"
|
||||
|
||||
// HTTP server handle
|
||||
static httpd_handle_t server = NULL;
|
||||
@ -112,29 +106,6 @@ void feed_watchdog(void) {
|
||||
}
|
||||
}
|
||||
|
||||
// WiFi event handler
|
||||
static void event_handler(void* arg, esp_event_base_t event_base,
|
||||
int32_t event_id, void* event_data)
|
||||
{
|
||||
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
|
||||
esp_wifi_connect();
|
||||
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
|
||||
if (s_retry_num < WIFI_MAXIMUM_RETRY) {
|
||||
esp_wifi_connect();
|
||||
s_retry_num++;
|
||||
ESP_LOGI(SYSTEM_TAG, "retry to connect to the AP");
|
||||
} else {
|
||||
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
|
||||
}
|
||||
ESP_LOGI(SYSTEM_TAG, "connect to the AP fail");
|
||||
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
|
||||
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
|
||||
ESP_LOGI(SYSTEM_TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
|
||||
s_retry_num = 0;
|
||||
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to set CORS headers
|
||||
static void set_cors_headers(httpd_req_t *req) {
|
||||
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
|
||||
@ -164,9 +135,16 @@ static esp_err_t status_get_handler(httpd_req_t *req)
|
||||
int last_on_speed;
|
||||
motor_get_last_on_state(&last_on_mode, &last_on_speed);
|
||||
|
||||
ESP_LOGI(SYSTEM_TAG, "Status request - Mode: %s, Current: %d%%, Target: %d%%, State: %s, Ramping: %s",
|
||||
// Get WiFi information
|
||||
wifi_info_t wifi_info;
|
||||
wifi_manager_get_info(&wifi_info);
|
||||
char ip_str[16];
|
||||
wifi_manager_get_ip_string(ip_str, sizeof(ip_str));
|
||||
|
||||
ESP_LOGI(SYSTEM_TAG, "Status request - Mode: %s, Current: %d%%, Target: %d%%, State: %s, Ramping: %s, WiFi: %s (%s)",
|
||||
motor_mode_to_string(state->mode), state->current_speed, state->target_speed,
|
||||
motor_state_to_string(state->state), state->ramping ? "YES" : "NO");
|
||||
motor_state_to_string(state->state), state->ramping ? "YES" : "NO",
|
||||
wifi_manager_status_to_string(wifi_info.status), ip_str);
|
||||
|
||||
set_cors_headers(req);
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
@ -189,6 +167,7 @@ static esp_err_t status_get_handler(httpd_req_t *req)
|
||||
const char* last_on_mode_str = "exhaust";
|
||||
if (last_on_mode == MOTOR_INTAKE) last_on_mode_str = "intake";
|
||||
|
||||
// Motor status
|
||||
cJSON_AddStringToObject(json, "mode", mode_str);
|
||||
cJSON_AddNumberToObject(json, "current_speed", state->current_speed);
|
||||
cJSON_AddNumberToObject(json, "target_speed", state->target_speed);
|
||||
@ -198,6 +177,15 @@ static esp_err_t status_get_handler(httpd_req_t *req)
|
||||
cJSON_AddStringToObject(json, "last_on_mode", last_on_mode_str);
|
||||
cJSON_AddNumberToObject(json, "last_on_speed", last_on_speed);
|
||||
|
||||
// WiFi status
|
||||
cJSON *wifi_json = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(wifi_json, "status", wifi_manager_status_to_string(wifi_info.status));
|
||||
cJSON_AddStringToObject(wifi_json, "ssid", wifi_info.ssid);
|
||||
cJSON_AddStringToObject(wifi_json, "ip", ip_str);
|
||||
cJSON_AddNumberToObject(wifi_json, "rssi", wifi_info.rssi);
|
||||
cJSON_AddBoolToObject(wifi_json, "connected", wifi_manager_is_connected());
|
||||
cJSON_AddItemToObject(json, "wifi", wifi_json);
|
||||
|
||||
// Add pending command info if in cooldown
|
||||
if (state->state == MOTOR_STATE_COOLDOWN) {
|
||||
const char* pending_mode_str = "off";
|
||||
@ -382,62 +370,6 @@ static httpd_handle_t start_webserver(void)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void wifi_init_sta(void)
|
||||
{
|
||||
s_wifi_event_group = xEventGroupCreate();
|
||||
|
||||
ESP_ERROR_CHECK(esp_netif_init());
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
esp_netif_create_default_wifi_sta();
|
||||
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||
|
||||
esp_event_handler_instance_t instance_any_id;
|
||||
esp_event_handler_instance_t instance_got_ip;
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
|
||||
ESP_EVENT_ANY_ID,
|
||||
&event_handler,
|
||||
NULL,
|
||||
&instance_any_id));
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
|
||||
IP_EVENT_STA_GOT_IP,
|
||||
&event_handler,
|
||||
NULL,
|
||||
&instance_got_ip));
|
||||
|
||||
wifi_config_t wifi_config = {
|
||||
.sta = {
|
||||
.ssid = WIFI_SSID,
|
||||
.password = WIFI_PASS,
|
||||
.threshold.authmode = WIFI_AUTH_WPA2_PSK,
|
||||
.pmf_cfg = {
|
||||
.capable = true,
|
||||
.required = false
|
||||
},
|
||||
},
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
|
||||
ESP_ERROR_CHECK(esp_wifi_start());
|
||||
|
||||
ESP_LOGI(SYSTEM_TAG, "wifi_init_sta finished.");
|
||||
|
||||
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
|
||||
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
|
||||
pdFALSE,
|
||||
pdFALSE,
|
||||
portMAX_DELAY);
|
||||
|
||||
if (bits & WIFI_CONNECTED_BIT) {
|
||||
ESP_LOGI(SYSTEM_TAG, "connected to ap SSID:%s", WIFI_SSID);
|
||||
} else if (bits & WIFI_FAIL_BIT) {
|
||||
ESP_LOGI(SYSTEM_TAG, "Failed to connect to SSID:%s", WIFI_SSID);
|
||||
} else {
|
||||
ESP_LOGE(SYSTEM_TAG, "UNEXPECTED EVENT");
|
||||
}
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
ESP_LOGI(SYSTEM_TAG, "Starting Maxxfan HTTP Controller with State Preservation!");
|
||||
@ -472,18 +404,63 @@ void app_main(void)
|
||||
ESP_LOGW(SYSTEM_TAG, "⚠️ Failed to load state: %s", esp_err_to_name(load_result));
|
||||
}
|
||||
|
||||
ESP_LOGI(SYSTEM_TAG, "Connecting to WiFi network: %s", WIFI_SSID);
|
||||
wifi_init_sta();
|
||||
// Initialize WiFi manager
|
||||
ESP_LOGI(SYSTEM_TAG, "Initializing WiFi manager...");
|
||||
ret = wifi_manager_init();
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(SYSTEM_TAG, "Failed to initialize WiFi manager: %s", esp_err_to_name(ret));
|
||||
return;
|
||||
}
|
||||
|
||||
// Start HTTP server
|
||||
start_webserver();
|
||||
// Connect to WiFi using default credentials
|
||||
ESP_LOGI(SYSTEM_TAG, "Connecting to WiFi network: %s", WIFI_SSID);
|
||||
ret = wifi_manager_connect_default();
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(SYSTEM_TAG, "Failed to start WiFi connection: %s", esp_err_to_name(ret));
|
||||
return;
|
||||
}
|
||||
|
||||
// Wait for WiFi connection (with timeout)
|
||||
ESP_LOGI(SYSTEM_TAG, "Waiting for WiFi connection...");
|
||||
esp_err_t wifi_result = wifi_manager_wait_for_connection(30000); // 30 second timeout
|
||||
if (wifi_result == ESP_OK) {
|
||||
char ip_str[16];
|
||||
wifi_manager_get_ip_string(ip_str, sizeof(ip_str));
|
||||
wifi_info_t wifi_info;
|
||||
wifi_manager_get_info(&wifi_info);
|
||||
|
||||
ESP_LOGI(SYSTEM_TAG, "✓ WiFi connected successfully!");
|
||||
ESP_LOGI(SYSTEM_TAG, " SSID: %s", wifi_info.ssid);
|
||||
ESP_LOGI(SYSTEM_TAG, " IP Address: %s", ip_str);
|
||||
ESP_LOGI(SYSTEM_TAG, " Signal Strength: %d dBm", wifi_info.rssi);
|
||||
} else if (wifi_result == ESP_ERR_TIMEOUT) {
|
||||
ESP_LOGW(SYSTEM_TAG, "⚠️ WiFi connection timeout - continuing with limited functionality");
|
||||
} else {
|
||||
ESP_LOGW(SYSTEM_TAG, "⚠️ WiFi connection failed - continuing with limited functionality");
|
||||
}
|
||||
|
||||
// Start HTTP server (even if WiFi failed, for debugging)
|
||||
ESP_LOGI(SYSTEM_TAG, "Starting web server...");
|
||||
httpd_handle_t web_server = start_webserver();
|
||||
if (web_server) {
|
||||
ESP_LOGI(SYSTEM_TAG, "✓ Web server started successfully");
|
||||
} else {
|
||||
ESP_LOGE(SYSTEM_TAG, "✗ Failed to start web server");
|
||||
}
|
||||
|
||||
// Report final system state after initialization
|
||||
const motor_state_t* final_state = motor_get_state();
|
||||
wifi_info_t final_wifi_info;
|
||||
wifi_manager_get_info(&final_wifi_info);
|
||||
char final_ip_str[16];
|
||||
wifi_manager_get_ip_string(final_ip_str, sizeof(final_ip_str));
|
||||
|
||||
ESP_LOGI(SYSTEM_TAG, "=== SYSTEM INITIALIZATION COMPLETE ===");
|
||||
ESP_LOGI(SYSTEM_TAG, "Reset Reason: %s", state_manager_get_reset_reason_string());
|
||||
ESP_LOGI(SYSTEM_TAG, "Watchdog Reset: %s", state_manager_was_watchdog_reset() ? "YES" : "NO");
|
||||
ESP_LOGI(SYSTEM_TAG, "Final motor state: mode=%s, target=%d%%, current=%d%%, state=%s",
|
||||
|
||||
// Motor status
|
||||
ESP_LOGI(SYSTEM_TAG, "Motor: mode=%s, target=%d%%, current=%d%%, state=%s",
|
||||
motor_mode_to_string(final_state->mode), final_state->target_speed,
|
||||
final_state->current_speed, motor_state_to_string(final_state->state));
|
||||
|
||||
@ -500,6 +477,18 @@ void app_main(void)
|
||||
ESP_LOGI(SYSTEM_TAG, "Last ON state: %s @ %d%%",
|
||||
motor_mode_to_string(last_on_mode), last_on_speed);
|
||||
ESP_LOGI(SYSTEM_TAG, "User turned off: %s", motor_get_user_turned_off() ? "YES" : "NO");
|
||||
|
||||
// WiFi status
|
||||
ESP_LOGI(SYSTEM_TAG, "WiFi: status=%s, SSID=%s, IP=%s, RSSI=%d dBm",
|
||||
wifi_manager_status_to_string(final_wifi_info.status),
|
||||
final_wifi_info.ssid, final_ip_str, final_wifi_info.rssi);
|
||||
|
||||
// Connection statistics
|
||||
uint32_t total_attempts, successful_connections;
|
||||
esp_err_t last_wifi_error;
|
||||
wifi_manager_get_stats(&total_attempts, &successful_connections, &last_wifi_error);
|
||||
ESP_LOGI(SYSTEM_TAG, "WiFi Stats: %lu attempts, %lu successful", total_attempts, successful_connections);
|
||||
|
||||
ESP_LOGI(SYSTEM_TAG, "Saved state exists: %s", state_manager_has_saved_state() ? "YES" : "NO");
|
||||
ESP_LOGI(SYSTEM_TAG, "=====================================");
|
||||
|
||||
@ -507,17 +496,31 @@ void app_main(void)
|
||||
ESP_LOGI(SYSTEM_TAG, "Features: State Preservation, Direction Safety, Motor Ramping, ON Button");
|
||||
ESP_LOGI(SYSTEM_TAG, "Safety: %d-second cooldown for direction changes", DIRECTION_CHANGE_COOLDOWN_MS / 1000);
|
||||
ESP_LOGI(SYSTEM_TAG, "Memory: Remembers settings after power loss (except watchdog resets)");
|
||||
ESP_LOGI(SYSTEM_TAG, "WiFi: Enhanced connection management with auto-reconnect");
|
||||
ESP_LOGI(SYSTEM_TAG, "State Manager: Enhanced NVS operations with validation and recovery");
|
||||
ESP_LOGI(SYSTEM_TAG, "Open your browser and go to: http://[ESP32_IP_ADDRESS]");
|
||||
ESP_LOGI(SYSTEM_TAG, "Check the monitor output above for your IP address");
|
||||
|
||||
if (wifi_manager_is_connected()) {
|
||||
ESP_LOGI(SYSTEM_TAG, "🌐 Open your browser and go to: http://%s", final_ip_str);
|
||||
} else {
|
||||
ESP_LOGI(SYSTEM_TAG, "⚠️ WiFi not connected - check network settings");
|
||||
}
|
||||
|
||||
// Main loop - reset watchdog periodically and update motor cooldown
|
||||
uint32_t loop_count = 0;
|
||||
while (1) {
|
||||
feed_watchdog();
|
||||
|
||||
// Update motor cooldown time for status reporting
|
||||
motor_update_cooldown_time();
|
||||
|
||||
// Periodic WiFi status logging (every 30 seconds)
|
||||
if (++loop_count % 10 == 0) {
|
||||
wifi_status_t current_status = wifi_manager_get_status();
|
||||
if (current_status != WIFI_STATUS_CONNECTED) {
|
||||
ESP_LOGW(SYSTEM_TAG, "WiFi status: %s", wifi_manager_status_to_string(current_status));
|
||||
}
|
||||
}
|
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(WATCHDOG_FEED_INTERVAL_MS));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user