aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c68
1 files changed, 68 insertions, 0 deletions
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 @@
1#include "esp_event.h"
2#include "esp_log.h"
3#include "esp_system.h"
4#include "esp_timer.h"
5#include "nvs_flash.h"
6
7#include "mqtt.h"
8#include "wifi.h"
9
10static const char *TAG = "app";
11
12/**
13 * @brief Initialize NVS flash storage, handling full or incompatible pages.
14 *
15 * @return esp_err_t ESP_OK on success, error code otherwise.
16 */
17static esp_err_t nvs_flash_init_check(void)
18{
19 esp_err_t ret = nvs_flash_init();
20 if (ret == ESP_ERR_NVS_NO_FREE_PAGES ||
21 ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
22 ESP_LOGW(TAG,
23 "NVS partition was full or incompatible, erasing...");
24 ESP_ERROR_CHECK(nvs_flash_erase());
25 ret = nvs_flash_init();
26 }
27 if (ret != ESP_OK)
28 ESP_LOGE(TAG, "Failed to initialize NVS (%s)",
29 esp_err_to_name(ret));
30 return ret;
31}
32
33/**
34 * @brief Application entry point.
35 *
36 * Initializes NVS, network interface, event loop,
37 * WiFi station, and MQTT client with proper error handling and logging.
38 */
39void app_main(void)
40{
41 esp_err_t ret = nvs_flash_init_check();
42 if (ret != ESP_OK) {
43 ESP_LOGE(TAG, "NVS init failed, aborting app start");
44 return;
45 }
46
47 ret = esp_netif_init();
48 if (ret != ESP_OK) {
49 ESP_LOGE(TAG, "Failed to initialize network interface (%s)",
50 esp_err_to_name(ret));
51 return;
52 }
53
54 ret = esp_event_loop_create_default();
55 if (ret != ESP_OK) {
56 ESP_LOGE(TAG, "Failed to create event loop (%s)",
57 esp_err_to_name(ret));
58 return;
59 }
60
61 ESP_LOGI(TAG, "Initializing WiFi in station mode");
62 wifi_init_sta();
63
64 ESP_LOGI(TAG, "Starting MQTT client");
65 mqtt_app_start();
66
67 ESP_LOGI(TAG, "Application setup completed");
68}