From cd4d2658b98ae38ac00d553bd19a7eb0f72c8c41 Mon Sep 17 00:00:00 2001 From: Erick Ahmed Date: Thu, 6 Nov 2025 17:58:08 +0100 Subject: [PATCH 01/32] 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)); From 29ad9350a32b8db4e633720cafa781d092665364 Mon Sep 17 00:00:00 2001 From: Erick Ahmed Date: Sat, 29 Nov 2025 14:15:17 +0100 Subject: [PATCH 02/32] Fix task name --- firmware/esp32/main/main.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/firmware/esp32/main/main.cpp b/firmware/esp32/main/main.cpp index 6d3740f..7b6cf68 100644 --- a/firmware/esp32/main/main.cpp +++ b/firmware/esp32/main/main.cpp @@ -48,5 +48,6 @@ 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); + + xTaskCreate(mqttTask, "mqtt", 2048, NULL, configMAX_PRIORITIES-8, NULL); } From 28ac3f44b3d0d725560b4c46c14e159842cfeeae Mon Sep 17 00:00:00 2001 From: Erick Ahmed Date: Sat, 29 Nov 2025 14:16:11 +0100 Subject: [PATCH 03/32] Fix task delay duration --- firmware/esp32/main/mqtt.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firmware/esp32/main/mqtt.cpp b/firmware/esp32/main/mqtt.cpp index ddbc3ce..2275f4a 100644 --- a/firmware/esp32/main/mqtt.cpp +++ b/firmware/esp32/main/mqtt.cpp @@ -27,6 +27,6 @@ void mqttTask(void *pvParameters) { ESP_ERROR_CHECK(esp_task_wdt_reset()); ESP_LOGI(TAG, "Task reset"); - vTaskDelay(pdMS_TO_TICKS(6000)); + vTaskDelay(pdMS_TO_TICKS(3000)); } } From 1ef2476f782a6486fd619154d6159d9e137fc874 Mon Sep 17 00:00:00 2001 From: Erick Ahmed Date: Sun, 30 Nov 2025 17:40:54 +0100 Subject: [PATCH 04/32] MQTT initialization --- firmware/esp32/main/mqtt.cpp | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/firmware/esp32/main/mqtt.cpp b/firmware/esp32/main/mqtt.cpp index 2275f4a..1d7d055 100644 --- a/firmware/esp32/main/mqtt.cpp +++ b/firmware/esp32/main/mqtt.cpp @@ -8,22 +8,45 @@ static const char* TAG = "MQTT"; -static void mqtt_init(void) {} + +static void mqtt_init(void) { + // TODO; implement Trust only or Mutual TLS in the future (and secure boot + flash encryption) + const esp_mqtt_client_config_t mqtt_cfg = { + .broker = { + //TODO: make this a definition in config.hpp + .address.uri = "mqtt://yourmqttserver", + .address.port = 1883 + }, + + esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); + mqttClient = client; + /* The last argument may be used to pass data to the event handler, in this example mqtt_event_handler */ + esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL); + esp_mqtt_client_start(client); +} + +static void mqtt_subscribe(void) { + msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0); +} static void mqtt_publish(void) {} +static void mqtt_fetch(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(); + if (bits & WIFI_CONNECTED_BIT) { + mqtt_publish(); + mqtt_fetch(); + } ESP_ERROR_CHECK(esp_task_wdt_reset()); ESP_LOGI(TAG, "Task reset"); From 401c610a58ec2986619abea85b2dc30badaec055 Mon Sep 17 00:00:00 2001 From: Erick Ahmed Date: Sun, 30 Nov 2025 22:22:26 +0100 Subject: [PATCH 05/32] Create shared event group --- firmware/esp32/main/main.cpp | 2 ++ firmware/esp32/main/wifi.cpp | 8 ++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/firmware/esp32/main/main.cpp b/firmware/esp32/main/main.cpp index 7b6cf68..3f68264 100644 --- a/firmware/esp32/main/main.cpp +++ b/firmware/esp32/main/main.cpp @@ -7,6 +7,8 @@ #include "esp_task_wdt.h" #include "wifi.hpp" +extern EventGroupHandle_t connectivity_event_group; + static const char* TAG = "MAIN"; volatile bool criticalErrorFlag = false; diff --git a/firmware/esp32/main/wifi.cpp b/firmware/esp32/main/wifi.cpp index 8551451..2f2c155 100644 --- a/firmware/esp32/main/wifi.cpp +++ b/firmware/esp32/main/wifi.cpp @@ -13,7 +13,7 @@ static const char* TAG = "WIFI"; const int WIFI_CONNECTED_BIT = BIT0; -EventGroupHandle_t wifi_event_group; +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) { @@ -27,7 +27,7 @@ static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t e wifi_event_sta_disconnected_t* disconn = (wifi_event_sta_disconnected_t*) event_data; ESP_LOGW(TAG, "Disconnected, reason: %d", disconn->reason); - xEventGroupClearBits(wifi_event_group, WIFI_CONNECTED_BIT); + xEventGroupClearBits(connectivity_event_group, WIFI_CONNECTED_BIT); esp_wifi_connect(); break; } @@ -38,7 +38,7 @@ static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t e else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; ESP_LOGI(TAG, "Got IP: " IPSTR, IP2STR(&event->ip_info.ip)); - xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_BIT); + xEventGroupSetBits(connectivity_event_group, WIFI_CONNECTED_BIT); } } @@ -50,7 +50,7 @@ static void wifi_init_sta(void) { ESP_ERROR_CHECK(nvs_flash_init()); } - wifi_event_group = xEventGroupCreate(); + connectivity_event_group = xEventGroupCreate(); ESP_ERROR_CHECK(esp_netif_init()); From 665125e07fd4e2883a6d0f6188d1e9e08b8b66e8 Mon Sep 17 00:00:00 2001 From: Erick Ahmed Date: Sun, 30 Nov 2025 22:24:09 +0100 Subject: [PATCH 06/32] Implement MQTT event groups --- firmware/esp32/main/mqtt.cpp | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/firmware/esp32/main/mqtt.cpp b/firmware/esp32/main/mqtt.cpp index 1d7d055..7fd9940 100644 --- a/firmware/esp32/main/mqtt.cpp +++ b/firmware/esp32/main/mqtt.cpp @@ -8,6 +8,23 @@ static const char* TAG = "MQTT"; +const int MQTT_CONNECTED_BIT = BIT0; + +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; + switch (event->event_id) { + case MQTT_EVENT_CONNECTED: + xEventGroupSetBits(connectivity_event_group, MQTT_CONNECTED_BIT); + ESP_LOGI(TAG, "Connected to broker"); + break; + case MQTT_EVENT_DISCONNECTED: + xEventGroupClearBits(connectivity_event_group, MQTT_CONNECTED_BIT); + ESP_LOGI(TAG, "Disconnected from broker"); + break; + default: + break; + } +} static void mqtt_init(void) { // TODO; implement Trust only or Mutual TLS in the future (and secure boot + flash encryption) @@ -31,8 +48,6 @@ static void mqtt_subscribe(void) { static void mqtt_publish(void) {} -static void mqtt_fetch(void) {} - void mqttTask(void *pvParameters) { ESP_LOGI(TAG, "Task started"); @@ -40,13 +55,10 @@ void mqttTask(void *pvParameters) { 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); + ESP_LOGI(TAG, "Checking broker connection..."); - if (bits & WIFI_CONNECTED_BIT) { - mqtt_publish(); - mqtt_fetch(); - } + if(bits && MQTT_CONNECTED_BIT) mqtt_publish(); + else ESP_LOGW(TAG, "Waiting for connection..."); ESP_ERROR_CHECK(esp_task_wdt_reset()); ESP_LOGI(TAG, "Task reset"); From 2fcc3172aa3d92adc7471a943c079ed0fc17c3bc Mon Sep 17 00:00:00 2001 From: Erick Ahmed Date: Sun, 30 Nov 2025 22:24:33 +0100 Subject: [PATCH 07/32] Fix message logs for consistency with codebase --- firmware/esp32/main/wifi.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/firmware/esp32/main/wifi.cpp b/firmware/esp32/main/wifi.cpp index 2f2c155..cc53964 100644 --- a/firmware/esp32/main/wifi.cpp +++ b/firmware/esp32/main/wifi.cpp @@ -11,7 +11,6 @@ #define WIFI_TIMEOUT_TICKS pdMS_TO_TICKS(8000) static const char* TAG = "WIFI"; -const int WIFI_CONNECTED_BIT = BIT0; const int WIFI_CONNECTED_BIT = BIT0; @@ -19,7 +18,7 @@ static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t e if (event_base == WIFI_EVENT) { switch (event_id) { case WIFI_EVENT_STA_START: - ESP_LOGI(TAG, "Wi-Fi process started, connecting..."); + ESP_LOGI(TAG, "Process started, connecting..."); esp_wifi_connect(); break; @@ -76,14 +75,14 @@ static void wifi_init_sta(void) { } void wifiTask(void *pvParameters) { - ESP_LOGI(TAG, "WiFi task started"); + ESP_LOGI(TAG, "Task started"); wifi_init_sta(); ESP_ERROR_CHECK(esp_task_wdt_add(NULL)); for(;;) { ESP_ERROR_CHECK(esp_task_wdt_reset()); - ESP_LOGI(TAG, "Wi-Fi task reset"); + ESP_LOGI(TAG, "Task reset"); vTaskDelay(pdMS_TO_TICKS(4000)); } } From 5dd2c00ac0a057b9291c4d0b3f55a927644bd179 Mon Sep 17 00:00:00 2001 From: Erick Ahmed Date: Sun, 30 Nov 2025 23:35:23 +0100 Subject: [PATCH 08/32] 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)); } } From 3670b58f8e53a616bedfb79368294eb107f9329c Mon Sep 17 00:00:00 2001 From: Erick Ahmed Date: Sun, 30 Nov 2025 23:36:54 +0100 Subject: [PATCH 09/32] Implement task handle for managing I2C --- firmware/esp32/main/mqtt.cpp | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/firmware/esp32/main/mqtt.cpp b/firmware/esp32/main/mqtt.cpp index bf6c0b8..876ed34 100644 --- a/firmware/esp32/main/mqtt.cpp +++ b/firmware/esp32/main/mqtt.cpp @@ -12,18 +12,36 @@ 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; switch (event->event_id) { case MQTT_EVENT_CONNECTED: xEventGroupSetBits(connectivity_event_group, MQTT_CONNECTED_BIT); + mqtt_subscribe(); ESP_LOGI(TAG, "Connected to broker"); break; case MQTT_EVENT_DISCONNECTED: xEventGroupClearBits(connectivity_event_group, MQTT_CONNECTED_BIT); - ESP_LOGI(TAG, "Disconnected from broker"); + ESP_LOGW(TAG, "Disconnected from broker"); + break; + case MQTT_EVENT_DATA: + ESP_LOGI(TAG, "Listening to broker"); + ESP_LOGV(TAG, "Topic: %.*s\n", event->topic_len, event->topic); + ESP_LOGV(TAG, "Data: %.*s\n", event->data_len, event->data); + + char incoming_message[50]; //FIXME: check how long is the longest message on AVR! + + memcpy(incoming_message, event->data, event->data_len); + incoming_message[event->data_len] = '\0'; //WARNING: maybe add some checks to the data_len + xTaskNotifyGive(i2c_task_handle); break; default: break; @@ -38,24 +56,24 @@ static void mqtt_init(void) { .address.uri = "mqtt://yourmqttserver", .address.port = 1883 }, + }; esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); mqttClient = client; - /* The last argument may be used to pass data to the event handler, in this example mqtt_event_handler */ + // the last argument may be used to pass data to the event handler esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL); esp_mqtt_client_start(client); } -static void mqtt_subscribe(void) { - msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0); -} - -static void mqtt_publish(void) {} +void mqtt_publish(void) {} void mqttTask(void *pvParameters) { ESP_LOGI(TAG, "Task started"); + i2c_task_handle = xTaskGetCurrentTaskHandle(); + mqtt_init(); + ESP_ERROR_CHECK(esp_task_wdt_add(NULL)); for(;;) { From 0a418805a47e1d20883ebce4298fd35ad8e702b3 Mon Sep 17 00:00:00 2001 From: Erick Ahmed Date: Sun, 30 Nov 2025 23:40:24 +0100 Subject: [PATCH 10/32] Changed some logs type from informative to verbose --- firmware/esp32/main/main.cpp | 2 +- firmware/esp32/main/mqtt.cpp | 12 ++++++------ firmware/esp32/main/wifi.cpp | 6 +++--- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/firmware/esp32/main/main.cpp b/firmware/esp32/main/main.cpp index 55baf4f..b9bf9fc 100644 --- a/firmware/esp32/main/main.cpp +++ b/firmware/esp32/main/main.cpp @@ -36,7 +36,7 @@ static void watchdogTask(void *pvParameters) { } ESP_ERROR_CHECK(esp_task_wdt_reset()); - ESP_LOGI(TAG, "WDT reset"); + ESP_LOGV(TAG, "WDT reset"); vTaskDelay(keepAlivePeriod); } diff --git a/firmware/esp32/main/mqtt.cpp b/firmware/esp32/main/mqtt.cpp index 876ed34..efbc970 100644 --- a/firmware/esp32/main/mqtt.cpp +++ b/firmware/esp32/main/mqtt.cpp @@ -26,14 +26,14 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ case MQTT_EVENT_CONNECTED: xEventGroupSetBits(connectivity_event_group, MQTT_CONNECTED_BIT); mqtt_subscribe(); - ESP_LOGI(TAG, "Connected to broker"); + ESP_LOGV(TAG, "Connected to broker"); break; case MQTT_EVENT_DISCONNECTED: xEventGroupClearBits(connectivity_event_group, MQTT_CONNECTED_BIT); ESP_LOGW(TAG, "Disconnected from broker"); break; case MQTT_EVENT_DATA: - ESP_LOGI(TAG, "Listening to broker"); + ESP_LOGV(TAG, "Listening to broker"); ESP_LOGV(TAG, "Topic: %.*s\n", event->topic_len, event->topic); ESP_LOGV(TAG, "Data: %.*s\n", event->data_len, event->data); @@ -68,7 +68,7 @@ static void mqtt_init(void) { void mqtt_publish(void) {} void mqttTask(void *pvParameters) { - ESP_LOGI(TAG, "Task started"); + ESP_LOGV(TAG, "Task started"); i2c_task_handle = xTaskGetCurrentTaskHandle(); @@ -77,10 +77,10 @@ void mqttTask(void *pvParameters) { ESP_ERROR_CHECK(esp_task_wdt_add(NULL)); for(;;) { - ESP_LOGI(TAG, "Checking broker connection..."); + ESP_LOGV(TAG, "Checking broker connection..."); if(bits && MQTT_CONNECTED_BIT) { - ESP_LOGI(TAG, "Connection estabilished"); + ESP_LOGV(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) @@ -90,6 +90,6 @@ void mqttTask(void *pvParameters) { } ESP_ERROR_CHECK(esp_task_wdt_reset()); - ESP_LOGI(TAG, "Task reset"); + ESP_LOGV(TAG, "Task reset"); } } diff --git a/firmware/esp32/main/wifi.cpp b/firmware/esp32/main/wifi.cpp index 7c850c3..b5ec5ef 100644 --- a/firmware/esp32/main/wifi.cpp +++ b/firmware/esp32/main/wifi.cpp @@ -18,7 +18,7 @@ static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t e if (event_base == WIFI_EVENT) { switch (event_id) { case WIFI_EVENT_STA_START: - ESP_LOGI(TAG, "Process started, connecting..."); + ESP_LOGV(TAG, "Process started, connecting..."); esp_wifi_connect(); break; @@ -75,14 +75,14 @@ static void wifi_init_sta(void) { } void wifiTask(void *pvParameters) { - ESP_LOGI(TAG, "Task started"); + ESP_LOGV(TAG, "Task started"); wifi_init_sta(); ESP_ERROR_CHECK(esp_task_wdt_add(NULL)); for(;;) { ESP_ERROR_CHECK(esp_task_wdt_reset()); - ESP_LOGI(TAG, "Task reset"); + ESP_LOGV(TAG, "Task reset"); vTaskDelay(pdMS_TO_TICKS(WIFI_TIMEOUT_TICKS)); } } From 213f6b399f40303cc369fc4339c7dd8c82fab80d Mon Sep 17 00:00:00 2001 From: Erick Ahmed Date: Sun, 30 Nov 2025 23:40:59 +0100 Subject: [PATCH 11/32] Changed some logs type from informative to debug --- firmware/esp32/main/mqtt.cpp | 6 +++--- firmware/esp32/main/wifi.cpp | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/firmware/esp32/main/mqtt.cpp b/firmware/esp32/main/mqtt.cpp index efbc970..88d9e80 100644 --- a/firmware/esp32/main/mqtt.cpp +++ b/firmware/esp32/main/mqtt.cpp @@ -33,9 +33,9 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ ESP_LOGW(TAG, "Disconnected from broker"); break; case MQTT_EVENT_DATA: - ESP_LOGV(TAG, "Listening to broker"); - ESP_LOGV(TAG, "Topic: %.*s\n", event->topic_len, event->topic); - ESP_LOGV(TAG, "Data: %.*s\n", event->data_len, event->data); + ESP_LOGD(TAG, "Listening to broker"); + ESP_LOGD(TAG, "Topic: %.*s\n", event->topic_len, event->topic); + ESP_LOGD(TAG, "Data: %.*s\n", event->data_len, event->data); char incoming_message[50]; //FIXME: check how long is the longest message on AVR! diff --git a/firmware/esp32/main/wifi.cpp b/firmware/esp32/main/wifi.cpp index b5ec5ef..8da5d36 100644 --- a/firmware/esp32/main/wifi.cpp +++ b/firmware/esp32/main/wifi.cpp @@ -36,7 +36,7 @@ static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t e } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; - ESP_LOGI(TAG, "Got IP: " IPSTR, IP2STR(&event->ip_info.ip)); + ESP_LOGD(TAG, "Got IP: " IPSTR, IP2STR(&event->ip_info.ip)); xEventGroupSetBits(connectivity_event_group, WIFI_CONNECTED_BIT); } } From c6c4011a470bd50d14d2a68685a28c415e435558 Mon Sep 17 00:00:00 2001 From: Erick Ahmed Date: Sun, 30 Nov 2025 23:46:21 +0100 Subject: [PATCH 12/32] Fix vTaskDelay syntax error --- firmware/esp32/main/main.cpp | 6 ++---- firmware/esp32/main/mqtt.cpp | 8 ++++---- firmware/esp32/main/wifi.cpp | 4 ++-- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/firmware/esp32/main/main.cpp b/firmware/esp32/main/main.cpp index b9bf9fc..a604e55 100644 --- a/firmware/esp32/main/main.cpp +++ b/firmware/esp32/main/main.cpp @@ -7,7 +7,7 @@ #include "esp_task_wdt.h" #include "wifi.hpp" -#define WATCHDOG_KEEPALIVE_TICKS pdMS_TO_TICKS(8000) +#define WATCHDOG_KEEPALIVE_MS 8000 extern EventGroupHandle_t connectivity_event_group; extern EventBits_t bits = xEventGroupGetBits(connectivity_event_group); @@ -27,8 +27,6 @@ static void watchdogTask(void *pvParameters) { ESP_ERROR_CHECK(esp_task_wdt_add(NULL)); - const TickType_t keepAlivePeriod = pdMS_TO_TICKS(WATCHDOG_KEEPALIVE_TICKS); - for(;;) { if (criticalErrorFlag) { ESP_LOGE(TAG, "Critical error flag raised. Rebooting."); @@ -38,7 +36,7 @@ static void watchdogTask(void *pvParameters) { ESP_ERROR_CHECK(esp_task_wdt_reset()); ESP_LOGV(TAG, "WDT reset"); - vTaskDelay(keepAlivePeriod); + vTaskDelay(pdMS_TO_TICKS(WATCHDOG_KEEPALIVE_MS)); } } diff --git a/firmware/esp32/main/mqtt.cpp b/firmware/esp32/main/mqtt.cpp index 88d9e80..bc9c905 100644 --- a/firmware/esp32/main/mqtt.cpp +++ b/firmware/esp32/main/mqtt.cpp @@ -7,8 +7,8 @@ #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) +#define MQTT_TASK_TIMEOUT_MS 3000 +#define MQTT_TIMEOUT_MS MQTT_TASK_TIMEOUT_MS/2 static const char* TAG = "MQTT"; @@ -82,11 +82,11 @@ void mqttTask(void *pvParameters) { if(bits && MQTT_CONNECTED_BIT) { ESP_LOGV(TAG, "Connection estabilished"); - uint32_t task_notification = ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(MQTT_TASK_TIMEOUT_TICKS)); + 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) } else { ESP_LOGW(TAG, "Waiting for connection..."); - vTaskDelay(pdMS_TO_TICKS(MQTT_TIMEOUT_TICKS)); + vTaskDelay(pdMS_TO_TICKS(MQTT_TIMEOUT_MS)); } ESP_ERROR_CHECK(esp_task_wdt_reset()); diff --git a/firmware/esp32/main/wifi.cpp b/firmware/esp32/main/wifi.cpp index 8da5d36..ccfc3cc 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(4500) +#define WIFI_TIMEOUT_MS 4500 static const char* TAG = "WIFI"; @@ -83,6 +83,6 @@ void wifiTask(void *pvParameters) { for(;;) { ESP_ERROR_CHECK(esp_task_wdt_reset()); ESP_LOGV(TAG, "Task reset"); - vTaskDelay(pdMS_TO_TICKS(WIFI_TIMEOUT_TICKS)); + vTaskDelay(pdMS_TO_TICKS(WIFI_TIMEOUT_MS)); } } From 6fd2d2523d8b64161f642a73d6419de6c75f640e Mon Sep 17 00:00:00 2001 From: Erick Ahmed Date: Mon, 1 Dec 2025 00:53:12 +0100 Subject: [PATCH 13/32] 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()); From 04b5fb0a7c6c314bd5828b918e8e2a70032f0ea5 Mon Sep 17 00:00:00 2001 From: Erick Ahmed Date: Mon, 1 Dec 2025 00:54:00 +0100 Subject: [PATCH 14/32] Fix task event notification --- firmware/esp32/main/mqtt.cpp | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/firmware/esp32/main/mqtt.cpp b/firmware/esp32/main/mqtt.cpp index d16b97f..db1325a 100644 --- a/firmware/esp32/main/mqtt.cpp +++ b/firmware/esp32/main/mqtt.cpp @@ -8,15 +8,17 @@ #include "i2c.hpp" #define MQTT_TASK_TIMEOUT_MS 3000 -#define MQTT_TIMEOUT_MS MQTT_TASK_TIMEOUT_MS/2 +#define MQTT_TIMEOUT_MS (MQTT_TASK_TIMEOUT_MS / 2) static const char* TAG = "MQTT"; static TaskHandle_t i2c_task_handle = NULL; static esp_mqtt_client_handle_t mqtt_client = NULL; -static void mqtt_subscribe(void) { - msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0); +static char global_rx_buffer[50]; + +static inline void mqtt_subscribe(void) { + esp_mqtt_client_subscribe(mqtt_client, "/topic/qos0", 0); } static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) { @@ -37,11 +39,12 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ ESP_LOGD(TAG, "Topic: %.*s\n", event->topic_len, event->topic); ESP_LOGD(TAG, "Data: %.*s\n", event->data_len, event->data); - char incoming_message[50]; //FIXME: check how long is the longest message on AVR! + int len = event->data_len; + if (len > sizeof(global_rx_buffer) - 1) len = sizeof(global_rx_buffer) - 1; + memcpy(global_rx_buffer, event->data, len); + global_rx_buffer[len] = '\0'; - memcpy(incoming_message, event->data, event->data_len); - incoming_message[event->data_len] = '\0'; //WARNING: maybe add some checks to the data_len - xTaskNotifyGive(i2c_task_handle); + if (i2c_task_handle != NULL) xTaskNotifyGive(i2c_task_handle); break; default: break; @@ -58,18 +61,15 @@ static void mqtt_init(void) { }, }; - esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); - mqttClient = client; - // the last argument may be used to pass data to the event handler - esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL); - esp_mqtt_client_start(client); + mqtt_client = esp_mqtt_client_init(&mqtt_cfg); + esp_mqtt_client_register_event(mqtt_client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL); + esp_mqtt_client_start(mqtt_client); } void mqttTask(void *pvParameters) { ESP_LOGV(TAG, "Task started"); i2c_task_handle = xTaskGetCurrentTaskHandle(); - mqtt_init(); ESP_ERROR_CHECK(esp_task_wdt_add(NULL)); @@ -83,8 +83,8 @@ void mqttTask(void *pvParameters) { 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) - } else { + if (task_notification) i2c_do(global_rx_buffer); + else { ESP_LOGW(TAG, "Waiting for connection..."); vTaskDelay(pdMS_TO_TICKS(MQTT_TIMEOUT_MS)); } From 567fca2916a8a30a3f59f2279bedd735b1672555 Mon Sep 17 00:00:00 2001 From: Erick Ahmed Date: Mon, 1 Dec 2025 00:55:59 +0100 Subject: [PATCH 15/32] Tweak logs --- firmware/esp32/main/mqtt.cpp | 3 ++- firmware/esp32/main/wifi.cpp | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/firmware/esp32/main/mqtt.cpp b/firmware/esp32/main/mqtt.cpp index db1325a..b43897b 100644 --- a/firmware/esp32/main/mqtt.cpp +++ b/firmware/esp32/main/mqtt.cpp @@ -67,7 +67,7 @@ static void mqtt_init(void) { } void mqttTask(void *pvParameters) { - ESP_LOGV(TAG, "Task started"); + ESP_LOGI(TAG, "Task started"); i2c_task_handle = xTaskGetCurrentTaskHandle(); mqtt_init(); @@ -92,4 +92,5 @@ void mqttTask(void *pvParameters) { ESP_ERROR_CHECK(esp_task_wdt_reset()); ESP_LOGV(TAG, "Task reset"); } + ESP_LOGI(TAG, "Task stopped"); } diff --git a/firmware/esp32/main/wifi.cpp b/firmware/esp32/main/wifi.cpp index 8a75804..d9e8aa6 100644 --- a/firmware/esp32/main/wifi.cpp +++ b/firmware/esp32/main/wifi.cpp @@ -71,7 +71,7 @@ static void wifi_init_sta(void) { } void wifiTask(void *pvParameters) { - ESP_LOGV(TAG, "Task started"); + ESP_LOGI(TAG, "Task started"); wifi_init_sta(); ESP_ERROR_CHECK(esp_task_wdt_add(NULL)); @@ -81,4 +81,5 @@ void wifiTask(void *pvParameters) { ESP_LOGV(TAG, "Task reset"); vTaskDelay(pdMS_TO_TICKS(WIFI_TIMEOUT_MS)); } + ESP_LOGI(TAG, "Task stopped"); } From a332c37e720052ff2d76e627b75fa9036c43b313 Mon Sep 17 00:00:00 2001 From: Erick Ahmed Date: Thu, 4 Dec 2025 21:51:02 +0100 Subject: [PATCH 16/32] Replace polling it with a blocking wait on connectivity flag and notification --- firmware/esp32/main/mqtt.cpp | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/firmware/esp32/main/mqtt.cpp b/firmware/esp32/main/mqtt.cpp index b43897b..0ab817b 100644 --- a/firmware/esp32/main/mqtt.cpp +++ b/firmware/esp32/main/mqtt.cpp @@ -8,7 +8,6 @@ #include "i2c.hpp" #define MQTT_TASK_TIMEOUT_MS 3000 -#define MQTT_TIMEOUT_MS (MQTT_TASK_TIMEOUT_MS / 2) static const char* TAG = "MQTT"; @@ -77,20 +76,16 @@ void mqttTask(void *pvParameters) { for(;;) { 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 (task_notification) i2c_do(global_rx_buffer); - else { - ESP_LOGW(TAG, "Waiting for connection..."); - vTaskDelay(pdMS_TO_TICKS(MQTT_TIMEOUT_MS)); + if (ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(MQTT_TASK_TIMEOUT_MS))) { + i2c_do(global_rx_buffer); } ESP_ERROR_CHECK(esp_task_wdt_reset()); ESP_LOGV(TAG, "Task reset"); - } + } ESP_LOGI(TAG, "Task stopped"); } From fb98ce800a175f0b8052b4e888de87d6a9c29e40 Mon Sep 17 00:00:00 2001 From: Erick Ahmed Date: Thu, 4 Dec 2025 21:51:16 +0100 Subject: [PATCH 17/32] Add network speed check for future use --- firmware/esp32/main/wifi.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/firmware/esp32/main/wifi.cpp b/firmware/esp32/main/wifi.cpp index d9e8aa6..5e7c5f5 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_MS 4500 +#define WIFI_TIMEOUT_MS 1500 static const char* TAG = "WIFI"; @@ -77,6 +77,13 @@ void wifiTask(void *pvParameters) { ESP_ERROR_CHECK(esp_task_wdt_add(NULL)); for(;;) { + wifi_ap_record_t ap; + if (esp_wifi_sta_get_ap_info(&ap) == ESP_OK) { + ESP_LOGV(TAG, "RSSI: %d", ap.rssi); + } + + //TODO: manage data send rate if with slow network + ESP_ERROR_CHECK(esp_task_wdt_reset()); ESP_LOGV(TAG, "Task reset"); vTaskDelay(pdMS_TO_TICKS(WIFI_TIMEOUT_MS)); From 985642e0feeb22fdb8275e61a6169d7fd6d043ee Mon Sep 17 00:00:00 2001 From: Erick Ahmed Date: Thu, 4 Dec 2025 23:00:34 +0100 Subject: [PATCH 18/32] Fix compilation issues with espressif mqtt lib --- firmware/esp32/include/mqtt.hpp | 6 +++++ firmware/esp32/include/wifi-credentials.h | 2 ++ firmware/esp32/main/CMakeLists.txt | 2 +- firmware/esp32/main/idf_component.yml | 2 ++ firmware/esp32/main/main.cpp | 1 + firmware/esp32/main/mqtt.cpp | 29 ++++++++++++----------- 6 files changed, 27 insertions(+), 15 deletions(-) create mode 100644 firmware/esp32/include/mqtt.hpp create mode 100644 firmware/esp32/main/idf_component.yml diff --git a/firmware/esp32/include/mqtt.hpp b/firmware/esp32/include/mqtt.hpp new file mode 100644 index 0000000..440c395 --- /dev/null +++ b/firmware/esp32/include/mqtt.hpp @@ -0,0 +1,6 @@ +#ifndef MQTT_HPP +#define MQTT_HPP + +void mqttTask(void *pvParameters); + +#endif diff --git a/firmware/esp32/include/wifi-credentials.h b/firmware/esp32/include/wifi-credentials.h index 271369b..b93536c 100644 --- a/firmware/esp32/include/wifi-credentials.h +++ b/firmware/esp32/include/wifi-credentials.h @@ -1,6 +1,8 @@ #ifndef WIFI_CREDENTIALS_H #define WIFI_CREDENTIALS_H +// TODO: make this a config.h + #define SSID "your_wifi_ssid" #define PASSWORD "your_wifi_password" #define AUTH_MODE WIFI_AUTH_WPA2_PSK diff --git a/firmware/esp32/main/CMakeLists.txt b/firmware/esp32/main/CMakeLists.txt index a9ed769..29932c5 100644 --- a/firmware/esp32/main/CMakeLists.txt +++ b/firmware/esp32/main/CMakeLists.txt @@ -9,4 +9,4 @@ idf_component_register(SRCS "./main.cpp" esp_system freertos log - mqtt_client) + mqtt) diff --git a/firmware/esp32/main/idf_component.yml b/firmware/esp32/main/idf_component.yml new file mode 100644 index 0000000..5334d28 --- /dev/null +++ b/firmware/esp32/main/idf_component.yml @@ -0,0 +1,2 @@ +dependencies: + espressif/mqtt: "*" diff --git a/firmware/esp32/main/main.cpp b/firmware/esp32/main/main.cpp index 2f70b9e..a12f469 100644 --- a/firmware/esp32/main/main.cpp +++ b/firmware/esp32/main/main.cpp @@ -6,6 +6,7 @@ #include "esp_pm.h" #include "esp_task_wdt.h" #include "wifi.hpp" +#include "mqtt.hpp" #define WATCHDOG_KEEPALIVE_MS 8000 diff --git a/firmware/esp32/main/mqtt.cpp b/firmware/esp32/main/mqtt.cpp index 0ab817b..9f84b21 100644 --- a/firmware/esp32/main/mqtt.cpp +++ b/firmware/esp32/main/mqtt.cpp @@ -5,7 +5,7 @@ #include "esp_log.h" #include "mqtt_client.h" #include "main.hpp" -#include "i2c.hpp" +//#include "i2c.hpp" #define MQTT_TASK_TIMEOUT_MS 3000 @@ -24,16 +24,18 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ esp_mqtt_event_handle_t event = (esp_mqtt_event_handle_t)event_data; switch (event->event_id) { - case MQTT_EVENT_CONNECTED: + case MQTT_EVENT_CONNECTED: { xEventGroupSetBits(connectivity_event_group, MQTT_CONNECTED_BIT); mqtt_subscribe(); ESP_LOGV(TAG, "Connected to broker"); break; - case MQTT_EVENT_DISCONNECTED: + } + case MQTT_EVENT_DISCONNECTED: { xEventGroupClearBits(connectivity_event_group, MQTT_CONNECTED_BIT); ESP_LOGW(TAG, "Disconnected from broker"); break; - case MQTT_EVENT_DATA: + } + case MQTT_EVENT_DATA: { ESP_LOGD(TAG, "Listening to broker"); ESP_LOGD(TAG, "Topic: %.*s\n", event->topic_len, event->topic); ESP_LOGD(TAG, "Data: %.*s\n", event->data_len, event->data); @@ -45,23 +47,22 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ if (i2c_task_handle != NULL) xTaskNotifyGive(i2c_task_handle); break; - default: + } + default: { break; + } } } static void mqtt_init(void) { // TODO; implement Trust only or Mutual TLS in the future (and secure boot + flash encryption) - const esp_mqtt_client_config_t mqtt_cfg = { - .broker = { - //TODO: make this a definition in config.hpp - .address.uri = "mqtt://yourmqttserver", - .address.port = 1883 - }, - }; + // TODO: add to config.h + esp_mqtt_client_config_t mqtt_cfg = {}; + mqtt_cfg.broker.address.uri = "mqtt://yourmqttserver"; + mqtt_cfg.broker.address.port = 1883; mqtt_client = esp_mqtt_client_init(&mqtt_cfg); - esp_mqtt_client_register_event(mqtt_client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL); + esp_mqtt_client_register_event(mqtt_client, (esp_mqtt_event_id_t)ESP_EVENT_ANY_ID, mqtt_event_handler, NULL); esp_mqtt_client_start(mqtt_client); } @@ -81,7 +82,7 @@ void mqttTask(void *pvParameters) { ESP_LOGV(TAG, "Connection established"); if (ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(MQTT_TASK_TIMEOUT_MS))) { - i2c_do(global_rx_buffer); + //i2c_do(global_rx_buffer); PLACEHOLDER FUNC! } ESP_ERROR_CHECK(esp_task_wdt_reset()); From b0059f6e9f67541d44aef250b22992c41d33484a Mon Sep 17 00:00:00 2001 From: Erick Ahmed Date: Tue, 9 Dec 2025 00:24:36 +0100 Subject: [PATCH 19/32] Move user configuration to config.h --- .../esp32/include/{wifi-credentials.h => config.h} | 6 ++++-- firmware/esp32/main/wifi.cpp | 12 +++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) rename firmware/esp32/include/{wifi-credentials.h => config.h} (54%) diff --git a/firmware/esp32/include/wifi-credentials.h b/firmware/esp32/include/config.h similarity index 54% rename from firmware/esp32/include/wifi-credentials.h rename to firmware/esp32/include/config.h index 271369b..0f74cfb 100644 --- a/firmware/esp32/include/wifi-credentials.h +++ b/firmware/esp32/include/config.h @@ -1,8 +1,10 @@ -#ifndef WIFI_CREDENTIALS_H -#define WIFI_CREDENTIALS_H +#ifndef CONFIG_H +#define CONFIG_H #define SSID "your_wifi_ssid" #define PASSWORD "your_wifi_password" #define AUTH_MODE WIFI_AUTH_WPA2_PSK +#define MQTT_ADDR "mqtt://yourmqttserver" +#define MQTT_PORT 1883 #endif diff --git a/firmware/esp32/main/wifi.cpp b/firmware/esp32/main/wifi.cpp index 0dc72c8..da49dd9 100644 --- a/firmware/esp32/main/wifi.cpp +++ b/firmware/esp32/main/wifi.cpp @@ -6,7 +6,7 @@ #include "esp_log.h" #include "nvs_flash.h" #include "main.hpp" -#include "wifi-credentials.h" +#include "config.h" #define WIFI_TIMEOUT_TICKS pdMS_TO_TICKS(8000) @@ -83,10 +83,16 @@ 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); + wifi_ap_record_t ap; + if (esp_wifi_sta_get_ap_info(&ap) == ESP_OK) { + ESP_LOGV(TAG, "RSSI: %d", ap.rssi); + } + + //TODO: manage data send rate if with slow network + // or just let mqtt know (useful for making a notice on home manager) ESP_ERROR_CHECK(esp_task_wdt_reset()); ESP_LOGI(TAG, "Wi-Fi task reset"); - vTaskDelay(pdMS_TO_TICKS(4000)); + vTaskDelay(pdMS_TO_TICKS(WIFI_TIMEOUT_TICKS)); } } From 2c7951c2bb9e805f8d3ac1d4fd59cff1af8b27ad Mon Sep 17 00:00:00 2001 From: Erick Ahmed Date: Tue, 9 Dec 2025 01:37:18 +0100 Subject: [PATCH 20/32] Fix missing code from merge for connectivity_event_group --- firmware/esp32/main/mqtt.cpp | 55 +++++++++++++++++++++--------------- firmware/esp32/main/wifi.cpp | 35 ++++++++--------------- 2 files changed, 45 insertions(+), 45 deletions(-) diff --git a/firmware/esp32/main/mqtt.cpp b/firmware/esp32/main/mqtt.cpp index 9f84b21..58eda80 100644 --- a/firmware/esp32/main/mqtt.cpp +++ b/firmware/esp32/main/mqtt.cpp @@ -6,6 +6,7 @@ #include "mqtt_client.h" #include "main.hpp" //#include "i2c.hpp" +#include "config-erick.h" #define MQTT_TASK_TIMEOUT_MS 3000 @@ -16,7 +17,7 @@ static esp_mqtt_client_handle_t mqtt_client = NULL; static char global_rx_buffer[50]; -static inline void mqtt_subscribe(void) { +static void mqtt_subscribe(void) { esp_mqtt_client_subscribe(mqtt_client, "/topic/qos0", 0); } @@ -24,52 +25,56 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ esp_mqtt_event_handle_t event = (esp_mqtt_event_handle_t)event_data; switch (event->event_id) { - case MQTT_EVENT_CONNECTED: { + case MQTT_EVENT_CONNECTED: xEventGroupSetBits(connectivity_event_group, MQTT_CONNECTED_BIT); mqtt_subscribe(); ESP_LOGV(TAG, "Connected to broker"); break; - } - case MQTT_EVENT_DISCONNECTED: { + + case MQTT_EVENT_DISCONNECTED: xEventGroupClearBits(connectivity_event_group, MQTT_CONNECTED_BIT); ESP_LOGW(TAG, "Disconnected from broker"); break; - } - case MQTT_EVENT_DATA: { - ESP_LOGD(TAG, "Listening to broker"); - ESP_LOGD(TAG, "Topic: %.*s\n", event->topic_len, event->topic); - ESP_LOGD(TAG, "Data: %.*s\n", event->data_len, event->data); + + case MQTT_EVENT_DATA: + { + ESP_LOGD(TAG, "Data received"); int len = event->data_len; if (len > sizeof(global_rx_buffer) - 1) len = sizeof(global_rx_buffer) - 1; + memcpy(global_rx_buffer, event->data, len); global_rx_buffer[len] = '\0'; if (i2c_task_handle != NULL) xTaskNotifyGive(i2c_task_handle); break; } - default: { + + default: break; - } } } static void mqtt_init(void) { - // TODO; implement Trust only or Mutual TLS in the future (and secure boot + flash encryption) - // TODO: add to config.h esp_mqtt_client_config_t mqtt_cfg = {}; - mqtt_cfg.broker.address.uri = "mqtt://yourmqttserver"; - mqtt_cfg.broker.address.port = 1883; + + mqtt_cfg.broker.address.uri = MQTT_ADDR; + mqtt_cfg.broker.address.port = MQTT_PORT; mqtt_client = esp_mqtt_client_init(&mqtt_cfg); + esp_mqtt_client_register_event(mqtt_client, (esp_mqtt_event_id_t)ESP_EVENT_ANY_ID, mqtt_event_handler, NULL); + esp_mqtt_client_start(mqtt_client); } +void mqtt_publish(void) {} + void mqttTask(void *pvParameters) { - ESP_LOGI(TAG, "Task started"); + ESP_LOGV(TAG, "Task started"); i2c_task_handle = xTaskGetCurrentTaskHandle(); + mqtt_init(); ESP_ERROR_CHECK(esp_task_wdt_add(NULL)); @@ -77,16 +82,22 @@ void mqttTask(void *pvParameters) { for(;;) { ESP_LOGV(TAG, "Checking broker connection..."); - xEventGroupWaitBits(connectivity_event_group, WIFI_CONNECTED_BIT | MQTT_CONNECTED_BIT, pdFALSE, pdTRUE, portMAX_DELAY); + EventBits_t bits = xEventGroupGetBits(connectivity_event_group); - ESP_LOGV(TAG, "Connection established"); + if ((bits & (WIFI_CONNECTED_BIT | MQTT_CONNECTED_BIT)) == (WIFI_CONNECTED_BIT | MQTT_CONNECTED_BIT)) { - if (ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(MQTT_TASK_TIMEOUT_MS))) { - //i2c_do(global_rx_buffer); PLACEHOLDER FUNC! + 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)} + + } else { + ESP_LOGW(TAG, "Waiting for connection..."); + vTaskDelay(pdMS_TO_TICKS(MQTT_TASK_TIMEOUT_MS/2)); } ESP_ERROR_CHECK(esp_task_wdt_reset()); ESP_LOGV(TAG, "Task reset"); - } - ESP_LOGI(TAG, "Task stopped"); + } } diff --git a/firmware/esp32/main/wifi.cpp b/firmware/esp32/main/wifi.cpp index 48b9605..dfe46c0 100644 --- a/firmware/esp32/main/wifi.cpp +++ b/firmware/esp32/main/wifi.cpp @@ -6,27 +6,26 @@ #include "esp_log.h" #include "nvs_flash.h" #include "main.hpp" -#include "config.h" +#include "config-erick.h" -#define WIFI_TIMEOUT_TICKS pdMS_TO_TICKS(8000) +#define WIFI_TIMEOUT_MS 4500 static const char* TAG = "WIFI"; -EventGroupHandle_t wifi_event_group; - 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) { case WIFI_EVENT_STA_START: - ESP_LOGI(TAG, "Wi-Fi process started, connecting..."); - + ESP_LOGV(TAG, "Process started, connecting..."); esp_wifi_connect(); break; + case WIFI_EVENT_STA_DISCONNECTED: { wifi_event_sta_disconnected_t* disconn = (wifi_event_sta_disconnected_t*) event_data; ESP_LOGW(TAG, "Disconnected, reason: %d", disconn->reason); - xEventGroupClearBits(wifi_event_group, WIFI_CONNECTED_BIT); + xEventGroupClearBits(connectivity_event_group, WIFI_CONNECTED_BIT | MQTT_CONNECTED_BIT); + esp_wifi_connect(); break; } @@ -36,8 +35,9 @@ static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t e } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; - ESP_LOGI(TAG, "Got IP: " IPSTR, IP2STR(&event->ip_info.ip)); - xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_BIT); + ESP_LOGD(TAG, "Got IP: " IPSTR, IP2STR(&event->ip_info.ip)); + + xEventGroupSetBits(connectivity_event_group, WIFI_CONNECTED_BIT); } } @@ -49,10 +49,7 @@ static void wifi_init_sta(void) { ESP_ERROR_CHECK(nvs_flash_init()); } - wifi_event_group = xEventGroupCreate(); - ESP_ERROR_CHECK(esp_netif_init()); - ESP_ERROR_CHECK(esp_event_loop_create_default()); esp_netif_create_default_wifi_sta(); @@ -75,22 +72,14 @@ static void wifi_init_sta(void) { } void wifiTask(void *pvParameters) { - ESP_LOGI(TAG, "WiFi task started"); + ESP_LOGV(TAG, "Task started"); wifi_init_sta(); ESP_ERROR_CHECK(esp_task_wdt_add(NULL)); for(;;) { - wifi_ap_record_t ap; - if (esp_wifi_sta_get_ap_info(&ap) == ESP_OK) { - ESP_LOGV(TAG, "RSSI: %d", ap.rssi); - } - - //TODO: manage data send rate if with slow network - // or just let mqtt know (useful for making a notice on home manager) - ESP_ERROR_CHECK(esp_task_wdt_reset()); - ESP_LOGI(TAG, "Wi-Fi task reset"); - vTaskDelay(pdMS_TO_TICKS(WIFI_TIMEOUT_TICKS)); + ESP_LOGV(TAG, "Task reset"); + vTaskDelay(pdMS_TO_TICKS(WIFI_TIMEOUT_MS)); } } From 4cd9a3210c149903fd80302c397a627ad51d516d Mon Sep 17 00:00:00 2001 From: Erick Ahmed Date: Tue, 9 Dec 2025 10:01:54 +0100 Subject: [PATCH 21/32] Comment out I2C task handles --- firmware/esp32/main/mqtt.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/firmware/esp32/main/mqtt.cpp b/firmware/esp32/main/mqtt.cpp index 58eda80..c7cc4ad 100644 --- a/firmware/esp32/main/mqtt.cpp +++ b/firmware/esp32/main/mqtt.cpp @@ -12,7 +12,7 @@ static const char* TAG = "MQTT"; -static TaskHandle_t i2c_task_handle = NULL; +//static TaskHandle_t i2c_task_handle = NULL; static esp_mqtt_client_handle_t mqtt_client = NULL; static char global_rx_buffer[50]; @@ -46,7 +46,7 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ memcpy(global_rx_buffer, event->data, len); global_rx_buffer[len] = '\0'; - if (i2c_task_handle != NULL) xTaskNotifyGive(i2c_task_handle); + //if(i2c_task_handle != NULL) xTaskNotifyGive(i2c_task_handle); break; } @@ -73,7 +73,7 @@ void mqtt_publish(void) {} void mqttTask(void *pvParameters) { ESP_LOGV(TAG, "Task started"); - i2c_task_handle = xTaskGetCurrentTaskHandle(); + //i2c_task_handle = xTaskGetCurrentTaskHandle(); mqtt_init(); From 9bad1790a75aa72ac79822e8778cac8b86dbd1f6 Mon Sep 17 00:00:00 2001 From: Erick Ahmed Date: Tue, 9 Dec 2025 10:02:29 +0100 Subject: [PATCH 22/32] Log MQTT errors --- firmware/esp32/main/mqtt.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/firmware/esp32/main/mqtt.cpp b/firmware/esp32/main/mqtt.cpp index c7cc4ad..9a4a9f2 100644 --- a/firmware/esp32/main/mqtt.cpp +++ b/firmware/esp32/main/mqtt.cpp @@ -50,6 +50,12 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ break; } + case MQTT_EVENT_ERROR: + ESP_LOGE(TAG, "Error event"); + if(event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT) ESP_LOGE(TAG, "Network Error: %s", strerror(event->error_handle->esp_transport_sock_errno)); + else if(event->error_handle->error_type == MQTT_ERROR_TYPE_CONNECTION_REFUSED) ESP_LOGE(TAG, "Connection Refused! Reason code: 0x%x", event->error_handle->connect_return_code); + break; + default: break; } From c53783072e9ee723a957da02b988f52be5b72603 Mon Sep 17 00:00:00 2001 From: Erick Ahmed Date: Tue, 9 Dec 2025 10:02:47 +0100 Subject: [PATCH 23/32] Set MQTT username and password --- firmware/esp32/include/config.h | 5 +++++ firmware/esp32/main/mqtt.cpp | 2 ++ 2 files changed, 7 insertions(+) diff --git a/firmware/esp32/include/config.h b/firmware/esp32/include/config.h index e91de90..43de798 100644 --- a/firmware/esp32/include/config.h +++ b/firmware/esp32/include/config.h @@ -3,10 +3,15 @@ // TODO: make this a config.h +// WI-FI #define SSID "your_wifi_ssid" #define PASSWORD "your_wifi_password" #define AUTH_MODE WIFI_AUTH_WPA2_PSK + +// MQTT #define MQTT_ADDR "mqtt://yourmqttserver" #define MQTT_PORT 1883 +#define MQTT_USER "your_username" +#define MQTT_PASS "your_password" #endif diff --git a/firmware/esp32/main/mqtt.cpp b/firmware/esp32/main/mqtt.cpp index 9a4a9f2..305db1c 100644 --- a/firmware/esp32/main/mqtt.cpp +++ b/firmware/esp32/main/mqtt.cpp @@ -66,6 +66,8 @@ static void mqtt_init(void) { mqtt_cfg.broker.address.uri = MQTT_ADDR; mqtt_cfg.broker.address.port = MQTT_PORT; + mqtt_cfg.credentials.username = MQTT_USER; + mqtt_cfg.credentials.authentication.password = MQTT_PASS; mqtt_client = esp_mqtt_client_init(&mqtt_cfg); From 4e78a5f2edbb36bdbb3baa5bd3ebc425c5bd83b7 Mon Sep 17 00:00:00 2001 From: Erick Ahmed Date: Tue, 9 Dec 2025 10:03:06 +0100 Subject: [PATCH 24/32] Go in critical error if MQTT is not being initialized --- firmware/esp32/main/mqtt.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/firmware/esp32/main/mqtt.cpp b/firmware/esp32/main/mqtt.cpp index 305db1c..8c92246 100644 --- a/firmware/esp32/main/mqtt.cpp +++ b/firmware/esp32/main/mqtt.cpp @@ -70,6 +70,10 @@ static void mqtt_init(void) { mqtt_cfg.credentials.authentication.password = MQTT_PASS; mqtt_client = esp_mqtt_client_init(&mqtt_cfg); + if(mqtt_client == NULL) { + ESP_LOGE(TAG, "esp_mqtt_client_init returned NULL"); + criticalErrorFlag = true; + } esp_mqtt_client_register_event(mqtt_client, (esp_mqtt_event_id_t)ESP_EVENT_ANY_ID, mqtt_event_handler, NULL); From eaad4469bc873ca3e534bfdbc9f03e6657e5f9cb Mon Sep 17 00:00:00 2001 From: Erick Ahmed Date: Tue, 9 Dec 2025 10:04:06 +0100 Subject: [PATCH 25/32] Normalize spacing in ESP32 firmware --- firmware/esp32/main/main.cpp | 2 +- firmware/esp32/main/mqtt.cpp | 12 +++++------- firmware/esp32/main/wifi.cpp | 12 ++++++------ 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/firmware/esp32/main/main.cpp b/firmware/esp32/main/main.cpp index a12f469..d0a2489 100644 --- a/firmware/esp32/main/main.cpp +++ b/firmware/esp32/main/main.cpp @@ -28,7 +28,7 @@ static void watchdogTask(void *pvParameters) { ESP_ERROR_CHECK(esp_task_wdt_add(NULL)); for(;;) { - if (criticalErrorFlag) { + if(criticalErrorFlag) { ESP_LOGE(TAG, "Critical error flag raised. Rebooting."); esp_restart(); } diff --git a/firmware/esp32/main/mqtt.cpp b/firmware/esp32/main/mqtt.cpp index 8c92246..8f75cbd 100644 --- a/firmware/esp32/main/mqtt.cpp +++ b/firmware/esp32/main/mqtt.cpp @@ -22,9 +22,9 @@ static void mqtt_subscribe(void) { } 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 = (esp_mqtt_event_handle_t)event_data; + esp_mqtt_event_handle_t event =(esp_mqtt_event_handle_t)event_data; - switch (event->event_id) { + switch(event->event_id) { case MQTT_EVENT_CONNECTED: xEventGroupSetBits(connectivity_event_group, MQTT_CONNECTED_BIT); mqtt_subscribe(); @@ -41,7 +41,7 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ ESP_LOGD(TAG, "Data received"); int len = event->data_len; - if (len > sizeof(global_rx_buffer) - 1) len = sizeof(global_rx_buffer) - 1; + if(len > sizeof(global_rx_buffer) - 1) len = sizeof(global_rx_buffer) - 1; memcpy(global_rx_buffer, event->data, len); global_rx_buffer[len] = '\0'; @@ -96,14 +96,12 @@ void mqttTask(void *pvParameters) { EventBits_t bits = xEventGroupGetBits(connectivity_event_group); - if ((bits & (WIFI_CONNECTED_BIT | MQTT_CONNECTED_BIT)) == (WIFI_CONNECTED_BIT | MQTT_CONNECTED_BIT)) { - + 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)} - + if(task_notification) {} //{twi_do(global_rx_buffer)} } else { ESP_LOGW(TAG, "Waiting for connection..."); vTaskDelay(pdMS_TO_TICKS(MQTT_TASK_TIMEOUT_MS/2)); diff --git a/firmware/esp32/main/wifi.cpp b/firmware/esp32/main/wifi.cpp index dfe46c0..88c903d 100644 --- a/firmware/esp32/main/wifi.cpp +++ b/firmware/esp32/main/wifi.cpp @@ -13,15 +13,15 @@ static const char* TAG = "WIFI"; 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) { + if(event_base == WIFI_EVENT) { + switch(event_id) { case WIFI_EVENT_STA_START: ESP_LOGV(TAG, "Process started, connecting..."); esp_wifi_connect(); break; case WIFI_EVENT_STA_DISCONNECTED: { - wifi_event_sta_disconnected_t* disconn = (wifi_event_sta_disconnected_t*) event_data; + wifi_event_sta_disconnected_t* disconn =(wifi_event_sta_disconnected_t*) event_data; ESP_LOGW(TAG, "Disconnected, reason: %d", disconn->reason); xEventGroupClearBits(connectivity_event_group, WIFI_CONNECTED_BIT | MQTT_CONNECTED_BIT); @@ -33,8 +33,8 @@ static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t e break; } } - else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { - ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; + else if(event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { + ip_event_got_ip_t* event =(ip_event_got_ip_t*) event_data; ESP_LOGD(TAG, "Got IP: " IPSTR, IP2STR(&event->ip_info.ip)); xEventGroupSetBits(connectivity_event_group, WIFI_CONNECTED_BIT); @@ -44,7 +44,7 @@ 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(); - if (nvs_err == ESP_ERR_NVS_NO_FREE_PAGES || nvs_err == ESP_ERR_NVS_NEW_VERSION_FOUND) { + if(nvs_err == ESP_ERR_NVS_NO_FREE_PAGES || nvs_err == ESP_ERR_NVS_NEW_VERSION_FOUND) { ESP_ERROR_CHECK(nvs_flash_erase()); ESP_ERROR_CHECK(nvs_flash_init()); } From aa395c381bd8f31e031fb3704267f54273d2d308 Mon Sep 17 00:00:00 2001 From: Erick Ahmed Date: Tue, 9 Dec 2025 10:49:32 +0100 Subject: [PATCH 26/32] Remove redundant log --- firmware/esp32/main/mqtt.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/firmware/esp32/main/mqtt.cpp b/firmware/esp32/main/mqtt.cpp index 8f75cbd..8a501e2 100644 --- a/firmware/esp32/main/mqtt.cpp +++ b/firmware/esp32/main/mqtt.cpp @@ -92,8 +92,6 @@ void mqttTask(void *pvParameters) { ESP_ERROR_CHECK(esp_task_wdt_add(NULL)); for(;;) { - ESP_LOGV(TAG, "Checking broker connection..."); - EventBits_t bits = xEventGroupGetBits(connectivity_event_group); if((bits &(WIFI_CONNECTED_BIT | MQTT_CONNECTED_BIT)) ==(WIFI_CONNECTED_BIT | MQTT_CONNECTED_BIT)) { From 6a2a933002befe30eae8261e873daa39632bdfde Mon Sep 17 00:00:00 2001 From: Erick Ahmed Date: Tue, 9 Dec 2025 23:01:29 +0100 Subject: [PATCH 27/32] Better wait implementation with no spinlock from MQTT task --- firmware/esp32/main/mqtt.cpp | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/firmware/esp32/main/mqtt.cpp b/firmware/esp32/main/mqtt.cpp index 8a501e2..b7d04de 100644 --- a/firmware/esp32/main/mqtt.cpp +++ b/firmware/esp32/main/mqtt.cpp @@ -92,18 +92,14 @@ void mqttTask(void *pvParameters) { ESP_ERROR_CHECK(esp_task_wdt_add(NULL)); for(;;) { - EventBits_t bits = xEventGroupGetBits(connectivity_event_group); + ESP_LOGV(TAG, "Checking connection..."); + 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 present"); - uint32_t task_notification = ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(MQTT_TASK_TIMEOUT_MS)); + uint32_t task_notification = ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(MQTT_TASK_TIMEOUT_MS)); - if(task_notification) {} //{twi_do(global_rx_buffer)} - } else { - ESP_LOGW(TAG, "Waiting for connection..."); - vTaskDelay(pdMS_TO_TICKS(MQTT_TASK_TIMEOUT_MS/2)); - } + if(task_notification) {} //{twi_do(global_rx_buffer)} ESP_ERROR_CHECK(esp_task_wdt_reset()); ESP_LOGV(TAG, "Task reset"); From 2e6e82982993c3383cf65c3c83e51f9609b5bf45 Mon Sep 17 00:00:00 2001 From: Erick Ahmed Date: Tue, 9 Dec 2025 23:22:35 +0100 Subject: [PATCH 28/32] Only clear Wi-Fi bit --- firmware/esp32/main/wifi.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firmware/esp32/main/wifi.cpp b/firmware/esp32/main/wifi.cpp index 88c903d..fc8311f 100644 --- a/firmware/esp32/main/wifi.cpp +++ b/firmware/esp32/main/wifi.cpp @@ -24,7 +24,7 @@ static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t e wifi_event_sta_disconnected_t* disconn =(wifi_event_sta_disconnected_t*) event_data; ESP_LOGW(TAG, "Disconnected, reason: %d", disconn->reason); - xEventGroupClearBits(connectivity_event_group, WIFI_CONNECTED_BIT | MQTT_CONNECTED_BIT); + xEventGroupClearBits(connectivity_event_group, WIFI_CONNECTED_BIT); esp_wifi_connect(); break; From b1a6dfddd3abff3311f7857502add1c06ceb0884 Mon Sep 17 00:00:00 2001 From: Erick Ahmed Date: Tue, 9 Dec 2025 23:23:10 +0100 Subject: [PATCH 29/32] Wait for Wi-Fi connection before initializing MQTT --- firmware/esp32/main/mqtt.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/firmware/esp32/main/mqtt.cpp b/firmware/esp32/main/mqtt.cpp index b7d04de..f88ace2 100644 --- a/firmware/esp32/main/mqtt.cpp +++ b/firmware/esp32/main/mqtt.cpp @@ -85,16 +85,16 @@ void mqtt_publish(void) {} void mqttTask(void *pvParameters) { ESP_LOGV(TAG, "Task started"); - //i2c_task_handle = xTaskGetCurrentTaskHandle(); + ESP_LOGV(TAG, "Waiting for Wi-Fi before initializing server"); + xEventGroupWaitBits(connectivity_event_group, WIFI_CONNECTED_BIT, pdFALSE, pdTRUE, portMAX_DELAY); + ESP_LOGV(TAG, "Server initialized"); mqtt_init(); - ESP_ERROR_CHECK(esp_task_wdt_add(NULL)); for(;;) { ESP_LOGV(TAG, "Checking connection..."); xEventGroupWaitBits(connectivity_event_group, WIFI_CONNECTED_BIT | MQTT_CONNECTED_BIT, pdFALSE, pdTRUE, portMAX_DELAY); - ESP_LOGV(TAG, "Connection present"); uint32_t task_notification = ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(MQTT_TASK_TIMEOUT_MS)); From 06a672d1f7b8a766d80f6df54ef197fff4c969a8 Mon Sep 17 00:00:00 2001 From: Erick Ahmed Date: Tue, 9 Dec 2025 23:40:53 +0100 Subject: [PATCH 30/32] Remove old code about I2C --- firmware/esp32/main/mqtt.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/firmware/esp32/main/mqtt.cpp b/firmware/esp32/main/mqtt.cpp index f88ace2..423dae7 100644 --- a/firmware/esp32/main/mqtt.cpp +++ b/firmware/esp32/main/mqtt.cpp @@ -45,8 +45,6 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ memcpy(global_rx_buffer, event->data, len); global_rx_buffer[len] = '\0'; - - //if(i2c_task_handle != NULL) xTaskNotifyGive(i2c_task_handle); break; } From 1a893547b680a13ccc5f56c3770aed10ef3fd1ab Mon Sep 17 00:00:00 2001 From: Erick Ahmed Date: Tue, 9 Dec 2025 23:50:26 +0100 Subject: [PATCH 31/32] Finalize event groups to improve performance --- firmware/esp32/main/mqtt.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/firmware/esp32/main/mqtt.cpp b/firmware/esp32/main/mqtt.cpp index 423dae7..6af5268 100644 --- a/firmware/esp32/main/mqtt.cpp +++ b/firmware/esp32/main/mqtt.cpp @@ -91,13 +91,15 @@ void mqttTask(void *pvParameters) { ESP_ERROR_CHECK(esp_task_wdt_add(NULL)); for(;;) { - ESP_LOGV(TAG, "Checking connection..."); - xEventGroupWaitBits(connectivity_event_group, WIFI_CONNECTED_BIT | MQTT_CONNECTED_BIT, pdFALSE, pdTRUE, portMAX_DELAY); - ESP_LOGV(TAG, "Connection present"); + ESP_LOGV(TAG, "Checking Wi-Fi connection..."); + EventBits_t bits = xEventGroupWaitBits(connectivity_event_group, WIFI_CONNECTED_BIT | MQTT_CONNECTED_BIT, pdFALSE, pdTRUE, pdMS_TO_TICKS(MQTT_TASK_TIMEOUT_MS)); - uint32_t task_notification = ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(MQTT_TASK_TIMEOUT_MS)); + if ((bits & (WIFI_CONNECTED_BIT | MQTT_CONNECTED_BIT)) == (WIFI_CONNECTED_BIT | MQTT_CONNECTED_BIT)) { + ESP_LOGV(TAG, "Wi-Fi and MQTT up"); - if(task_notification) {} //{twi_do(global_rx_buffer)} + + + } else ESP_LOGV(TAG, "Wi-Fi or MQTT down"); ESP_ERROR_CHECK(esp_task_wdt_reset()); ESP_LOGV(TAG, "Task reset"); From 626bbe9dbb728753322591453f122fa5cfa80d5b Mon Sep 17 00:00:00 2001 From: Erick Ahmed Date: Wed, 10 Dec 2025 00:27:30 +0100 Subject: [PATCH 32/32] Remove task handle: managed in dev-twi main.cpp --- firmware/esp32/main/mqtt.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/firmware/esp32/main/mqtt.cpp b/firmware/esp32/main/mqtt.cpp index 6af5268..a9bd99b 100644 --- a/firmware/esp32/main/mqtt.cpp +++ b/firmware/esp32/main/mqtt.cpp @@ -12,7 +12,6 @@ static const char* TAG = "MQTT"; -//static TaskHandle_t i2c_task_handle = NULL; static esp_mqtt_client_handle_t mqtt_client = NULL; static char global_rx_buffer[50];