Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
af7fa204e1 | |||
f4ecd08e45 | |||
7608ba0abf |
@ -86,6 +86,7 @@ void app_main(void)
|
|||||||
strcpy(send_message.char_value, "THIS IS A CHAR");
|
strcpy(send_message.char_value, "THIS IS A CHAR");
|
||||||
send_message.float_value = 1.234;
|
send_message.float_value = 1.234;
|
||||||
send_message.bool_value = false;
|
send_message.bool_value = false;
|
||||||
|
printf("Used ESP-NOW version %d.\n", zh_espnow_get_version());
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
send_message.int_value = esp_random();
|
send_message.int_value = esp_random();
|
||||||
|
@ -16,7 +16,8 @@
|
|||||||
.queue_size = 64, \
|
.queue_size = 64, \
|
||||||
.wifi_interface = WIFI_IF_STA, \
|
.wifi_interface = WIFI_IF_STA, \
|
||||||
.wifi_channel = 1, \
|
.wifi_channel = 1, \
|
||||||
.attempts = 3}
|
.attempts = 3, \
|
||||||
|
.battery_mode = false}
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C"
|
extern "C"
|
||||||
@ -31,6 +32,7 @@ extern "C"
|
|||||||
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.
|
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.
|
||||||
uint8_t wifi_channel; // Wi-Fi channel uses to send/receive ESP-NOW data. @note Values from 1 to 14.
|
uint8_t wifi_channel; // Wi-Fi channel uses to send/receive ESP-NOW data. @note Values from 1 to 14.
|
||||||
uint8_t attempts; // Maximum number of attempts to send a message. @note It is not recommended to set a value greater than 5.
|
uint8_t attempts; // Maximum number of attempts to send a message. @note It is not recommended to set a value greater than 5.
|
||||||
|
bool battery_mode; // Battery operation mode. If true, the node does not receive messages.
|
||||||
} zh_espnow_init_config_t;
|
} zh_espnow_init_config_t;
|
||||||
|
|
||||||
ESP_EVENT_DECLARE_BASE(ZH_ESPNOW);
|
ESP_EVENT_DECLARE_BASE(ZH_ESPNOW);
|
||||||
@ -104,6 +106,14 @@ extern "C"
|
|||||||
*/
|
*/
|
||||||
esp_err_t zh_espnow_send(const uint8_t *target, const uint8_t *data, const uint8_t data_len);
|
esp_err_t zh_espnow_send(const uint8_t *target, const uint8_t *data, const uint8_t data_len);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Get ESP-NOW version.
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
* - ESP-NOW version
|
||||||
|
*/
|
||||||
|
uint8_t zh_espnow_get_version(void);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
@ -1 +1 @@
|
|||||||
1.1.0
|
1.3.1
|
64
zh_espnow.c
64
zh_espnow.c
@ -73,15 +73,30 @@ esp_err_t zh_espnow_init(const zh_espnow_init_config_t *config)
|
|||||||
ESP_LOGW(TAG, "ESP-NOW initialization warning. The device is connected to the router. Channel %d will be used for ESP-NOW.", prim);
|
ESP_LOGW(TAG, "ESP-NOW initialization warning. The device is connected to the router. Channel %d will be used for ESP-NOW.", prim);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#if defined CONFIG_IDF_TARGET_ESP8266 || CONFIG_IDF_TARGET_ESP32C2
|
||||||
|
esp_wifi_set_protocol(_init_config.wifi_interface, WIFI_PROTOCOL_11B);
|
||||||
|
#else
|
||||||
esp_wifi_set_protocol(_init_config.wifi_interface, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_LR);
|
esp_wifi_set_protocol(_init_config.wifi_interface, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_LR);
|
||||||
|
#endif
|
||||||
_event_group_handle = xEventGroupCreate();
|
_event_group_handle = xEventGroupCreate();
|
||||||
_queue_handle = xQueueCreate(_init_config.queue_size, sizeof(_queue_t));
|
_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)
|
if (_init_config.battery_mode == false)
|
||||||
{
|
{
|
||||||
ESP_LOGE(TAG, "ESP-NOW initialization fail. Internal error at line %d.", __LINE__);
|
if (esp_now_init() != ESP_OK || esp_now_register_send_cb(_send_cb) != ESP_OK || esp_now_register_recv_cb(_recv_cb) != ESP_OK)
|
||||||
return ESP_FAIL;
|
{
|
||||||
|
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)
|
else
|
||||||
|
{
|
||||||
|
if (esp_now_init() != ESP_OK || esp_now_register_send_cb(_send_cb) != ESP_OK)
|
||||||
|
{
|
||||||
|
ESP_LOGE(TAG, "ESP-NOW initialization fail. Internal error at line %d.", __LINE__);
|
||||||
|
return ESP_FAIL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (xTaskCreatePinnedToCore(&_processing, "zh_espnow_processing", _init_config.stack_size, NULL, _init_config.task_priority, &_processing_task_handle, tskNO_AFFINITY) != pdPASS)
|
||||||
{
|
{
|
||||||
ESP_LOGE(TAG, "ESP-NOW initialization fail. Internal error at line %d.", __LINE__);
|
ESP_LOGE(TAG, "ESP-NOW initialization fail. Internal error at line %d.", __LINE__);
|
||||||
return ESP_FAIL;
|
return ESP_FAIL;
|
||||||
@ -102,7 +117,10 @@ esp_err_t zh_espnow_deinit(void)
|
|||||||
vEventGroupDelete(_event_group_handle);
|
vEventGroupDelete(_event_group_handle);
|
||||||
vQueueDelete(_queue_handle);
|
vQueueDelete(_queue_handle);
|
||||||
esp_now_unregister_send_cb();
|
esp_now_unregister_send_cb();
|
||||||
esp_now_unregister_recv_cb();
|
if (_init_config.battery_mode == false)
|
||||||
|
{
|
||||||
|
esp_now_unregister_recv_cb();
|
||||||
|
}
|
||||||
esp_now_deinit();
|
esp_now_deinit();
|
||||||
vTaskDelete(_processing_task_handle);
|
vTaskDelete(_processing_task_handle);
|
||||||
_is_initialized = false;
|
_is_initialized = false;
|
||||||
@ -154,6 +172,11 @@ esp_err_t zh_espnow_send(const uint8_t *target, const uint8_t *data, const uint8
|
|||||||
}
|
}
|
||||||
memcpy(queue.data.payload, data, data_len);
|
memcpy(queue.data.payload, data, data_len);
|
||||||
queue.data.payload_len = data_len;
|
queue.data.payload_len = data_len;
|
||||||
|
if (xQueueSend(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
|
||||||
|
{
|
||||||
|
ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__);
|
||||||
|
return ESP_FAIL;
|
||||||
|
}
|
||||||
if (target == NULL)
|
if (target == NULL)
|
||||||
{
|
{
|
||||||
ESP_LOGI(TAG, "Adding outgoing ESP-NOW data to MAC FF:FF:FF:FF:FF:FF to queue success.");
|
ESP_LOGI(TAG, "Adding outgoing ESP-NOW data to MAC FF:FF:FF:FF:FF:FF to queue success.");
|
||||||
@ -162,11 +185,6 @@ esp_err_t zh_espnow_send(const uint8_t *target, const uint8_t *data, const uint8
|
|||||||
{
|
{
|
||||||
ESP_LOGI(TAG, "Adding outgoing ESP-NOW data to MAC %02X:%02X:%02X:%02X:%02X:%02X to queue success.", MAC2STR(target));
|
ESP_LOGI(TAG, "Adding outgoing ESP-NOW data to MAC %02X:%02X:%02X:%02X:%02X:%02X to queue success.", MAC2STR(target));
|
||||||
}
|
}
|
||||||
if (xQueueSend(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
|
|
||||||
{
|
|
||||||
ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__);
|
|
||||||
return ESP_FAIL;
|
|
||||||
}
|
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -213,15 +231,16 @@ static void IRAM_ATTR _recv_cb(const esp_now_recv_info_t *esp_now_info, const ui
|
|||||||
}
|
}
|
||||||
memcpy(queue.data.payload, data, data_len);
|
memcpy(queue.data.payload, data, data_len);
|
||||||
queue.data.payload_len = data_len;
|
queue.data.payload_len = data_len;
|
||||||
|
if (xQueueSend(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
|
||||||
|
{
|
||||||
|
ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__);
|
||||||
|
return;
|
||||||
|
}
|
||||||
#if defined CONFIG_IDF_TARGET_ESP8266 || ESP_IDF_VERSION_MAJOR == 4
|
#if defined CONFIG_IDF_TARGET_ESP8266 || ESP_IDF_VERSION_MAJOR == 4
|
||||||
ESP_LOGI(TAG, "Adding incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to queue success.", MAC2STR(mac_addr));
|
ESP_LOGI(TAG, "Adding incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to queue success.", MAC2STR(mac_addr));
|
||||||
#else
|
#else
|
||||||
ESP_LOGI(TAG, "Adding incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to queue success.", MAC2STR(esp_now_info->src_addr));
|
ESP_LOGI(TAG, "Adding incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X to queue success.", MAC2STR(esp_now_info->src_addr));
|
||||||
#endif
|
#endif
|
||||||
if (xQueueSend(_queue_handle, &queue, portTICK_PERIOD_MS) != pdTRUE)
|
|
||||||
{
|
|
||||||
ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void IRAM_ATTR _processing(void *pvParameter)
|
static void IRAM_ATTR _processing(void *pvParameter)
|
||||||
@ -309,11 +328,14 @@ static void IRAM_ATTR _processing(void *pvParameter)
|
|||||||
on_send->status = ZH_ESPNOW_SEND_FAIL;
|
on_send->status = ZH_ESPNOW_SEND_FAIL;
|
||||||
_attempts = 0;
|
_attempts = 0;
|
||||||
}
|
}
|
||||||
ESP_LOGI(TAG, "Outgoing ESP-NOW data to MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.mac_addr));
|
|
||||||
if (esp_event_post(ZH_ESPNOW, ZH_ESPNOW_ON_SEND_EVENT, on_send, sizeof(zh_espnow_event_on_send_t), portTICK_PERIOD_MS) != ESP_OK)
|
if (esp_event_post(ZH_ESPNOW, ZH_ESPNOW_ON_SEND_EVENT, on_send, sizeof(zh_espnow_event_on_send_t), portTICK_PERIOD_MS) != ESP_OK)
|
||||||
{
|
{
|
||||||
ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__);
|
ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ESP_LOGI(TAG, "Outgoing ESP-NOW data to MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.mac_addr));
|
||||||
|
}
|
||||||
heap_caps_free(queue.data.payload);
|
heap_caps_free(queue.data.payload);
|
||||||
esp_now_del_peer(peer->peer_addr);
|
esp_now_del_peer(peer->peer_addr);
|
||||||
heap_caps_free(peer);
|
heap_caps_free(peer);
|
||||||
@ -322,15 +344,25 @@ static void IRAM_ATTR _processing(void *pvParameter)
|
|||||||
case ON_RECV:
|
case ON_RECV:
|
||||||
ESP_LOGI(TAG, "Incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X processing begin.", MAC2STR(queue.data.mac_addr));
|
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;
|
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 + sizeof(recv_data->mac_addr) + sizeof(uint8_t), 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__);
|
ESP_LOGE(TAG, "ESP-NOW message processing task internal error at line %d.", __LINE__);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ESP_LOGI(TAG, "Incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X processed success.", MAC2STR(queue.data.mac_addr));
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
vTaskDelete(NULL);
|
vTaskDelete(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t zh_espnow_get_version(void)
|
||||||
|
{
|
||||||
|
uint32_t version = 0;
|
||||||
|
esp_now_get_version(&version);
|
||||||
|
return version;
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user