diff options
| author | Filip Wandzio <contact@philw.dev> | 2025-10-29 13:29:27 +0100 |
|---|---|---|
| committer | Filip Wandzio <contact@philw.dev> | 2025-10-29 13:29:27 +0100 |
| commit | a27af14e815ef993e9a0c94e5bc8b32dbf989a94 (patch) | |
| tree | fed25911f251f95c7f2c71d1d770a5013452664c /firmware | |
| parent | 4698910f842c322b80ebd1cf19a4e32c0fb2249d (diff) | |
| download | e1-master.tar.gz e1-master.zip | |
Solve baud detection bug, fix wifi searching task scheduling bug, improve documentation, implement additional mqtt topicsHEADmaster
Diffstat (limited to '')
| -rw-r--r-- | firmware/.baud_detected | 1 | ||||
| -rw-r--r-- | firmware/Makefile | 30 | ||||
| -rw-r--r-- | firmware/platformio.ini | 2 | ||||
| -rw-r--r-- | firmware/src/main.c | 152 | ||||
| -rw-r--r-- | firmware/src/mqtt.c | 28 | ||||
| -rw-r--r-- | firmware/src/wifi_scan.c | 19 |
6 files changed, 142 insertions, 90 deletions
diff --git a/firmware/.baud_detected b/firmware/.baud_detected new file mode 100644 index 0000000..be25b30 --- /dev/null +++ b/firmware/.baud_detected | |||
| @@ -0,0 +1 @@ | |||
| 115200 | |||
diff --git a/firmware/Makefile b/firmware/Makefile index 0fd1699..0595792 100644 --- a/firmware/Makefile +++ b/firmware/Makefile | |||
| @@ -9,7 +9,29 @@ BUILD_FLAGS = -D WIFI_SSID="\\\"$(WIFI_SSID)\\\"" \ | |||
| 9 | -D WIFI_PASS="\\\"$(WIFI_PASS)\\\"" \ | 9 | -D WIFI_PASS="\\\"$(WIFI_PASS)\\\"" \ |
| 10 | -D MQTT_URI="\\\"$(MQTT_URI)\\\"" | 10 | -D MQTT_URI="\\\"$(MQTT_URI)\\\"" |
| 11 | 11 | ||
| 12 | .PHONY: build upload monitor clean debug-flags test | 12 | PORT ?= $(shell ls /dev/ttyUSB* /dev/ttyACM* 2>/dev/null | head -n 1) |
| 13 | |||
| 14 | ifdef BAUD | ||
| 15 | BAUD := $(BAUD) | ||
| 16 | else ifneq ("$(wildcard .baud_detected)","") | ||
| 17 | BAUD := $(shell cat .baud_detected) | ||
| 18 | else | ||
| 19 | BAUD := 115200 | ||
| 20 | endif | ||
| 21 | |||
| 22 | detect-baud: | ||
| 23 | @echo "[*] Detecting correct baud rate..." | ||
| 24 | @if screen -L -dmS test_screen $(PORT) 74880; then \ | ||
| 25 | sleep 1; \ | ||
| 26 | grep -q "rst:" screenlog.0 && echo "74880" > .baud_detected || echo "115200" > .baud_detected; \ | ||
| 27 | rm -f screenlog.0; \ | ||
| 28 | screen -S test_screen -X quit; \ | ||
| 29 | else \ | ||
| 30 | echo "115200" > .baud_detected; \ | ||
| 31 | fi | ||
| 32 | @echo "[✓] Set BAUD=$$(cat .baud_detected)" | ||
| 33 | |||
| 34 | .PHONY: build upload monitor clean debug-flags test detect-baud | ||
| 13 | 35 | ||
| 14 | build: | 36 | build: |
| 15 | @echo "Starting build with SSID=$(WIFI_SSID)" | 37 | @echo "Starting build with SSID=$(WIFI_SSID)" |
| @@ -20,7 +42,11 @@ upload: | |||
| 20 | PLATFORMIO_BUILD_FLAGS="$(BUILD_FLAGS)" pio run -e $(ENV) -t upload | 42 | PLATFORMIO_BUILD_FLAGS="$(BUILD_FLAGS)" pio run -e $(ENV) -t upload |
| 21 | 43 | ||
| 22 | monitor: | 44 | monitor: |
| 23 | pio device monitor -e $(ENV) | 45 | @PORT=$(PORT); \ |
| 46 | BAUD=$(BAUD); \ | ||
| 47 | if [ -z "$$PORT" ]; then echo "[ERROR] Port /dev/ttyUSB* or /dev/ttyACM* not found"; exit 1; fi; \ | ||
| 48 | echo "[*] Opening monitor: $$PORT @ $$BAUD baud"; \ | ||
| 49 | screen $$PORT $$BAUD | ||
| 24 | 50 | ||
| 25 | clean: | 51 | clean: |
| 26 | pio run -e $(ENV) -t clean | 52 | pio run -e $(ENV) -t clean |
diff --git a/firmware/platformio.ini b/firmware/platformio.ini index 4f199c3..b106ca7 100644 --- a/firmware/platformio.ini +++ b/firmware/platformio.ini | |||
| @@ -5,4 +5,4 @@ framework = espidf | |||
| 5 | monitor_speed = 115200 | 5 | monitor_speed = 115200 |
| 6 | platform_packages = | 6 | platform_packages = |
| 7 | framework-espidf@3.50500.0 | 7 | framework-espidf@3.50500.0 |
| 8 | ; board_build.flash_size = 2MB | 8 | board_build.flash_size = 2MB |
diff --git a/firmware/src/main.c b/firmware/src/main.c index 3f186f8..9fa4c61 100644 --- a/firmware/src/main.c +++ b/firmware/src/main.c | |||
| @@ -1,5 +1,7 @@ | |||
| 1 | #include "esp_event.h" | 1 | #include "esp_event.h" |
| 2 | #include "esp_log.h" | 2 | #include "esp_log.h" |
| 3 | #include "esp_netif.h" | ||
| 4 | #include "esp_sntp.h" | ||
| 3 | #include "esp_system.h" | 5 | #include "esp_system.h" |
| 4 | #include "esp_timer.h" | 6 | #include "esp_timer.h" |
| 5 | #include "nvs_flash.h" | 7 | #include "nvs_flash.h" |
| @@ -8,21 +10,26 @@ | |||
| 8 | #include "wifi.h" | 10 | #include "wifi.h" |
| 9 | 11 | ||
| 10 | #include <stdio.h> | 12 | #include <stdio.h> |
| 13 | #include <time.h> | ||
| 14 | |||
| 11 | static const char *TAG = "app"; | 15 | static const char *TAG = "app"; |
| 16 | static EventGroupHandle_t wifi_event_group; | ||
| 17 | #define WIFI_CONNECTED_BIT BIT0 | ||
| 12 | 18 | ||
| 13 | /** | 19 | /** |
| 14 | * @brief Initialize NVS (Non-Volatile Storage) flash storage, handling full or | 20 | * @brief Initialize NVS (Non-Volatile Storage) flash storage, handling full or |
| 15 | * incompatible pages. | 21 | * incompatible pages. |
| 16 | * | 22 | * |
| 17 | * This function checks the NVS flash storage to ensure that there is enough | 23 | * This function checks the NVS flash storage to ensure that there is enough |
| 18 | * space and that the version of NVS is compatible. If the partition is full or | 24 | * space and that the version of NVS is compatible. If the partition |
| 19 | * incompatible, it will erase the NVS partition and attempt initialization | 25 | is full or |
| 20 | * again. | 26 | * incompatible, it will erase the NVS partition and attempt initialization |
| 21 | * | 27 | * again. |
| 22 | * @return esp_err_t | 28 | * |
| 23 | * - ESP_OK: Initialization successful. | 29 | * @return esp_err_t |
| 24 | * - Other error codes if initialization or erasure fails. | 30 | * - ESP_OK: Initialization successful. |
| 25 | */ | 31 | * - Other error codes if initialization or erasure fails. |
| 32 | */ | ||
| 26 | static esp_err_t nvs_flash_init_check(void) | 33 | static esp_err_t nvs_flash_init_check(void) |
| 27 | { | 34 | { |
| 28 | esp_err_t ret = nvs_flash_init(); | 35 | esp_err_t ret = nvs_flash_init(); |
| @@ -31,13 +38,7 @@ static esp_err_t nvs_flash_init_check(void) | |||
| 31 | ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { | 38 | ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { |
| 32 | ESP_LOGW(TAG, | 39 | ESP_LOGW(TAG, |
| 33 | "NVS partition is full or incompatible, erasing..."); | 40 | "NVS partition is full or incompatible, erasing..."); |
| 34 | ret = nvs_flash_erase(); | 41 | ESP_ERROR_CHECK(nvs_flash_erase()); |
| 35 | |||
| 36 | if (ret != ESP_OK) { | ||
| 37 | ESP_LOGE(TAG, "Failed to erase NVS partition (%s)", | ||
| 38 | esp_err_to_name(ret)); | ||
| 39 | return ret; | ||
| 40 | } | ||
| 41 | ret = nvs_flash_init(); | 42 | ret = nvs_flash_init(); |
| 42 | } | 43 | } |
| 43 | 44 | ||
| @@ -64,69 +65,88 @@ static esp_err_t nvs_flash_init_check(void) | |||
| 64 | * - ESP_OK: If initialization is successful. | 65 | * - ESP_OK: If initialization is successful. |
| 65 | * - Other error codes if initialization fails. | 66 | * - Other error codes if initialization fails. |
| 66 | */ | 67 | */ |
| 67 | static esp_err_t init_component(esp_err_t (*init_func)(void), | 68 | static void wifi_event_handler(void *arg, esp_event_base_t event_base, |
| 68 | const char *component_name) | 69 | int32_t event_id, void *event_data) |
| 69 | { | 70 | { |
| 70 | esp_err_t ret = init_func(); | 71 | if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { |
| 71 | 72 | esp_wifi_connect(); | |
| 72 | if (ret != ESP_OK) { | 73 | } else if (event_base == WIFI_EVENT && |
| 73 | ESP_LOGE(TAG, "Failed to initialize %s (%s)", component_name, | 74 | event_id == WIFI_EVENT_STA_DISCONNECTED) { |
| 74 | esp_err_to_name(ret)); | 75 | ESP_LOGW(TAG, "Wi-Fi disconnected, reconnecting..."); |
| 75 | return ret; | 76 | esp_wifi_connect(); |
| 77 | xEventGroupClearBits(wifi_event_group, WIFI_CONNECTED_BIT); | ||
| 78 | } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { | ||
| 79 | ESP_LOGI(TAG, "Wi-Fi connected and got IP!"); | ||
| 80 | xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_BIT); | ||
| 76 | } | 81 | } |
| 77 | |||
| 78 | return ESP_OK; | ||
| 79 | } | 82 | } |
| 80 | 83 | ||
| 81 | /** | 84 | /** |
| 82 | * @brief Initializes all the essential components for the application. | 85 | * @TODO missing doc |
| 83 | * | ||
| 84 | * This function initializes the NVS storage, network interface, and event loop | ||
| 85 | * in sequence. If any of these components fail to initialize, the function | ||
| 86 | * returns an error, and the application will stop early. | ||
| 87 | * | ||
| 88 | * @return esp_err_t | ||
| 89 | * - ESP_OK: If all components are successfully initialized. | ||
| 90 | * - ESP_FAIL: If any component fails to initialize. | ||
| 91 | */ | 86 | */ |
| 92 | esp_err_t init_app_components(void) | 87 | static void obtain_time(void) |
| 93 | { | 88 | { |
| 94 | if (init_component(nvs_flash_init_check, "NVS") != ESP_OK) { | 89 | ESP_LOGI(TAG, "Starting SNTP time sync..."); |
| 95 | ESP_LOGE(TAG, "NVS initialization failed, aborting app start."); | 90 | sntp_setoperatingmode(SNTP_OPMODE_POLL); |
| 96 | return ESP_FAIL; | 91 | sntp_setservername(0, "pool.ntp.org"); |
| 92 | sntp_init(); | ||
| 93 | |||
| 94 | time_t now = 0; | ||
| 95 | struct tm timeinfo = {0}; | ||
| 96 | int retry = 0; | ||
| 97 | const int retry_count = 10; | ||
| 98 | |||
| 99 | while (timeinfo.tm_year < (2022 - 1900) && ++retry < retry_count) { | ||
| 100 | ESP_LOGI(TAG, "Waiting for system time to be set... (%d/%d)", | ||
| 101 | retry, retry_count); | ||
| 102 | vTaskDelay(pdMS_TO_TICKS(1000)); | ||
| 103 | time(&now); | ||
| 104 | localtime_r(&now, &timeinfo); | ||
| 97 | } | 105 | } |
| 98 | 106 | ||
| 99 | if (init_component(esp_netif_init, "network interface") != ESP_OK) | 107 | if (timeinfo.tm_year >= (2022 - 1900)) { |
| 100 | return ESP_FAIL; | 108 | char buf[64]; |
| 101 | 109 | strftime(buf, sizeof(buf), "%c", &timeinfo); | |
| 102 | if (init_component(esp_event_loop_create_default, "event loop") != | 110 | ESP_LOGI(TAG, "System time synchronized: %s", buf); |
| 103 | ESP_OK) | 111 | } else { |
| 104 | return ESP_FAIL; | 112 | ESP_LOGW(TAG, "Failed to synchronize time."); |
| 105 | 113 | } | |
| 106 | return ESP_OK; | ||
| 107 | } | 114 | } |
| 108 | 115 | ||
| 109 | /** | 116 | /** |
| 110 | * @brief Main entry point of the application. | 117 | * @brief Main entry point of the application. |
| 111 | * | 118 | * |
| 112 | * This function is responsible for initializing the core components of the | 119 | * This function is responsible for initializing the core components |
| 113 | * application, including the NVS storage, network interface, and event loop. If | 120 | of the |
| 114 | * any initialization step fails, the application will log the error and stop. | 121 | * application, including the NVS storage, network interface, and event loop. If |
| 115 | * | 122 | * any initialization step fails, the application will log the error |
| 116 | * After initialization, this function will configure the WiFi station and start | 123 | and stop. |
| 117 | * the MQTT client. | 124 | * |
| 118 | */ | 125 | * After initialization, this function will configure the WiFi station and start |
| 119 | 126 | * the MQTT client. | |
| 127 | */ | ||
| 120 | void app_main(void) | 128 | void app_main(void) |
| 121 | { | 129 | { |
| 122 | if (init_app_components() != ESP_OK) { | 130 | ESP_ERROR_CHECK(nvs_flash_init_check()); |
| 123 | ESP_LOGE(TAG, "App initialization failed, aborting."); | 131 | ESP_ERROR_CHECK(esp_netif_init()); |
| 124 | return; | 132 | ESP_ERROR_CHECK(esp_event_loop_create_default()); |
| 125 | } | 133 | |
| 134 | wifi_event_group = xEventGroupCreate(); | ||
| 126 | 135 | ||
| 127 | ESP_LOGI(TAG, "Initializing WiFi in station mode"); | 136 | ESP_LOGI(TAG, "Initializing WiFi (station mode)"); |
| 128 | wifi_init_sta(); | 137 | wifi_init_sta(); |
| 129 | 138 | ||
| 130 | ESP_LOGI(TAG, "Starting MQTT client"); | 139 | ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, |
| 140 | &wifi_event_handler, NULL)); | ||
| 141 | ESP_ERROR_CHECK(esp_event_handler_register( | ||
| 142 | IP_EVENT, IP_EVENT_STA_GOT_IP, &wifi_event_handler, NULL)); | ||
| 143 | |||
| 144 | ESP_LOGI(TAG, "Waiting for Wi-Fi connection..."); | ||
| 145 | xEventGroupWaitBits(wifi_event_group, WIFI_CONNECTED_BIT, pdFALSE, | ||
| 146 | pdFALSE, portMAX_DELAY); | ||
| 147 | |||
| 148 | obtain_time(); | ||
| 149 | |||
| 150 | ESP_LOGI(TAG, "Starting MQTT client..."); | ||
| 131 | mqtt_app_start(); | 151 | mqtt_app_start(); |
| 132 | } | 152 | } |
diff --git a/firmware/src/mqtt.c b/firmware/src/mqtt.c index b3553eb..a726534 100644 --- a/firmware/src/mqtt.c +++ b/firmware/src/mqtt.c | |||
| @@ -11,7 +11,6 @@ | |||
| 11 | static const char *TAG = "mqtt"; | 11 | static const char *TAG = "mqtt"; |
| 12 | static esp_mqtt_client_handle_t client; | 12 | static esp_mqtt_client_handle_t client; |
| 13 | 13 | ||
| 14 | // Zliczniki i pomiary | ||
| 15 | static long long last_sent = 0; | 14 | static long long last_sent = 0; |
| 16 | static long long last_rtt = 0; | 15 | static long long last_rtt = 0; |
| 17 | static long long last_rtt_prev = 0; | 16 | static long long last_rtt_prev = 0; |
| @@ -20,7 +19,6 @@ static int received_count = 0; | |||
| 20 | static unsigned long tx_bytes = 0; | 19 | static unsigned long tx_bytes = 0; |
| 21 | static unsigned long rx_bytes = 0; | 20 | static unsigned long rx_bytes = 0; |
| 22 | 21 | ||
| 23 | // Kanały pomiarów | ||
| 24 | #define TOPIC_RTT "device/metrics/rtt" | 22 | #define TOPIC_RTT "device/metrics/rtt" |
| 25 | #define TOPIC_JITTER "device/metrics/jitter" | 23 | #define TOPIC_JITTER "device/metrics/jitter" |
| 26 | #define TOPIC_LOSS "device/metrics/loss" | 24 | #define TOPIC_LOSS "device/metrics/loss" |
| @@ -29,6 +27,9 @@ static unsigned long rx_bytes = 0; | |||
| 29 | #define TOPIC_NET_TX "device/metrics/net_tx" | 27 | #define TOPIC_NET_TX "device/metrics/net_tx" |
| 30 | #define TOPIC_NET_RX "device/metrics/net_rx" | 28 | #define TOPIC_NET_RX "device/metrics/net_rx" |
| 31 | 29 | ||
| 30 | #define TOPIC_IN "device/echo/in" | ||
| 31 | #define TOPIC_OUT "device/echo/out" | ||
| 32 | |||
| 32 | static void mqtt_event_handler(void *handler_args, esp_event_base_t base, | 33 | static void mqtt_event_handler(void *handler_args, esp_event_base_t base, |
| 33 | int32_t event_id, void *event_data) | 34 | int32_t event_id, void *event_data) |
| 34 | { | 35 | { |
| @@ -37,13 +38,12 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, | |||
| 37 | switch (event->event_id) { | 38 | switch (event->event_id) { |
| 38 | case MQTT_EVENT_CONNECTED: | 39 | case MQTT_EVENT_CONNECTED: |
| 39 | ESP_LOGI(TAG, "MQTT connected!"); | 40 | ESP_LOGI(TAG, "MQTT connected!"); |
| 40 | esp_mqtt_client_subscribe(event->client, PUB_TOPIC, 1); | 41 | esp_mqtt_client_subscribe(event->client, TOPIC_IN, 1); |
| 41 | break; | 42 | break; |
| 42 | 43 | ||
| 43 | case MQTT_EVENT_DATA: { | 44 | case MQTT_EVENT_DATA: { |
| 44 | char topic[event->topic_len + 1]; | 45 | char topic[event->topic_len + 1]; |
| 45 | char data[event->data_len + 1]; | 46 | char data[event->data_len + 1]; |
| 46 | |||
| 47 | memcpy(topic, event->topic, event->topic_len); | 47 | memcpy(topic, event->topic, event->topic_len); |
| 48 | topic[event->topic_len] = '\0'; | 48 | topic[event->topic_len] = '\0'; |
| 49 | memcpy(data, event->data, event->data_len); | 49 | memcpy(data, event->data, event->data_len); |
| @@ -61,8 +61,9 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, | |||
| 61 | last_rtt_prev = last_rtt; | 61 | last_rtt_prev = last_rtt; |
| 62 | last_rtt = rtt; | 62 | last_rtt = rtt; |
| 63 | received_count++; | 63 | received_count++; |
| 64 | |||
| 64 | esp_mqtt_client_publish( | 65 | esp_mqtt_client_publish( |
| 65 | event->client, SUB_TOPIC, data, 0, 1, 0); | 66 | event->client, TOPIC_OUT, data, 0, 1, 0); |
| 66 | tx_bytes += strlen(data); | 67 | tx_bytes += strlen(data); |
| 67 | } | 68 | } |
| 68 | } | 69 | } |
| @@ -91,7 +92,7 @@ static void mqtt_test_task(void *pvParameters) | |||
| 91 | last_sent = esp_timer_get_time() / 1000ULL; | 92 | last_sent = esp_timer_get_time() / 1000ULL; |
| 92 | char msg[32]; | 93 | char msg[32]; |
| 93 | snprintf(msg, sizeof(msg), "%lld", last_sent); | 94 | snprintf(msg, sizeof(msg), "%lld", last_sent); |
| 94 | esp_mqtt_client_publish(client, PUB_TOPIC, msg, 0, 1, 0); | 95 | esp_mqtt_client_publish(client, TOPIC_IN, msg, 0, 1, 0); |
| 95 | tx_bytes += strlen(msg); | 96 | tx_bytes += strlen(msg); |
| 96 | sent_count++; | 97 | sent_count++; |
| 97 | vTaskDelay(pdMS_TO_TICKS(delay_ms)); | 98 | vTaskDelay(pdMS_TO_TICKS(delay_ms)); |
| @@ -106,7 +107,6 @@ static void mqtt_metrics_task(void *pvParameters) | |||
| 106 | long long dt = (now - last_time) / 1000ULL; | 107 | long long dt = (now - last_time) / 1000ULL; |
| 107 | last_time = now; | 108 | last_time = now; |
| 108 | 109 | ||
| 109 | // RTT | ||
| 110 | if (last_rtt > 0) { | 110 | if (last_rtt > 0) { |
| 111 | char buf[32]; | 111 | char buf[32]; |
| 112 | snprintf(buf, sizeof(buf), "%lld", last_rtt); | 112 | snprintf(buf, sizeof(buf), "%lld", last_rtt); |
| @@ -115,7 +115,6 @@ static void mqtt_metrics_task(void *pvParameters) | |||
| 115 | tx_bytes += strlen(buf); | 115 | tx_bytes += strlen(buf); |
| 116 | } | 116 | } |
| 117 | 117 | ||
| 118 | // Jitter | ||
| 119 | if (last_rtt_prev > 0) { | 118 | if (last_rtt_prev > 0) { |
| 120 | long long jitter = llabs(last_rtt - last_rtt_prev); | 119 | long long jitter = llabs(last_rtt - last_rtt_prev); |
| 121 | char buf[32]; | 120 | char buf[32]; |
| @@ -125,23 +124,21 @@ static void mqtt_metrics_task(void *pvParameters) | |||
| 125 | tx_bytes += strlen(buf); | 124 | tx_bytes += strlen(buf); |
| 126 | } | 125 | } |
| 127 | 126 | ||
| 128 | // Packet loss | 127 | int loss = |
| 129 | int loss = sent_count > 0 ? (sent_count - received_count) * | 128 | (sent_count > 0) |
| 130 | 100 / sent_count | 129 | ? ((sent_count - received_count) * 100 / sent_count) |
| 131 | : 0; | 130 | : 0; |
| 132 | char buf_loss[16]; | 131 | char buf_loss[16]; |
| 133 | snprintf(buf_loss, sizeof(buf_loss), "%d", loss); | 132 | snprintf(buf_loss, sizeof(buf_loss), "%d", loss); |
| 134 | esp_mqtt_client_publish(client, TOPIC_LOSS, buf_loss, 0, 1, 0); | 133 | esp_mqtt_client_publish(client, TOPIC_LOSS, buf_loss, 0, 1, 0); |
| 135 | tx_bytes += strlen(buf_loss); | 134 | tx_bytes += strlen(buf_loss); |
| 136 | 135 | ||
| 137 | // Throughput | ||
| 138 | char buf_thr[16]; | 136 | char buf_thr[16]; |
| 139 | snprintf(buf_thr, sizeof(buf_thr), "%d", received_count); | 137 | snprintf(buf_thr, sizeof(buf_thr), "%d", received_count); |
| 140 | esp_mqtt_client_publish(client, TOPIC_THROUGHPUT, buf_thr, 0, 1, | 138 | esp_mqtt_client_publish(client, TOPIC_THROUGHPUT, buf_thr, 0, 1, |
| 141 | 0); | 139 | 0); |
| 142 | tx_bytes += strlen(buf_thr); | 140 | tx_bytes += strlen(buf_thr); |
| 143 | 141 | ||
| 144 | // CPU usage | ||
| 145 | UBaseType_t free_heap = xPortGetFreeHeapSize(); | 142 | UBaseType_t free_heap = xPortGetFreeHeapSize(); |
| 146 | UBaseType_t min_free_heap = xPortGetMinimumEverFreeHeapSize(); | 143 | UBaseType_t min_free_heap = xPortGetMinimumEverFreeHeapSize(); |
| 147 | int cpu_load = | 144 | int cpu_load = |
| @@ -156,16 +153,15 @@ static void mqtt_metrics_task(void *pvParameters) | |||
| 156 | esp_mqtt_client_publish(client, TOPIC_CPU, buf_cpu, 0, 1, 0); | 153 | esp_mqtt_client_publish(client, TOPIC_CPU, buf_cpu, 0, 1, 0); |
| 157 | tx_bytes += strlen(buf_cpu); | 154 | tx_bytes += strlen(buf_cpu); |
| 158 | 155 | ||
| 159 | // Network TX/RX bytes | ||
| 160 | char buf_tx[32], buf_rx[32]; | 156 | char buf_tx[32], buf_rx[32]; |
| 161 | snprintf(buf_tx, sizeof(buf_tx), "%lu", tx_bytes); | 157 | snprintf(buf_tx, sizeof(buf_tx), "%lu", tx_bytes); |
| 162 | snprintf(buf_rx, sizeof(buf_rx), "%lu", rx_bytes); | 158 | snprintf(buf_rx, sizeof(buf_rx), "%lu", rx_bytes); |
| 163 | esp_mqtt_client_publish(client, TOPIC_NET_TX, buf_tx, 0, 1, 0); | 159 | esp_mqtt_client_publish(client, TOPIC_NET_TX, buf_tx, 0, 1, 0); |
| 164 | esp_mqtt_client_publish(client, TOPIC_NET_RX, buf_rx, 0, 1, 0); | 160 | esp_mqtt_client_publish(client, TOPIC_NET_RX, buf_rx, 0, 1, 0); |
| 165 | 161 | ||
| 166 | // Reset liczniki na kolejny interwał | ||
| 167 | tx_bytes = 0; | 162 | tx_bytes = 0; |
| 168 | rx_bytes = 0; | 163 | rx_bytes = 0; |
| 164 | sent_count = 0; | ||
| 169 | received_count = 0; | 165 | received_count = 0; |
| 170 | 166 | ||
| 171 | vTaskDelay(pdMS_TO_TICKS(1000)); | 167 | vTaskDelay(pdMS_TO_TICKS(1000)); |
diff --git a/firmware/src/wifi_scan.c b/firmware/src/wifi_scan.c index 02bb451..07f3dc7 100644 --- a/firmware/src/wifi_scan.c +++ b/firmware/src/wifi_scan.c | |||
| @@ -12,12 +12,19 @@ | |||
| 12 | #define WIFI_SCAN_TASK_NAME "wifi_scan_task" | 12 | #define WIFI_SCAN_TASK_NAME "wifi_scan_task" |
| 13 | 13 | ||
| 14 | /** | 14 | /** |
| 15 | * @brief Task to perform WiFi scanning asynchronously. | 15 | * @brief Asynchronous WiFi scanning task for ESP32 with FreeRTOS. |
| 16 | * | 16 | * |
| 17 | * Starts a WiFi scan, polls for scan completion, | 17 | * Configures and initiates a non-blocking WiFi scan on the specified or all |
| 18 | * retrieves and logs the found access points, then deletes itself. | 18 | * channels, including hidden networks if enabled. Periodically polls for scan |
| 19 | * completion without blocking other tasks, retrieves discovered access points | ||
| 20 | * up to a defined maximum, and logs each AP's SSID and signal strength (RSSI). | ||
| 19 | * | 21 | * |
| 20 | * @param pvParameter Task parameter (unused). | 22 | * After logging, the task self-deletes to free system resources. |
| 23 | * | ||
| 24 | * This design ensures WiFi scanning runs in the background, preserving system | ||
| 25 | * responsiveness and maintaining real-time operation in embedded applications. | ||
| 26 | * | ||
| 27 | * @param pvParameter Pointer to task parameters (unused). | ||
| 21 | */ | 28 | */ |
| 22 | static void wifi_scan_task(void *pvParameter) | 29 | static void wifi_scan_task(void *pvParameter) |
| 23 | { | 30 | { |
| @@ -36,13 +43,15 @@ static void wifi_scan_task(void *pvParameter) | |||
| 36 | while (true) { | 43 | while (true) { |
| 37 | uint16_t finished_ap_count = 0; | 44 | uint16_t finished_ap_count = 0; |
| 38 | ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&finished_ap_count)); | 45 | ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&finished_ap_count)); |
| 39 | if (finished_ap_count > 0) | 46 | if (finished_ap_count > 0) { |
| 40 | break; | 47 | break; |
| 48 | } | ||
| 41 | vTaskDelay(pdMS_TO_TICKS(WIFI_SCAN_POLL_INTERVAL_MS)); | 49 | vTaskDelay(pdMS_TO_TICKS(WIFI_SCAN_POLL_INTERVAL_MS)); |
| 42 | } | 50 | } |
| 43 | 51 | ||
| 44 | ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&ap_count, ap_info)); | 52 | ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&ap_count, ap_info)); |
| 45 | ESP_LOGI("wifi_scan", "Found %d access points:", ap_count); | 53 | ESP_LOGI("wifi_scan", "Found %d access points:", ap_count); |
| 54 | |||
| 46 | for (uint16_t ap_index = 0; ap_index < ap_count; ++ap_index) { | 55 | for (uint16_t ap_index = 0; ap_index < ap_count; ++ap_index) { |
| 47 | ESP_LOGI("wifi_scan", "%d: SSID: %s, RSSI: %d", ap_index + 1, | 56 | ESP_LOGI("wifi_scan", "%d: SSID: %s, RSSI: %d", ap_index + 1, |
| 48 | ap_info[ap_index].ssid, ap_info[ap_index].rssi); | 57 | ap_info[ap_index].ssid, ap_info[ap_index].rssi); |
