From 4807e1cfe11dbedd7656e534e3995edb575129a6 Mon Sep 17 00:00:00 2001 From: Filip Wandzio Date: Tue, 14 Oct 2025 22:18:55 +0200 Subject: Optimize mqtt rtt analysis method --- analysis/e1anl/src/agregate.py | 2 +- firmware/include/config.h | 7 +-- firmware/src/main.c | 108 ++++++++++++++++++++++++++++++++--------- firmware/src/mqtt.c | 67 ++++++++++++------------- firmware/src/wifi.c | 1 + firmware/src/wifi_scan.c | 59 +++++++++++----------- 6 files changed, 154 insertions(+), 90 deletions(-) diff --git a/analysis/e1anl/src/agregate.py b/analysis/e1anl/src/agregate.py index 1054288..05537d3 100644 --- a/analysis/e1anl/src/agregate.py +++ b/analysis/e1anl/src/agregate.py @@ -8,7 +8,7 @@ from collections import deque import os os.makedirs("output", exist_ok=True) -BROKER = "192.168.1.101" +BROKER = "192.168.1.103" SUB_TOPIC = "device/echo/in" CSV_FILE = "output/rtt_throughput_log.csv" WINDOW_SEC = 1 diff --git a/firmware/include/config.h b/firmware/include/config.h index 467e1f7..9672a38 100644 --- a/firmware/include/config.h +++ b/firmware/include/config.h @@ -1,8 +1,9 @@ #ifndef CONFIG_H #define CONFIG_H -#define PUB_TOPIC "device/echo/in" -#define SUB_TOPIC "device/echo/out" +#define SUB_TOPIC "device/echo/in" +#define PUB_TOPIC "device/echo/out" + #define PUBLISH_INTERVAL_MS 1000 #define BUFFER_SIZE 32 @@ -15,7 +16,7 @@ #endif #ifndef MQTT_URI -#define MQTT_URI "mqtt://0.0.0.0" +#define MQTT_URI "mqtt://192.168.1.103" #endif #endif diff --git a/firmware/src/main.c b/firmware/src/main.c index 790959d..3f186f8 100644 --- a/firmware/src/main.c +++ b/firmware/src/main.c @@ -7,54 +7,120 @@ #include "mqtt.h" #include "wifi.h" +#include static const char *TAG = "app"; /** - * @brief Initialize NVS flash storage, handling full or incompatible pages. + * @brief Initialize NVS (Non-Volatile Storage) flash storage, handling full or + * incompatible pages. * - * @return esp_err_t ESP_OK on success, error code otherwise. + * This function checks the NVS flash storage to ensure that there is enough + * space and that the version of NVS is compatible. If the partition is full or + * incompatible, it will erase the NVS partition and attempt initialization + * again. + * + * @return esp_err_t + * - ESP_OK: Initialization successful. + * - Other error codes if initialization or erasure fails. */ static esp_err_t nvs_flash_init_check(void) { esp_err_t ret = nvs_flash_init(); + if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { ESP_LOGW(TAG, - "NVS partition was full or incompatible, erasing..."); - ESP_ERROR_CHECK(nvs_flash_erase()); + "NVS partition is full or incompatible, erasing..."); + ret = nvs_flash_erase(); + + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Failed to erase NVS partition (%s)", + esp_err_to_name(ret)); + return ret; + } ret = nvs_flash_init(); } + if (ret != ESP_OK) ESP_LOGE(TAG, "Failed to initialize NVS (%s)", esp_err_to_name(ret)); + return ret; } /** - * @brief Application entry point. + * @brief General helper function to initialize a given component and log + * errors. * - * Initializes NVS, network interface, event loop, - * WiFi station, and MQTT client with proper error handling and logging. + * This function takes an initialization function and a component name, attempts + * to initialize the component, and logs an error if initialization fails. + * + * @param init_func A function pointer to the initialization function of the + * component. + * @param component_name A string describing the name of the component being + * initialized. + * + * @return esp_err_t + * - ESP_OK: If initialization is successful. + * - Other error codes if initialization fails. */ -void app_main(void) +static esp_err_t init_component(esp_err_t (*init_func)(void), + const char *component_name) { - esp_err_t ret = nvs_flash_init_check(); - if (ret != ESP_OK) { - ESP_LOGE(TAG, "NVS init failed, aborting app start"); - return; - } + esp_err_t ret = init_func(); - ret = esp_netif_init(); if (ret != ESP_OK) { - ESP_LOGE(TAG, "Failed to initialize network interface (%s)", + ESP_LOGE(TAG, "Failed to initialize %s (%s)", component_name, esp_err_to_name(ret)); - return; + return ret; } - ret = esp_event_loop_create_default(); - if (ret != ESP_OK) { - ESP_LOGE(TAG, "Failed to create event loop (%s)", - esp_err_to_name(ret)); + return ESP_OK; +} + +/** + * @brief Initializes all the essential components for the application. + * + * This function initializes the NVS storage, network interface, and event loop + * in sequence. If any of these components fail to initialize, the function + * returns an error, and the application will stop early. + * + * @return esp_err_t + * - ESP_OK: If all components are successfully initialized. + * - ESP_FAIL: If any component fails to initialize. + */ +esp_err_t init_app_components(void) +{ + if (init_component(nvs_flash_init_check, "NVS") != ESP_OK) { + ESP_LOGE(TAG, "NVS initialization failed, aborting app start."); + return ESP_FAIL; + } + + if (init_component(esp_netif_init, "network interface") != ESP_OK) + return ESP_FAIL; + + if (init_component(esp_event_loop_create_default, "event loop") != + ESP_OK) + return ESP_FAIL; + + return ESP_OK; +} + +/** + * @brief Main entry point of the application. + * + * This function is responsible for initializing the core components of the + * application, including the NVS storage, network interface, and event loop. If + * any initialization step fails, the application will log the error and stop. + * + * After initialization, this function will configure the WiFi station and start + * the MQTT client. + */ + +void app_main(void) +{ + if (init_app_components() != ESP_OK) { + ESP_LOGE(TAG, "App initialization failed, aborting."); return; } @@ -63,6 +129,4 @@ void app_main(void) ESP_LOGI(TAG, "Starting MQTT client"); mqtt_app_start(); - - ESP_LOGI(TAG, "Application setup completed"); } diff --git a/firmware/src/mqtt.c b/firmware/src/mqtt.c index 90451bc..b171437 100644 --- a/firmware/src/mqtt.c +++ b/firmware/src/mqtt.c @@ -8,24 +8,12 @@ #include #include -#ifndef BUFFER_SIZE -#define BUFFER_SIZE 64 -#endif - -#ifndef PUBLISH_INTERVAL_MS -#define PUBLISH_INTERVAL_MS 1000 -#endif - -#ifndef PUBLISHER_TASK_STACK_SIZE -#define PUBLISHER_TASK_STACK_SIZE 4096 -#endif - -#ifndef PUBLISHER_TASK_PRIORITY -#define PUBLISHER_TASK_PRIORITY 5 -#endif - +static const char *TAG = "mqtt"; static esp_mqtt_client_handle_t client; +/** + * @brief Obsługa zdarzeń MQTT + */ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) { @@ -33,56 +21,63 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, switch (event->event_id) { case MQTT_EVENT_CONNECTED: - printf("MQTT connected!\n"); + ESP_LOGI(TAG, "MQTT connected!"); esp_mqtt_client_subscribe(event->client, SUB_TOPIC, 1); break; case MQTT_EVENT_DATA: { char topic[event->topic_len + 1]; char data[event->data_len + 1]; + memcpy(topic, event->topic, event->topic_len); topic[event->topic_len] = '\0'; memcpy(data, event->data, event->data_len); data[event->data_len] = '\0'; - printf("MQTT Msg received\nTopic: %s\nData: %s\n", topic, data); + ESP_LOGI(TAG, "MQTT Msg received | Topic: %s | Data: %s", topic, + data); + // Echo wiadomości do PUB_TOPIC tylko jeśli timestamp jest + // sensowny char *endptr; - long long sent = strtoll(data, &endptr, 10); - if (endptr != data) { - long long now = esp_timer_get_time() / 1000ULL; - long long rtt = now - sent; - printf("RTT: %lld ms\n", rtt); + long long ts = strtoll(data, &endptr, 10); + if (endptr != data && *endptr == '\0' && ts > 0 && ts < 1e13) { + esp_mqtt_client_publish(event->client, PUB_TOPIC, data, + 0, 1, 0); + } else { + ESP_LOGW(TAG, "Nieprawidłowe dane, ignorowane: %s", + data); } + break; } - default: + case MQTT_EVENT_DISCONNECTED: + ESP_LOGW(TAG, "MQTT disconnected"); break; - } -} -static void publisher_task(void *pvParameters) -{ - while (1) { - char buf[BUFFER_SIZE]; - snprintf(buf, BUFFER_SIZE, "%lld", - (long long)(esp_timer_get_time() / 1000ULL)); - esp_mqtt_client_publish(client, PUB_TOPIC, buf, 0, 1, 0); - vTaskDelay(pdMS_TO_TICKS(PUBLISH_INTERVAL_MS)); + case MQTT_EVENT_ERROR: + ESP_LOGE(TAG, "MQTT error occurred"); + break; + + default: + break; } } +/** + * @brief Uruchamia klienta MQTT + */ void mqtt_app_start(void) { esp_mqtt_client_config_t mqtt_cfg = { .broker.address.uri = MQTT_URI, }; + client = esp_mqtt_client_init(&mqtt_cfg); esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL); esp_mqtt_client_start(client); - xTaskCreate(publisher_task, "publisher_task", PUBLISHER_TASK_STACK_SIZE, - NULL, PUBLISHER_TASK_PRIORITY, NULL); + ESP_LOGI(TAG, "MQTT client started"); } diff --git a/firmware/src/wifi.c b/firmware/src/wifi.c index ea76f24..704b4a4 100644 --- a/firmware/src/wifi.c +++ b/firmware/src/wifi.c @@ -24,6 +24,7 @@ * * The SSID and password are defined in the configuration headers. */ +// cppcheck-suppress unusedFunction void wifi_init_sta(void) { esp_netif_create_default_wifi_sta(); diff --git a/firmware/src/wifi_scan.c b/firmware/src/wifi_scan.c index e588cfc..02bb451 100644 --- a/firmware/src/wifi_scan.c +++ b/firmware/src/wifi_scan.c @@ -19,35 +19,36 @@ * * @param pvParameter Task parameter (unused). */ -static void wifi_scan_task(void *pvParameter) { - uint16_t ap_count = WIFI_SCAN_MAX_APS; - static wifi_ap_record_t ap_info[WIFI_SCAN_MAX_APS]; - wifi_scan_config_t scan_config = { - .ssid = NULL, - .bssid = NULL, - .channel = WIFI_SCAN_DEFAULT_CHANNEL, - .show_hidden = WIFI_SCAN_SHOW_HIDDEN, - }; +static void wifi_scan_task(void *pvParameter) +{ + uint16_t ap_count = WIFI_SCAN_MAX_APS; + static wifi_ap_record_t ap_info[WIFI_SCAN_MAX_APS]; + wifi_scan_config_t scan_config = { + .ssid = NULL, + .bssid = NULL, + .channel = WIFI_SCAN_DEFAULT_CHANNEL, + .show_hidden = WIFI_SCAN_SHOW_HIDDEN, + }; - ESP_LOGI("wifi_scan", "Starting Wi-Fi scan..."); - ESP_ERROR_CHECK(esp_wifi_scan_start(&scan_config, false)); + ESP_LOGI("wifi_scan", "Starting Wi-Fi scan..."); + ESP_ERROR_CHECK(esp_wifi_scan_start(&scan_config, false)); - while (true) { - uint16_t finished_ap_count = 0; - ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&finished_ap_count)); - if (finished_ap_count > 0) - break; - vTaskDelay(pdMS_TO_TICKS(WIFI_SCAN_POLL_INTERVAL_MS)); - } + while (true) { + uint16_t finished_ap_count = 0; + ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&finished_ap_count)); + if (finished_ap_count > 0) + break; + vTaskDelay(pdMS_TO_TICKS(WIFI_SCAN_POLL_INTERVAL_MS)); + } - ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&ap_count, ap_info)); - ESP_LOGI("wifi_scan", "Found %d access points:", ap_count); - for (uint16_t ap_index = 0; ap_index < ap_count; ++ap_index) { - ESP_LOGI("wifi_scan", "%d: SSID: %s, RSSI: %d", ap_index + 1, - ap_info[ap_index].ssid, ap_info[ap_index].rssi); - } + ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&ap_count, ap_info)); + ESP_LOGI("wifi_scan", "Found %d access points:", ap_count); + for (uint16_t ap_index = 0; ap_index < ap_count; ++ap_index) { + ESP_LOGI("wifi_scan", "%d: SSID: %s, RSSI: %d", ap_index + 1, + ap_info[ap_index].ssid, ap_info[ap_index].rssi); + } - vTaskDelete(NULL); + vTaskDelete(NULL); } /** @@ -55,7 +56,9 @@ static void wifi_scan_task(void *pvParameter) { * * Creates a FreeRTOS task that runs the WiFi scan asynchronously. */ -void wifi_scan_start(void) { - xTaskCreate(wifi_scan_task, WIFI_SCAN_TASK_NAME, WIFI_SCAN_TASK_STACK_SIZE, - NULL, WIFI_SCAN_TASK_PRIORITY, NULL); +void wifi_scan_start(void) +{ + xTaskCreate(wifi_scan_task, WIFI_SCAN_TASK_NAME, + WIFI_SCAN_TASK_STACK_SIZE, NULL, WIFI_SCAN_TASK_PRIORITY, + NULL); } -- cgit v1.2.3