From 6fd2d2523d8b64161f642a73d6419de6c75f640e Mon Sep 17 00:00:00 2001 From: Erick Ahmed Date: Mon, 1 Dec 2025 00:53:12 +0100 Subject: [PATCH] Add global event group handle for connectivity --- firmware/esp32/include/main.hpp | 14 +++++++++----- firmware/esp32/main/main.cpp | 5 +++-- firmware/esp32/main/mqtt.cpp | 12 ++++++------ firmware/esp32/main/wifi.cpp | 4 ---- 4 files changed, 18 insertions(+), 17 deletions(-) diff --git a/firmware/esp32/include/main.hpp b/firmware/esp32/include/main.hpp index 1433a50..7fad7f3 100644 --- a/firmware/esp32/include/main.hpp +++ b/firmware/esp32/include/main.hpp @@ -1,9 +1,13 @@ - #ifdef __cplusplus - extern "C" { - #endif + #ifndef MAIN_HPP + #define MAIN_HPP + + #include "freertos/FreeRTOS.h" + #include "freertos/event_groups.h" extern volatile bool criticalErrorFlag; + extern EventGroupHandle_t connectivity_event_group; + + #define WIFI_CONNECTED_BIT BIT0 + #define MQTT_CONNECTED_BIT BIT1 - #ifdef __cplusplus - } #endif diff --git a/firmware/esp32/main/main.cpp b/firmware/esp32/main/main.cpp index a604e55..2f70b9e 100644 --- a/firmware/esp32/main/main.cpp +++ b/firmware/esp32/main/main.cpp @@ -9,8 +9,7 @@ #define WATCHDOG_KEEPALIVE_MS 8000 -extern EventGroupHandle_t connectivity_event_group; -extern EventBits_t bits = xEventGroupGetBits(connectivity_event_group); +EventGroupHandle_t connectivity_event_group; static const char* TAG = "MAIN"; @@ -49,6 +48,8 @@ extern "C" void app_main(void) }; esp_pm_configure(&pm_cfg); + connectivity_event_group = xEventGroupCreate(); + xTaskCreate(watchdogTask, "watchdogs", 2048, NULL, configMAX_PRIORITIES-1, NULL); xTaskCreate(wifiTask, "wifi", 4096, NULL, configMAX_PRIORITIES-4, NULL); diff --git a/firmware/esp32/main/mqtt.cpp b/firmware/esp32/main/mqtt.cpp index bc9c905..d16b97f 100644 --- a/firmware/esp32/main/mqtt.cpp +++ b/firmware/esp32/main/mqtt.cpp @@ -14,14 +14,14 @@ static const char* TAG = "MQTT"; static TaskHandle_t i2c_task_handle = NULL; static esp_mqtt_client_handle_t mqtt_client = NULL; -const int MQTT_CONNECTED_BIT = BIT0; static void mqtt_subscribe(void) { msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0); } static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) { - esp_mqtt_event_handle_t event = event_data; + esp_mqtt_event_handle_t event = (esp_mqtt_event_handle_t)event_data; + switch (event->event_id) { case MQTT_EVENT_CONNECTED: xEventGroupSetBits(connectivity_event_group, MQTT_CONNECTED_BIT); @@ -65,8 +65,6 @@ static void mqtt_init(void) { esp_mqtt_client_start(client); } -void mqtt_publish(void) {} - void mqttTask(void *pvParameters) { ESP_LOGV(TAG, "Task started"); @@ -79,8 +77,10 @@ void mqttTask(void *pvParameters) { for(;;) { ESP_LOGV(TAG, "Checking broker connection..."); - if(bits && MQTT_CONNECTED_BIT) { - ESP_LOGV(TAG, "Connection estabilished"); + EventBits_t bits = xEventGroupGetBits(connectivity_event_group); + + if ((bits & (WIFI_CONNECTED_BIT | MQTT_CONNECTED_BIT)) == (WIFI_CONNECTED_BIT | MQTT_CONNECTED_BIT)) { + ESP_LOGV(TAG, "Connection established"); uint32_t task_notification = ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(MQTT_TASK_TIMEOUT_MS)); if (task_notification) twi_do(global_rx_buffer); //FIXME: develop i2c libs! (this func should then call mqtt_publish() if needed) diff --git a/firmware/esp32/main/wifi.cpp b/firmware/esp32/main/wifi.cpp index ccfc3cc..8a75804 100644 --- a/firmware/esp32/main/wifi.cpp +++ b/firmware/esp32/main/wifi.cpp @@ -12,8 +12,6 @@ static const char* TAG = "WIFI"; -const int WIFI_CONNECTED_BIT = BIT0; - static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { if (event_base == WIFI_EVENT) { switch (event_id) { @@ -49,8 +47,6 @@ static void wifi_init_sta(void) { ESP_ERROR_CHECK(nvs_flash_init()); } - connectivity_event_group = xEventGroupCreate(); - ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_event_loop_create_default());