#include #include #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" #include "nvs_flash.h" #include "nvs.h" #include "cJSON.h" // Project modules #include "config.h" #include "motor_control.h" // WiFi event group static EventGroupHandle_t s_wifi_event_group; static int s_retry_num = 0; // HTTP server handle static httpd_handle_t server = NULL; // Task handles for watchdog static TaskHandle_t main_task_handle = NULL; // Compact HTML web page for control static const char* html_page = "Maxxfan

Maxxfan Controller

" "

Status

Mode: OFF

Speed: 0%

" "

Target: 0%

State: IDLE

" "

Last ON: EXHAUST @ 50%

" "
Ramping...
" "
Direction change cooldown: 0s
" "
Error
Connecting...
" "

Fan Control

" "" "" "
" "

Speed Control

" "" "" "
" ""; // Forward declarations static esp_err_t save_motor_state_to_nvs(void); static esp_err_t load_motor_state_from_nvs(void); static bool is_watchdog_reset(void); // Initialize watchdog timer void init_watchdog(void) { ESP_LOGI(SYSTEM_TAG, "Setting up watchdog monitoring..."); // Get current task handle and add to watchdog main_task_handle = xTaskGetCurrentTaskHandle(); esp_err_t result = esp_task_wdt_add(main_task_handle); if (result == ESP_OK) { ESP_LOGI(SYSTEM_TAG, "Main task added to watchdog monitoring"); } else if (result == ESP_ERR_INVALID_ARG) { ESP_LOGI(SYSTEM_TAG, "Task already monitored by watchdog"); } else { ESP_LOGW(SYSTEM_TAG, "Watchdog not available: %s", esp_err_to_name(result)); main_task_handle = NULL; // Disable watchdog feeding } } // Feed the watchdog void feed_watchdog(void) { if (main_task_handle != NULL) { esp_err_t result = esp_task_wdt_reset(); if (result != ESP_OK) { MOTOR_LOGD(SYSTEM_TAG, "Watchdog reset failed: %s", esp_err_to_name(result)); } } } // Check if this was a watchdog reset static bool is_watchdog_reset(void) { esp_reset_reason_t reset_reason = esp_reset_reason(); // Only consider TASK_WDT and INT_WDT as true watchdog resets // ESP_RST_WDT can be triggered by power disconnection, so we exclude it return (reset_reason == ESP_RST_TASK_WDT || reset_reason == ESP_RST_INT_WDT); } // Save motor state to NVS static esp_err_t save_motor_state_to_nvs(void) { nvs_handle_t nvs_handle; esp_err_t err; err = nvs_open(NVS_NAMESPACE, NVS_READWRITE, &nvs_handle); if (err != ESP_OK) { ESP_LOGE(SYSTEM_TAG, "Error opening NVS handle: %s", esp_err_to_name(err)); return err; } // Get current motor state const motor_state_t* state = motor_get_state(); motor_mode_t last_on_mode; int last_on_speed; motor_get_last_on_state(&last_on_mode, &last_on_speed); bool user_turned_off = motor_get_user_turned_off(); ESP_LOGI(SYSTEM_TAG, "=== SAVING STATE TO NVS ==="); ESP_LOGI(SYSTEM_TAG, "Mode: %d, Speed: %d%%, Last ON: %d@%d%%, User OFF: %s", state->mode, state->target_speed, last_on_mode, last_on_speed, user_turned_off ? "YES" : "NO"); // Save current motor state err = nvs_set_u8(nvs_handle, NVS_KEY_MODE, (uint8_t)state->mode); if (err == ESP_OK) { err = nvs_set_u8(nvs_handle, NVS_KEY_SPEED, (uint8_t)state->target_speed); } // Save last ON state if (err == ESP_OK) { err = nvs_set_u8(nvs_handle, NVS_KEY_LAST_ON_MODE, (uint8_t)last_on_mode); } if (err == ESP_OK) { err = nvs_set_u8(nvs_handle, NVS_KEY_LAST_ON_SPEED, (uint8_t)last_on_speed); } // Save power state (whether user turned off manually) if (err == ESP_OK) { err = nvs_set_u8(nvs_handle, NVS_KEY_POWER_STATE, user_turned_off ? 1 : 0); } if (err == ESP_OK) { err = nvs_commit(nvs_handle); if (err == ESP_OK) { ESP_LOGI(SYSTEM_TAG, "✓ Motor state successfully saved to NVS"); } else { ESP_LOGE(SYSTEM_TAG, "✗ NVS commit failed: %s", esp_err_to_name(err)); } } else { ESP_LOGE(SYSTEM_TAG, "✗ Error saving to NVS: %s", esp_err_to_name(err)); } ESP_LOGI(SYSTEM_TAG, "==========================="); nvs_close(nvs_handle); return err; } // Load motor state from NVS static esp_err_t load_motor_state_from_nvs(void) { nvs_handle_t nvs_handle; esp_err_t err; err = nvs_open(NVS_NAMESPACE, NVS_READONLY, &nvs_handle); if (err != ESP_OK) { ESP_LOGI(SYSTEM_TAG, "NVS not found, using default state"); return ESP_ERR_NVS_NOT_FOUND; } uint8_t stored_mode = 0; uint8_t stored_speed = 0; uint8_t stored_last_mode = 1; // Default to MOTOR_EXHAUST uint8_t stored_last_speed = 50; uint8_t stored_power_state = 0; // Load current motor state err = nvs_get_u8(nvs_handle, NVS_KEY_MODE, &stored_mode); if (err == ESP_OK) { nvs_get_u8(nvs_handle, NVS_KEY_SPEED, &stored_speed); nvs_get_u8(nvs_handle, NVS_KEY_LAST_ON_MODE, &stored_last_mode); nvs_get_u8(nvs_handle, NVS_KEY_LAST_ON_SPEED, &stored_last_speed); nvs_get_u8(nvs_handle, NVS_KEY_POWER_STATE, &stored_power_state); // Validate ranges using config macros if (stored_mode > MOTOR_INTAKE) stored_mode = MOTOR_OFF; if (!IS_VALID_SPEED(stored_speed)) stored_speed = 0; if (stored_last_mode < MOTOR_EXHAUST || stored_last_mode > MOTOR_INTAKE) stored_last_mode = MOTOR_EXHAUST; if (!IS_VALID_SPEED(stored_last_speed)) stored_last_speed = 50; // Set the last ON state in motor control module motor_set_last_on_state((motor_mode_t)stored_last_mode, stored_last_speed); motor_set_user_turned_off(stored_power_state == 1); ESP_LOGI(SYSTEM_TAG, "Loaded state from NVS - Mode: %d, Speed: %d%%, Last ON: %d@%d%%, User OFF: %s", stored_mode, stored_speed, stored_last_mode, stored_last_speed, stored_power_state ? "YES" : "NO"); // Check reset reason to decide whether to restore state bool was_watchdog_reset = is_watchdog_reset(); esp_reset_reason_t reset_reason = esp_reset_reason(); ESP_LOGI(SYSTEM_TAG, "=== RESET ANALYSIS ==="); ESP_LOGI(SYSTEM_TAG, "Reset reason: %d", reset_reason); ESP_LOGI(SYSTEM_TAG, "Reset reason name: %s", reset_reason == ESP_RST_POWERON ? "POWERON" : reset_reason == ESP_RST_EXT ? "EXTERNAL" : reset_reason == ESP_RST_SW ? "SOFTWARE" : reset_reason == ESP_RST_PANIC ? "PANIC" : reset_reason == ESP_RST_INT_WDT ? "INT_WDT" : reset_reason == ESP_RST_TASK_WDT ? "TASK_WDT" : reset_reason == ESP_RST_WDT ? "WDT" : reset_reason == ESP_RST_DEEPSLEEP ? "DEEPSLEEP" : reset_reason == ESP_RST_BROWNOUT ? "BROWNOUT" : reset_reason == ESP_RST_SDIO ? "SDIO" : "UNKNOWN"); ESP_LOGI(SYSTEM_TAG, "Watchdog reset: %s", was_watchdog_reset ? "YES" : "NO"); ESP_LOGI(SYSTEM_TAG, "Stored mode: %d, speed: %d", stored_mode, stored_speed); ESP_LOGI(SYSTEM_TAG, "User turned off: %s", stored_power_state ? "YES" : "NO"); ESP_LOGI(SYSTEM_TAG, "===================="); // Store the restored state for potential motor restoration if (was_watchdog_reset) { // True watchdog reset (TASK_WDT or INT_WDT) - don't restore state, start fresh ESP_LOGI(SYSTEM_TAG, "⚠️ TRUE watchdog reset detected - starting in OFF state for safety"); // Motor module is already initialized in OFF state, no action needed } else if (stored_power_state) { // User manually turned off - stay off ESP_LOGI(SYSTEM_TAG, "🔒 User had turned off manually - staying OFF"); // Motor module is already initialized in OFF state, no action needed } else if (stored_mode != MOTOR_OFF && stored_speed > 0) { // Normal power loss or general WDT (which can be power-related) - restore previous state ESP_LOGI(SYSTEM_TAG, "🔋 Power restored - will resume previous state: %s @ %d%%", stored_mode == MOTOR_EXHAUST ? "EXHAUST" : "INTAKE", stored_speed); // Set the motor to the restored state (will be applied after initialization) motor_set_speed((motor_mode_t)stored_mode, stored_speed); } else { ESP_LOGI(SYSTEM_TAG, "❌ No valid state to restore (mode=%d, speed=%d)", stored_mode, stored_speed); // Motor module is already initialized in OFF state, no action needed } } else { ESP_LOGI(SYSTEM_TAG, "No saved state found, using defaults"); err = ESP_ERR_NVS_NOT_FOUND; } nvs_close(nvs_handle); return err; } // 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", "*"); httpd_resp_set_hdr(req, "Access-Control-Allow-Methods", "GET, POST, OPTIONS"); httpd_resp_set_hdr(req, "Access-Control-Allow-Headers", "Content-Type, Accept"); httpd_resp_set_hdr(req, "Cache-Control", "no-cache"); } // HTTP handler for the main web page static esp_err_t root_get_handler(httpd_req_t *req) { set_cors_headers(req); httpd_resp_set_type(req, "text/html"); httpd_resp_send(req, html_page, HTTPD_RESP_USE_STRLEN); return ESP_OK; } // HTTP handler for fan status (GET /status) static esp_err_t status_get_handler(httpd_req_t *req) { // Update cooldown time before reporting motor_update_cooldown_time(); // Get current motor state const motor_state_t* state = motor_get_state(); motor_mode_t last_on_mode; 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", motor_mode_to_string(state->mode), state->current_speed, state->target_speed, motor_state_to_string(state->state), state->ramping ? "YES" : "NO"); set_cors_headers(req); httpd_resp_set_type(req, "application/json"); cJSON *json = cJSON_CreateObject(); const char* mode_str = "off"; if (state->mode == MOTOR_EXHAUST) mode_str = "exhaust"; else if (state->mode == MOTOR_INTAKE) mode_str = "intake"; const char* state_str = "idle"; switch (state->state) { case MOTOR_STATE_RAMPING: state_str = "ramping"; break; case MOTOR_STATE_STOPPING: state_str = "stopping"; break; case MOTOR_STATE_COOLDOWN: state_str = "cooldown"; break; case MOTOR_STATE_RESTARTING: state_str = "restarting"; break; default: state_str = "idle"; break; } const char* last_on_mode_str = "exhaust"; if (last_on_mode == MOTOR_INTAKE) last_on_mode_str = "intake"; cJSON_AddStringToObject(json, "mode", mode_str); cJSON_AddNumberToObject(json, "current_speed", state->current_speed); cJSON_AddNumberToObject(json, "target_speed", state->target_speed); cJSON_AddStringToObject(json, "state", state_str); cJSON_AddBoolToObject(json, "ramping", state->ramping); cJSON_AddNumberToObject(json, "cooldown_remaining", state->cooldown_remaining_ms); cJSON_AddStringToObject(json, "last_on_mode", last_on_mode_str); cJSON_AddNumberToObject(json, "last_on_speed", last_on_speed); // Add pending command info if in cooldown if (state->state == MOTOR_STATE_COOLDOWN) { const char* pending_mode_str = "off"; if (state->pending_mode == MOTOR_EXHAUST) pending_mode_str = "exhaust"; else if (state->pending_mode == MOTOR_INTAKE) pending_mode_str = "intake"; cJSON_AddStringToObject(json, "pending_mode", pending_mode_str); cJSON_AddNumberToObject(json, "pending_speed", state->pending_speed); } char *json_string = cJSON_Print(json); if (json_string) { httpd_resp_send(req, json_string, strlen(json_string)); free(json_string); } else { httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "JSON creation failed"); } cJSON_Delete(json); return ESP_OK; } // HTTP handler for fan control (POST /fan) static esp_err_t fan_post_handler(httpd_req_t *req) { char buf[MAX_JSON_BUFFER_SIZE]; int ret, remaining = req->content_len; if (remaining >= sizeof(buf)) { httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Content too long"); return ESP_FAIL; } ret = httpd_req_recv(req, buf, remaining); if (ret <= 0) { if (ret == HTTPD_SOCK_ERR_TIMEOUT) { httpd_resp_send_err(req, HTTPD_408_REQ_TIMEOUT, "Request timeout"); } return ESP_FAIL; } buf[ret] = '\0'; ESP_LOGI(SYSTEM_TAG, "Received POST data: %s", buf); cJSON *json = cJSON_Parse(buf); if (json == NULL) { ESP_LOGE(SYSTEM_TAG, "JSON parsing failed"); httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Invalid JSON"); return ESP_FAIL; } cJSON *mode_json = cJSON_GetObjectItem(json, "mode"); cJSON *speed_json = cJSON_GetObjectItem(json, "speed"); if (!cJSON_IsString(mode_json) || (!cJSON_IsNumber(speed_json) && !cJSON_IsString(speed_json))) { ESP_LOGE(SYSTEM_TAG, "JSON parsing failed - mode: %s, speed: %s", mode_json ? (cJSON_IsString(mode_json) ? mode_json->valuestring : "not_string") : "null", speed_json ? (cJSON_IsNumber(speed_json) ? "number" : (cJSON_IsString(speed_json) ? speed_json->valuestring : "not_number_or_string")) : "null"); cJSON_Delete(json); httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Missing mode or speed"); return ESP_FAIL; } const char* mode_str = mode_json->valuestring; int speed; // Handle both number and string speed values if (cJSON_IsNumber(speed_json)) { speed = (int)speed_json->valuedouble; } else if (cJSON_IsString(speed_json)) { speed = atoi(speed_json->valuestring); } else { speed = 0; } motor_mode_t mode = MOTOR_OFF; // Handle special "ON" command - resume last settings if (strcmp(mode_str, "on") == 0) { ESP_LOGI(SYSTEM_TAG, "ON button pressed - resuming last state"); motor_resume_last_state(); // Save state after ON button save_motor_state_to_nvs(); cJSON_Delete(json); return status_get_handler(req); } else if (strcmp(mode_str, "exhaust") == 0) { mode = MOTOR_EXHAUST; } else if (strcmp(mode_str, "intake") == 0) { mode = MOTOR_INTAKE; } ESP_LOGI(SYSTEM_TAG, "HTTP Request: mode=%s, speed=%d", mode_str, speed); motor_set_speed(mode, speed); // Save state after any motor command save_motor_state_to_nvs(); cJSON_Delete(json); // Send response with updated status return status_get_handler(req); } // HTTP handler for OPTIONS requests (CORS preflight) static esp_err_t options_handler(httpd_req_t *req) { set_cors_headers(req); httpd_resp_set_status(req, "200 OK"); httpd_resp_send(req, NULL, 0); return ESP_OK; } // Start HTTP server static httpd_handle_t start_webserver(void) { httpd_config_t config = HTTPD_DEFAULT_CONFIG(); config.server_port = HTTP_SERVER_PORT; config.max_uri_handlers = HTTP_MAX_URI_HANDLERS; config.recv_wait_timeout = HTTP_RECV_TIMEOUT_SEC; config.send_wait_timeout = HTTP_SEND_TIMEOUT_SEC; ESP_LOGI(SYSTEM_TAG, "Starting server on port: '%d'", config.server_port); if (httpd_start(&server, &config) == ESP_OK) { ESP_LOGI(SYSTEM_TAG, "Registering URI handlers"); // Root handler httpd_uri_t root = { .uri = "/", .method = HTTP_GET, .handler = root_get_handler, .user_ctx = NULL }; httpd_register_uri_handler(server, &root); // Status handler httpd_uri_t status = { .uri = "/status", .method = HTTP_GET, .handler = status_get_handler, .user_ctx = NULL }; httpd_register_uri_handler(server, &status); // Fan control handler httpd_uri_t fan = { .uri = "/fan", .method = HTTP_POST, .handler = fan_post_handler, .user_ctx = NULL }; httpd_register_uri_handler(server, &fan); // OPTIONS handler for CORS preflight httpd_uri_t options_status = { .uri = "/status", .method = HTTP_OPTIONS, .handler = options_handler, .user_ctx = NULL }; httpd_register_uri_handler(server, &options_status); httpd_uri_t options_fan = { .uri = "/fan", .method = HTTP_OPTIONS, .handler = options_handler, .user_ctx = NULL }; httpd_register_uri_handler(server, &options_fan); return server; } ESP_LOGI(SYSTEM_TAG, "Error starting server!"); 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!"); // Initialize NVS esp_err_t ret = nvs_flash_init(); if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { ESP_ERROR_CHECK(nvs_flash_erase()); ret = nvs_flash_init(); } ESP_ERROR_CHECK(ret); // Initialize watchdog timer init_watchdog(); // Initialize motor control system ESP_LOGI(SYSTEM_TAG, "Initializing motor control system..."); ret = motor_control_init(); if (ret != ESP_OK) { ESP_LOGE(SYSTEM_TAG, "Failed to initialize motor control: %s", esp_err_to_name(ret)); return; } // Load saved state from NVS and potentially restore motor state ESP_LOGI(SYSTEM_TAG, "Loading saved state..."); load_motor_state_from_nvs(); ESP_LOGI(SYSTEM_TAG, "Connecting to WiFi network: %s", WIFI_SSID); wifi_init_sta(); // Start HTTP server start_webserver(); // Report final motor state after initialization const motor_state_t* final_state = motor_get_state(); ESP_LOGI(SYSTEM_TAG, "=== MOTOR STATE AFTER INITIALIZATION ==="); ESP_LOGI(SYSTEM_TAG, "Final motor state: 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)); if (final_state->mode != MOTOR_OFF && final_state->target_speed > 0) { ESP_LOGI(SYSTEM_TAG, "Motor restored to: %s @ %d%%", motor_mode_to_string(final_state->mode), final_state->target_speed); } else { ESP_LOGI(SYSTEM_TAG, "Motor remains OFF"); } ESP_LOGI(SYSTEM_TAG, "======================================="); ESP_LOGI(SYSTEM_TAG, "=== Enhanced Maxxfan Controller Ready! ==="); 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, "Open your browser and go to: http://[ESP32_IP_ADDRESS]"); ESP_LOGI(SYSTEM_TAG, "Check the monitor output above for your IP address"); // Main loop - reset watchdog periodically and update motor cooldown while (1) { feed_watchdog(); // Update motor cooldown time for status reporting motor_update_cooldown_time(); vTaskDelay(pdMS_TO_TICKS(WATCHDOG_FEED_INTERVAL_MS)); } }