From e00f3a9ede1b8e46b480bd68daf48da0bb08acae Mon Sep 17 00:00:00 2001 From: Filip Wandzio Date: Thu, 4 Sep 2025 01:11:11 +0200 Subject: Initial --- src/main.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/main.c (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..790959d --- /dev/null +++ b/src/main.c @@ -0,0 +1,68 @@ +#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"); +} -- cgit v1.2.3