Replace polling it with a blocking wait on connectivity flag and

notification
This commit is contained in:
2025-12-04 21:51:02 +01:00
parent 567fca2916
commit a332c37e72
+3 -8
View File
@@ -8,7 +8,6 @@
#include "i2c.hpp" #include "i2c.hpp"
#define MQTT_TASK_TIMEOUT_MS 3000 #define MQTT_TASK_TIMEOUT_MS 3000
#define MQTT_TIMEOUT_MS (MQTT_TASK_TIMEOUT_MS / 2)
static const char* TAG = "MQTT"; static const char* TAG = "MQTT";
@@ -77,16 +76,12 @@ void mqttTask(void *pvParameters) {
for(;;) { for(;;) {
ESP_LOGV(TAG, "Checking broker connection..."); ESP_LOGV(TAG, "Checking broker connection...");
EventBits_t bits = xEventGroupGetBits(connectivity_event_group); xEventGroupWaitBits(connectivity_event_group, WIFI_CONNECTED_BIT | MQTT_CONNECTED_BIT, pdFALSE, pdTRUE, portMAX_DELAY);
if ((bits & (WIFI_CONNECTED_BIT | MQTT_CONNECTED_BIT)) == (WIFI_CONNECTED_BIT | MQTT_CONNECTED_BIT)) {
ESP_LOGV(TAG, "Connection established"); ESP_LOGV(TAG, "Connection established");
uint32_t task_notification = ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(MQTT_TASK_TIMEOUT_MS)); if (ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(MQTT_TASK_TIMEOUT_MS))) {
if (task_notification) i2c_do(global_rx_buffer); i2c_do(global_rx_buffer);
else {
ESP_LOGW(TAG, "Waiting for connection...");
vTaskDelay(pdMS_TO_TICKS(MQTT_TIMEOUT_MS));
} }
ESP_ERROR_CHECK(esp_task_wdt_reset()); ESP_ERROR_CHECK(esp_task_wdt_reset());