#include "esp_event.h" #include "esp_log.h" #include "esp_system.h" #include "esp_timer.h" #include "nvs_flash.h" #include "mqtt.h" #include "wifi.h" static const char *TAG = "app"; /** * @brief Initialize NVS flash storage, handling full or incompatible pages. * * @return esp_err_t ESP_OK on success, error code otherwise. */ static esp_err_t nvs_flash_init_check(void) { esp_err_t ret = nvs_flash_init(); if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { ESP_LOGW(TAG, "NVS partition was full or incompatible, erasing..."); ESP_ERROR_CHECK(nvs_flash_erase()); ret = nvs_flash_init(); } if (ret != ESP_OK) ESP_LOGE(TAG, "Failed to initialize NVS (%s)", esp_err_to_name(ret)); return ret; } /** * @brief Application entry point. * * Initializes NVS, network interface, event loop, * WiFi station, and MQTT client with proper error handling and logging. */ void app_main(void) { esp_err_t ret = nvs_flash_init_check(); if (ret != ESP_OK) { ESP_LOGE(TAG, "NVS init failed, aborting app start"); return; } ret = esp_netif_init(); if (ret != ESP_OK) { ESP_LOGE(TAG, "Failed to initialize network interface (%s)", esp_err_to_name(ret)); return; } ret = esp_event_loop_create_default(); if (ret != ESP_OK) { ESP_LOGE(TAG, "Failed to create event loop (%s)", esp_err_to_name(ret)); return; } ESP_LOGI(TAG, "Initializing WiFi in station mode"); wifi_init_sta(); ESP_LOGI(TAG, "Starting MQTT client"); mqtt_app_start(); ESP_LOGI(TAG, "Application setup completed"); }