From cd4d2658b98ae38ac00d553bd19a7eb0f72c8c41 Mon Sep 17 00:00:00 2001 From: Erick Ahmed Date: Thu, 6 Nov 2025 17:58:08 +0100 Subject: [PATCH] Initialize MQTT task --- firmware/esp32/main/CMakeLists.txt | 4 +++- firmware/esp32/main/main.cpp | 1 + firmware/esp32/main/mqtt.cpp | 32 ++++++++++++++++++++++++++++++ firmware/esp32/main/wifi.cpp | 3 --- 4 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 firmware/esp32/main/mqtt.cpp diff --git a/firmware/esp32/main/CMakeLists.txt b/firmware/esp32/main/CMakeLists.txt index 6ea9e3e..a9ed769 100644 --- a/firmware/esp32/main/CMakeLists.txt +++ b/firmware/esp32/main/CMakeLists.txt @@ -1,5 +1,6 @@ idf_component_register(SRCS "./main.cpp" "./wifi.cpp" + "./mqtt.cpp" INCLUDE_DIRS "../include" REQUIRES esp_wifi nvs_flash @@ -7,4 +8,5 @@ idf_component_register(SRCS "./main.cpp" esp_hw_support esp_system freertos - log) + log + mqtt_client) diff --git a/firmware/esp32/main/main.cpp b/firmware/esp32/main/main.cpp index deaad6f..6d3740f 100644 --- a/firmware/esp32/main/main.cpp +++ b/firmware/esp32/main/main.cpp @@ -48,4 +48,5 @@ extern "C" void app_main(void) xTaskCreate(watchdogTask, "watchdogs", 2048, NULL, configMAX_PRIORITIES-1, NULL); xTaskCreate(wifiTask, "wifi", 4096, NULL, configMAX_PRIORITIES-4, NULL); + xTaskCreate(wifiTask, "mqtt", 2048, NULL, configMAX_PRIORITIES-8, NULL); } diff --git a/firmware/esp32/main/mqtt.cpp b/firmware/esp32/main/mqtt.cpp new file mode 100644 index 0000000..ddbc3ce --- /dev/null +++ b/firmware/esp32/main/mqtt.cpp @@ -0,0 +1,32 @@ +#include "freertos/FreeRTOS.h" +#include "freertos/event_groups.h" +#include "esp_task_wdt.h" +#include "esp_event.h" +#include "esp_log.h" +#include "mqtt_client.h" +#include "main.hpp" + +static const char* TAG = "MQTT"; + +static void mqtt_init(void) {} + +static void mqtt_publish(void) {} + +void mqttTask(void *pvParameters) { + ESP_LOGI(TAG, "Task started"); + + mqtt_init(); + ESP_ERROR_CHECK(esp_task_wdt_add(NULL)); + + + for(;;) { + ESP_LOGI(TAG, "Checking Wi-Fi connection..."); + xEventGroupWaitBits(wifi_event_group, WIFI_CONNECTED_BIT, pdFALSE, pdTRUE, portMAX_DELAY); + + if (bits & WIFI_CONNECTED_BIT) mqtt_publish(); + + ESP_ERROR_CHECK(esp_task_wdt_reset()); + ESP_LOGI(TAG, "Task reset"); + vTaskDelay(pdMS_TO_TICKS(6000)); + } +} diff --git a/firmware/esp32/main/wifi.cpp b/firmware/esp32/main/wifi.cpp index 0dc72c8..8551451 100644 --- a/firmware/esp32/main/wifi.cpp +++ b/firmware/esp32/main/wifi.cpp @@ -42,7 +42,6 @@ static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t e } } - static void wifi_init_sta(void) { esp_err_t nvs_err = nvs_flash_init(); @@ -83,8 +82,6 @@ void wifiTask(void *pvParameters) { ESP_ERROR_CHECK(esp_task_wdt_add(NULL)); for(;;) { - EventBits_t bits = xEventGroupWaitBits(wifi_event_group, WIFI_CONNECTED_BIT, pdFALSE, pdTRUE, WIFI_TIMEOUT_TICKS); - ESP_ERROR_CHECK(esp_task_wdt_reset()); ESP_LOGI(TAG, "Wi-Fi task reset"); vTaskDelay(pdMS_TO_TICKS(4000));