diff --git a/README.md b/README.md index fa7699e..9a4b4ac 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ typedef struct void app_main(void) { - esp_log_level_set("zh_espnow", ESP_LOG_NONE); + esp_log_level_set("zh_espnow", ESP_LOG_NONE); // For ESP8266 first enable "Component config -> Log output -> Enable log set level" via menuconfig. nvs_flash_init(); esp_netif_init(); esp_event_loop_create_default(); @@ -87,13 +87,23 @@ void app_main(void) send_message.float_value = 1.234; send_message.bool_value = false; printf("Used ESP-NOW version %d.\n", zh_espnow_get_version()); + uint8_t counter = 0; for (;;) { + counter++; send_message.int_value = esp_random(); zh_espnow_send(NULL, (uint8_t *)&send_message, sizeof(send_message)); vTaskDelay(5000 / portTICK_PERIOD_MS); zh_espnow_send(target, (uint8_t *)&send_message, sizeof(send_message)); vTaskDelay(5000 / portTICK_PERIOD_MS); + if (counter == 10) + { + counter = 0; + const zh_espnow_stats_t *stats = zh_espnow_get_stats(); + printf("Number of successfully sent messages: %ld.\n", stats->sent_success); + printf("Number of failed sent messages: %ld.\n", stats->sent_fail); + printf("Number of received messages: %ld.\n", stats->received); + } } } diff --git a/include/zh_espnow.h b/include/zh_espnow.h index f5a8c4e..e5a485c 100644 --- a/include/zh_espnow.h +++ b/include/zh_espnow.h @@ -62,6 +62,13 @@ extern "C" uint16_t data_len; // Size of the received ESP-NOW message. } zh_espnow_event_on_recv_t; + typedef struct // Structure for message statistics storage. + { + uint32_t sent_success; // Number of successfully sent messages. + uint32_t sent_fail; // Number of failed sent messages. + uint32_t received; // Number of received messages. + } zh_espnow_stats_t; + /** * @brief Initialize ESP-NOW interface. * @@ -102,6 +109,13 @@ extern "C" */ uint8_t zh_espnow_get_version(void); + /** + * @brief Get ESP-NOW statistics. + * + * @return Pointer to the statistics structure. + */ + const zh_espnow_stats_t *zh_espnow_get_stats(void); + #ifdef __cplusplus } #endif \ No newline at end of file diff --git a/version.txt b/version.txt index afaf360..1cc5f65 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -1.0.0 \ No newline at end of file +1.1.0 \ No newline at end of file diff --git a/zh_espnow.c b/zh_espnow.c index 9f0254a..987ebd2 100755 --- a/zh_espnow.c +++ b/zh_espnow.c @@ -55,6 +55,7 @@ static EventGroupHandle_t _event_group_handle = {0}; static QueueHandle_t _queue_handle = {0}; static TaskHandle_t _processing_task_handle = {0}; static zh_espnow_init_config_t _init_config = {0}; +static zh_espnow_stats_t _stats = {0}; static bool _is_initialized = false; static const uint8_t _broadcast_mac[ESP_NOW_ETH_ALEN] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; #if defined ESP_NOW_MAX_DATA_LEN_V2 @@ -423,6 +424,7 @@ static void _zh_espnow_process_send(_queue_t *queue) { ESP_LOGI(TAG, "ESP-NOW data sent successfully to MAC %02X:%02X:%02X:%02X:%02X:%02X after %d attempts.", MAC2STR(queue->data.mac_addr), attempt + 1); on_send->status = ZH_ESPNOW_SEND_SUCCESS; + ++_stats.sent_success; break; } else @@ -435,6 +437,7 @@ static void _zh_espnow_process_send(_queue_t *queue) { ESP_LOGE(TAG, "Failed to send ESP-NOW data to MAC %02X:%02X:%02X:%02X:%02X:%02X after %d attempts.", MAC2STR(queue->data.mac_addr), _init_config.attempts); on_send->status = ZH_ESPNOW_SEND_FAIL; + ++_stats.sent_fail; } err = esp_event_post(ZH_ESPNOW, ZH_ESPNOW_ON_SEND_EVENT, on_send, sizeof(zh_espnow_event_on_send_t), portTICK_PERIOD_MS); if (err == ESP_OK) @@ -455,6 +458,7 @@ static void _zh_espnow_process_recv(_queue_t *queue) { ZH_ESPNOW_LOGI("Processing incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X started.", MAC2STR(queue->data.mac_addr)); zh_espnow_event_on_recv_t *recv_data = (zh_espnow_event_on_recv_t *)&queue->data; + ++_stats.received; esp_err_t err = esp_event_post(ZH_ESPNOW, ZH_ESPNOW_ON_RECV_EVENT, recv_data, recv_data->data_len + sizeof(recv_data->mac_addr) + sizeof(uint8_t), portTICK_PERIOD_MS); if (err == ESP_OK) { @@ -499,4 +503,9 @@ uint8_t zh_espnow_get_version(void) } ZH_ESPNOW_LOGI("ESP-NOW version receiption successfully."); return (uint8_t)version; +} + +const zh_espnow_stats_t *zh_espnow_get_stats(void) +{ + return &_stats; } \ No newline at end of file