From 5dd2c00ac0a057b9291c4d0b3f55a927644bd179 Mon Sep 17 00:00:00 2001 From: Erick Ahmed Date: Sun, 30 Nov 2025 23:35:23 +0100 Subject: [PATCH] Parametrize task delay values --- firmware/esp32/main/main.cpp | 5 ++++- firmware/esp32/main/mqtt.cpp | 16 +++++++++++++--- firmware/esp32/main/wifi.cpp | 4 ++-- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/firmware/esp32/main/main.cpp b/firmware/esp32/main/main.cpp index 3f68264..55baf4f 100644 --- a/firmware/esp32/main/main.cpp +++ b/firmware/esp32/main/main.cpp @@ -7,7 +7,10 @@ #include "esp_task_wdt.h" #include "wifi.hpp" +#define WATCHDOG_KEEPALIVE_TICKS pdMS_TO_TICKS(8000) + extern EventGroupHandle_t connectivity_event_group; +extern EventBits_t bits = xEventGroupGetBits(connectivity_event_group); static const char* TAG = "MAIN"; @@ -24,7 +27,7 @@ static void watchdogTask(void *pvParameters) { ESP_ERROR_CHECK(esp_task_wdt_add(NULL)); - const TickType_t keepAlivePeriod = pdMS_TO_TICKS(8000); + const TickType_t keepAlivePeriod = pdMS_TO_TICKS(WATCHDOG_KEEPALIVE_TICKS); for(;;) { if (criticalErrorFlag) { diff --git a/firmware/esp32/main/mqtt.cpp b/firmware/esp32/main/mqtt.cpp index 7fd9940..bf6c0b8 100644 --- a/firmware/esp32/main/mqtt.cpp +++ b/firmware/esp32/main/mqtt.cpp @@ -5,6 +5,10 @@ #include "esp_log.h" #include "mqtt_client.h" #include "main.hpp" +#include "i2c.hpp" + +#define MQTT_TASK_TIMEOUT_TICKS pdMS_TO_TICKS(3000) +#define MQTT_TIMEOUT_TICKS pdMS_TO_TICKS(MQTT_TASK_TIMEOUT/2) static const char* TAG = "MQTT"; @@ -57,11 +61,17 @@ void mqttTask(void *pvParameters) { for(;;) { ESP_LOGI(TAG, "Checking broker connection..."); - if(bits && MQTT_CONNECTED_BIT) mqtt_publish(); - else ESP_LOGW(TAG, "Waiting for connection..."); + if(bits && MQTT_CONNECTED_BIT) { + ESP_LOGI(TAG, "Connection estabilished"); + + uint32_t task_notification = ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(MQTT_TASK_TIMEOUT_TICKS)); + if (task_notification) twi_do(global_rx_buffer); //FIXME: develop i2c libs! (this func should then call mqtt_publish() if needed) + } else { + ESP_LOGW(TAG, "Waiting for connection..."); + vTaskDelay(pdMS_TO_TICKS(MQTT_TIMEOUT_TICKS)); + } ESP_ERROR_CHECK(esp_task_wdt_reset()); ESP_LOGI(TAG, "Task reset"); - vTaskDelay(pdMS_TO_TICKS(3000)); } } diff --git a/firmware/esp32/main/wifi.cpp b/firmware/esp32/main/wifi.cpp index cc53964..7c850c3 100644 --- a/firmware/esp32/main/wifi.cpp +++ b/firmware/esp32/main/wifi.cpp @@ -8,7 +8,7 @@ #include "main.hpp" #include "wifi-credentials.h" -#define WIFI_TIMEOUT_TICKS pdMS_TO_TICKS(8000) +#define WIFI_TIMEOUT_TICKS pdMS_TO_TICKS(4500) static const char* TAG = "WIFI"; @@ -83,6 +83,6 @@ void wifiTask(void *pvParameters) { for(;;) { ESP_ERROR_CHECK(esp_task_wdt_reset()); ESP_LOGI(TAG, "Task reset"); - vTaskDelay(pdMS_TO_TICKS(4000)); + vTaskDelay(pdMS_TO_TICKS(WIFI_TIMEOUT_TICKS)); } }