diff --git a/README.md b/README.md index 386cada..4ee8e04 100644 --- a/README.md +++ b/README.md @@ -3,21 +3,24 @@ ## Tested on 1. ESP8266 RTOS_SDK v3.4 -2. ESP32 ESP-IDF v5.2 +2. ESP32 ESP-IDF v5.4 ## Features -1. The maximum size of transmitted data is up to 250 bytes. +1. The maximum size of the transmitted data is up to 250 / 1490 bytes. Please see attention for details. 2. Support of any data types. 3. All nodes are not visible to the network scanner. 4. Not required a pre-pairings for data transfer. 5. Broadcast or unicast data transmissions. -6. Possibility uses WiFi AP or STA modes at the same time with ESP-NOW. +6. Possibility uses WiFi AP or STA modes at the same time with ESP-NOW. Please see attention for details. ## Attention -1. For correct operation in ESP-NOW + STA mode, your WiFi router must be set to the same channel as ESP-NOW. -2. All devices on the network must have the same WiFi channel. +1. For correct operation ESP-NOW interface must be the same as the WiFi interface (except in the case of APSTA mode - the ESP-NOW interface can be anything). +2. For correct operation in ESP-NOW + STA mode, your WiFi router must be set to the same channel as ESP-NOW. +3. All devices on the network must have the same WiFi channel. +4. For use encrypted messages, use the application layer. +5. ESP-NOW supports two versions: v1.0 (RTOS_SDK and ESP-IDF v5.3 and below) and v2.0 (ESP-IDF v5.4 and highter). The maximum packet length supported by v2.0 devices is 1490 bytes, while the maximum packet length supported by v1.0 devices is 250 bytes. The v2.0 devices are capable of receiving packets from both v2.0 and v1.0 devices. In contrast, v1.0 devices can only receive packets from other v1.0 devices. However, v1.0 devices can receive v2.0 packets if the packet length is less than or equal to 250 bytes. For packets exceeding this length, the v1.0 devices will either truncate the data to the first 250 bytes or discard the packet entirely. ## Using @@ -52,7 +55,7 @@ Sending and receiving messages: void zh_espnow_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data); -uint8_t target[6] = {0x34, 0x94, 0x54, 0x24, 0xA3, 0x41}; +uint8_t target[6] = {0xEC, 0x94, 0xCB, 0x87, 0xEC, 0xFC}; typedef struct { diff --git a/include/zh_espnow.h b/include/zh_espnow.h index daea1a9..07a9399 100644 --- a/include/zh_espnow.h +++ b/include/zh_espnow.h @@ -11,13 +11,12 @@ #define ZH_ESPNOW_INIT_CONFIG_DEFAULT() \ { \ - .task_priority = 4, \ - .stack_size = 2048, \ - .queue_size = 32, \ + .task_priority = 10, \ + .stack_size = 3072, \ + .queue_size = 64, \ .wifi_interface = WIFI_IF_STA, \ .wifi_channel = 1, \ - .attempts = 3 \ - } + .attempts = 3} #ifdef __cplusplus extern "C" @@ -26,7 +25,7 @@ extern "C" typedef struct // Structure for initial initialization of ESP-NOW interface. { - uint8_t task_priority; // Task priority for the ESP-NOW messages processing. @note It is not recommended to set a value less than 4. + uint8_t task_priority; // Task priority for the ESP-NOW messages processing. @note It is not recommended to set a value less than 5. uint16_t stack_size; // Stack size for task for the ESP-NOW messages processing. @note The minimum size is 2048 bytes. uint8_t queue_size; // Queue size for task for the ESP-NOW messages processing. @note The size depends on the number of messages to be processed. It is not recommended to set the value less than 16. wifi_interface_t wifi_interface; // WiFi interface (STA or AP) used for ESP-NOW operation. @note The MAC address of the device depends on the selected WiFi interface. @@ -50,15 +49,15 @@ extern "C" typedef struct // Structure for sending data to the event handler when an ESP-NOW message was sent. @note Should be used with ZH_ESPNOW event base and ZH_ESPNOW_ON_SEND_EVENT event. { - uint8_t mac_addr[6]; // MAC address of the device to which the ESP-NOW message was sent. + uint8_t mac_addr[ESP_NOW_ETH_ALEN]; // MAC address of the device to which the ESP-NOW message was sent. zh_espnow_on_send_event_type_t status; // Status of sent ESP-NOW message. } zh_espnow_event_on_send_t; typedef struct // Structure for sending data to the event handler when an ESP-NOW message was received. @note Should be used with ZH_ESPNOW event base and ZH_ESPNOW_ON_RECV_EVENT event. { - uint8_t mac_addr[6]; // MAC address of the sender ESP-NOW message. - uint8_t *data; // Pointer to the data of the received ESP-NOW message. - uint8_t data_len; // Size of the received ESP-NOW message. + uint8_t mac_addr[ESP_NOW_ETH_ALEN]; // MAC address of the sender ESP-NOW message. + uint8_t *data; // Pointer to the data of the received ESP-NOW message. + uint16_t data_len; // Size of the received ESP-NOW message. } zh_espnow_event_on_recv_t; /** diff --git a/version.txt b/version.txt index a6a3a43..1cc5f65 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -1.0.4 \ 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 old mode 100644 new mode 100755 index 027d80b..5e612c2 --- a/zh_espnow.c +++ b/zh_espnow.c @@ -20,6 +20,11 @@ static TaskHandle_t _processing_task_handle = {0}; static zh_espnow_init_config_t _init_config = {0}; static bool _is_initialized = false; static uint8_t _attempts = 0; +#if defined ESP_NOW_MAX_DATA_LEN_V2 +static uint16_t _max_message_size = ESP_NOW_MAX_DATA_LEN_V2; +#else +static uint16_t _max_message_size = ESP_NOW_MAX_DATA_LEN; +#endif typedef struct { @@ -30,9 +35,9 @@ typedef struct } id; struct { - uint8_t mac_addr[6]; + uint8_t mac_addr[ESP_NOW_ETH_ALEN]; uint8_t *payload; - uint8_t payload_len; + uint16_t payload_len; } data; } _queue_t; @@ -61,23 +66,24 @@ esp_err_t zh_espnow_init(const zh_espnow_init_config_t *config) else if (err == ESP_FAIL) { uint8_t prim = 0; - wifi_second_chan_t sec = 0; + wifi_second_chan_t sec = WIFI_SECOND_CHAN_NONE; esp_wifi_get_channel(&prim, &sec); if (prim != _init_config.wifi_channel) { ESP_LOGW(TAG, "ESP-NOW initialization warning. The device is connected to the router. Channel %d will be used for ESP-NOW.", prim); } } + esp_wifi_set_protocol(_init_config.wifi_interface, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_LR); _event_group_handle = xEventGroupCreate(); _queue_handle = xQueueCreate(_init_config.queue_size, sizeof(_queue_t)); if (esp_now_init() != ESP_OK || esp_now_register_send_cb(_send_cb) != ESP_OK || esp_now_register_recv_cb(_recv_cb) != ESP_OK) { - ESP_LOGE(TAG, "ESP-NOW initialization fail. Internal error."); + ESP_LOGE(TAG, "ESP-NOW initialization fail. Internal error at line %d.", __LINE__); return ESP_FAIL; } if (xTaskCreatePinnedToCore(&_processing, "NULL", _init_config.stack_size, NULL, _init_config.task_priority, &_processing_task_handle, tskNO_AFFINITY) != pdPASS) { - ESP_LOGE(TAG, "ESP-NOW initialization fail. Internal error."); + ESP_LOGE(TAG, "ESP-NOW initialization fail. Internal error at line %d.", __LINE__); return ESP_FAIL; } _is_initialized = true; @@ -119,7 +125,7 @@ esp_err_t zh_espnow_send(const uint8_t *target, const uint8_t *data, const uint8 ESP_LOGE(TAG, "Adding outgoing ESP-NOW data to queue fail. ESP-NOW not initialized."); return ESP_FAIL; } - if (data == NULL || data_len == 0 || data_len > ESP_NOW_MAX_DATA_LEN) + if (data == NULL || data_len == 0 || data_len > _max_message_size) { ESP_LOGE(TAG, "Adding outgoing ESP-NOW data to queue fail. Invalid argument."); return ESP_ERR_INVALID_ARG; @@ -129,31 +135,23 @@ esp_err_t zh_espnow_send(const uint8_t *target, const uint8_t *data, const uint8 ESP_LOGW(TAG, "Adding outgoing ESP-NOW data to queue fail. Queue is almost full."); return ESP_ERR_INVALID_STATE; } - uint8_t broadcast[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; + uint8_t broadcast[ESP_NOW_ETH_ALEN] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; _queue_t queue = {0}; queue.id = TO_SEND; if (target == NULL) { - memcpy(queue.data.mac_addr, broadcast, 6); + memcpy(queue.data.mac_addr, broadcast, ESP_NOW_ETH_ALEN); } else { - memcpy(queue.data.mac_addr, target, 6); - } - if (data_len / sizeof(void *) == 0) - { - queue.data.payload = heap_caps_malloc(data_len, MALLOC_CAP_32BIT); - } - else - { - queue.data.payload = heap_caps_malloc(data_len, MALLOC_CAP_8BIT); + memcpy(queue.data.mac_addr, target, ESP_NOW_ETH_ALEN); } + queue.data.payload = heap_caps_calloc(1, data_len, MALLOC_CAP_8BIT); if (queue.data.payload == NULL) { ESP_LOGE(TAG, "Adding outgoing ESP-NOW data to queue fail. Memory allocation fail or no free memory in the heap."); return ESP_ERR_NO_MEM; } - memset(queue.data.payload, 0, data_len); memcpy(queue.data.payload, data, data_len); queue.data.payload_len = data_len; if (target == NULL) @@ -172,7 +170,7 @@ esp_err_t zh_espnow_send(const uint8_t *target, const uint8_t *data, const uint8 return ESP_OK; } -static void _send_cb(const uint8_t *mac_addr, esp_now_send_status_t status) +static void IRAM_ATTR _send_cb(const uint8_t *mac_addr, esp_now_send_status_t status) { if (status == ESP_NOW_SEND_SUCCESS) { @@ -185,9 +183,9 @@ static void _send_cb(const uint8_t *mac_addr, esp_now_send_status_t status) } #if defined CONFIG_IDF_TARGET_ESP8266 || ESP_IDF_VERSION_MAJOR == 4 -static void _recv_cb(const uint8_t *mac_addr, const uint8_t *data, int data_len) +static void IRAM_ATTR _recv_cb(const uint8_t *mac_addr, const uint8_t *data, int data_len) #else -static void _recv_cb(const esp_now_recv_info_t *esp_now_info, const uint8_t *data, int data_len) +static void IRAM_ATTR _recv_cb(const esp_now_recv_info_t *esp_now_info, const uint8_t *data, int data_len) #endif { #if defined CONFIG_IDF_TARGET_ESP8266 || ESP_IDF_VERSION_MAJOR == 4 @@ -203,24 +201,16 @@ static void _recv_cb(const esp_now_recv_info_t *esp_now_info, const uint8_t *dat _queue_t queue = {0}; queue.id = ON_RECV; #if defined CONFIG_IDF_TARGET_ESP8266 || ESP_IDF_VERSION_MAJOR == 4 - memcpy(queue.data.mac_addr, mac_addr, 6); + memcpy(queue.data.mac_addr, mac_addr, ESP_NOW_ETH_ALEN); #else - memcpy(queue.data.mac_addr, esp_now_info->src_addr, 6); + memcpy(queue.data.mac_addr, esp_now_info->src_addr, ESP_NOW_ETH_ALEN); #endif - if (data_len / sizeof(void *) == 0) - { - queue.data.payload = heap_caps_malloc(data_len, MALLOC_CAP_32BIT); - } - else - { - queue.data.payload = heap_caps_malloc(data_len, MALLOC_CAP_8BIT); - } + queue.data.payload = heap_caps_calloc(1, data_len, MALLOC_CAP_8BIT); if (queue.data.payload == NULL) { ESP_LOGE(TAG, "Adding incoming ESP-NOW data to queue fail. Memory allocation fail or no free memory in the heap."); return; } - memset(queue.data.payload, 0, data_len); memcpy(queue.data.payload, data, data_len); queue.data.payload_len = data_len; #if defined CONFIG_IDF_TARGET_ESP8266 || ESP_IDF_VERSION_MAJOR == 4 @@ -234,7 +224,7 @@ static void _recv_cb(const esp_now_recv_info_t *esp_now_info, const uint8_t *dat } } -static void _processing(void *pvParameter) +static void IRAM_ATTR _processing(void *pvParameter) { _queue_t queue = {0}; while (xQueueReceive(_queue_handle, &queue, portMAX_DELAY) == pdTRUE) @@ -244,16 +234,15 @@ static void _processing(void *pvParameter) { case TO_SEND: ESP_LOGI(TAG, "Outgoing ESP-NOW data to MAC %02X:%02X:%02X:%02X:%02X:%02X processing begin.", MAC2STR(queue.data.mac_addr)); - esp_now_peer_info_t *peer = heap_caps_malloc(sizeof(esp_now_peer_info_t), MALLOC_CAP_8BIT); + esp_now_peer_info_t *peer = heap_caps_calloc(1, sizeof(esp_now_peer_info_t), MALLOC_CAP_8BIT); if (peer == NULL) { ESP_LOGE(TAG, "Outgoing ESP-NOW data processing fail. Memory allocation fail or no free memory in the heap."); heap_caps_free(queue.data.payload); break; } - memset(peer, 0, sizeof(esp_now_peer_info_t)); peer->ifidx = _init_config.wifi_interface; - memcpy(peer->peer_addr, queue.data.mac_addr, 6); + memcpy(peer->peer_addr, queue.data.mac_addr, ESP_NOW_ETH_ALEN); err = esp_now_add_peer(peer); if (err == ESP_ERR_ESPNOW_NO_MEM) { @@ -269,7 +258,7 @@ static void _processing(void *pvParameter) heap_caps_free(peer); break; } - zh_espnow_event_on_send_t *on_send = heap_caps_malloc(sizeof(zh_espnow_event_on_send_t), MALLOC_CAP_8BIT); + zh_espnow_event_on_send_t *on_send = heap_caps_calloc(1, sizeof(zh_espnow_event_on_send_t), MALLOC_CAP_8BIT); if (on_send == NULL) { ESP_LOGE(TAG, "Outgoing ESP-NOW data processing fail. Memory allocation fail or no free memory in the heap."); @@ -277,8 +266,7 @@ static void _processing(void *pvParameter) heap_caps_free(peer); break; } - memset(on_send, 0, sizeof(zh_espnow_event_on_send_t)); - memcpy(on_send->mac_addr, queue.data.mac_addr, 6); + memcpy(on_send->mac_addr, queue.data.mac_addr, ESP_NOW_ETH_ALEN); SEND: ++_attempts; err = esp_now_send(queue.data.mac_addr, queue.data.payload, queue.data.payload_len); @@ -293,7 +281,7 @@ static void _processing(void *pvParameter) } else if (err != ESP_OK) { - ESP_LOGE(TAG, "Sending ESP-NOW data fail. Internal error."); + ESP_LOGE(TAG, "Sending ESP-NOW data fail. Internal error at line %d.", __LINE__); heap_caps_free(queue.data.payload); heap_caps_free(peer); heap_caps_free(on_send); @@ -307,7 +295,7 @@ static void _processing(void *pvParameter) EventBits_t bit = xEventGroupWaitBits(_event_group_handle, DATA_SEND_SUCCESS | DATA_SEND_FAIL, pdTRUE, pdFALSE, 50 / portTICK_PERIOD_MS); if ((bit & DATA_SEND_SUCCESS) != 0) { - ESP_LOGI(TAG, "Confirmation message received. ESP-NOW message to MAC %02X:%02X:%02X:%02X:%02X:%02X sent success.", MAC2STR(queue.data.mac_addr)); + ESP_LOGI(TAG, "Confirmation message received. ESP-NOW message to MAC %02X:%02X:%02X:%02X:%02X:%02X sent success after %d attempts.", MAC2STR(queue.data.mac_addr), _attempts); on_send->status = ZH_ESPNOW_SEND_SUCCESS; _attempts = 0; } @@ -335,7 +323,7 @@ static void _processing(void *pvParameter) ESP_LOGI(TAG, "Incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X processing begin.", MAC2STR(queue.data.mac_addr)); zh_espnow_event_on_recv_t *recv_data = (zh_espnow_event_on_recv_t *)&queue.data; ESP_LOGI(TAG, "Incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.mac_addr)); - if (esp_event_post(ZH_ESPNOW, ZH_ESPNOW_ON_RECV_EVENT, recv_data, recv_data->data_len + 7, portTICK_PERIOD_MS) != ESP_OK) + if (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) != ESP_OK) { ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__); }