10 Commits

5 changed files with 152 additions and 5 deletions

View File

@ -87,6 +87,7 @@ void app_main(void)
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()); printf("Used ESP-NOW version %d.\n", zh_espnow_get_version());
printf("ESP-NOW channel %d. \n", zh_espnow_get_channel());
uint8_t counter = 0; uint8_t counter = 0;
for (;;) for (;;)
{ {

View File

@ -32,7 +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 10. uint8_t attempts; // Maximum number of attempts to send a message. @note It is not recommended to set a value greater than 10.
bool battery_mode; // Battery operation mode. If true, the node does not receive messages. 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);
@ -100,7 +100,7 @@ extern "C"
* *
* @return ESP_OK if success or an error code otherwise. * @return ESP_OK if success or an error code otherwise.
*/ */
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 uint16_t data_len);
/** /**
* @brief Get ESP-NOW version. * @brief Get ESP-NOW version.
@ -116,6 +116,68 @@ extern "C"
*/ */
const zh_espnow_stats_t *zh_espnow_get_stats(void); const zh_espnow_stats_t *zh_espnow_get_stats(void);
/**
* @brief Reset ESP-NOW statistics.
*/
void zh_espnow_reset_stats(void);
/**
* @brief Check ESP-NOW initialization status.
*
* @return True if ESP-NOW is initialized false otherwise.
*/
bool zh_espnow_is_initialized(void);
/**
* @brief Get number of attempts.
*
* @return Attemps number.
*/
uint8_t zh_espnow_get_attempts(void);
/**
* @brief Set number of attempts.
*
* @param[in] attempts Attemps number.
*
* @return ESP_OK if success or an error code otherwise.
*/
esp_err_t zh_espnow_set_attempts(uint8_t attempts);
/**
* @brief Get ESP-NOW channel.
*
* @return ESP-NOW channel if success or 0 otherwise.
*/
uint8_t zh_espnow_get_channel(void);
/**
* @brief Set ESP-NOW channel.
*
* @param[in] channel ESP-NOW channel (1-14).
*
* @return ESP_OK if success or an error code otherwise.
*/
esp_err_t zh_espnow_set_channel(uint8_t channel);
/**
* @brief Get battery mode.
*
* @return True if battery mode set false otherwise.
*/
bool zh_espnow_get_battery_mode(void);
/**
* @brief Set battery mode.
*
* @param[in] battery_mode True to enable the mode false to disable it.
*
* @note If true the node does not receive messages.
*
* @return ESP_OK if success or an error code otherwise.
*/
esp_err_t zh_espnow_set_battery_mode(bool battery_mode);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

0
main.c
View File

View File

@ -1 +1 @@
1.1.0 1.6.0

View File

@ -215,7 +215,7 @@ esp_err_t zh_espnow_deinit(void)
return final_status; return final_status;
} }
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 uint16_t data_len)
{ {
ZH_ESPNOW_LOGI("Adding to queue outgoing ESP-NOW data to MAC %02X:%02X:%02X:%02X:%02X:%02X started.", MAC2STR((target == NULL) ? _broadcast_mac : target)); ZH_ESPNOW_LOGI("Adding to queue outgoing ESP-NOW data to MAC %02X:%02X:%02X:%02X:%02X:%02X started.", MAC2STR((target == NULL) ? _broadcast_mac : target));
if (_is_initialized == false) if (_is_initialized == false)
@ -459,7 +459,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_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; zh_espnow_event_on_recv_t *recv_data = (zh_espnow_event_on_recv_t *)&queue->data;
++_stats.received; ++_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); esp_err_t err = esp_event_post(ZH_ESPNOW, ZH_ESPNOW_ON_RECV_EVENT, recv_data, sizeof(zh_espnow_event_on_recv_t), portTICK_PERIOD_MS);
if (err == ESP_OK) if (err == ESP_OK)
{ {
ZH_ESPNOW_LOGI("Incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X processed successfully.", MAC2STR(queue->data.mac_addr)); ZH_ESPNOW_LOGI("Incoming ESP-NOW data from MAC %02X:%02X:%02X:%02X:%02X:%02X processed successfully.", MAC2STR(queue->data.mac_addr));
@ -508,4 +508,88 @@ uint8_t zh_espnow_get_version(void)
const zh_espnow_stats_t *zh_espnow_get_stats(void) const zh_espnow_stats_t *zh_espnow_get_stats(void)
{ {
return &_stats; return &_stats;
}
void zh_espnow_reset_stats(void)
{
_stats.sent_success = 0;
_stats.sent_fail = 0;
_stats.received = 0;
ZH_ESPNOW_LOGI("ESP-NOW statistic reset successfully.");
}
bool zh_espnow_is_initialized(void)
{
return _is_initialized;
}
uint8_t zh_espnow_get_attempts(void)
{
return _init_config.attempts;
}
esp_err_t zh_espnow_set_attempts(uint8_t attempts)
{
ZH_ESPNOW_CHECK(_is_initialized == true, ESP_ERR_INVALID_STATE, "Number of attempts set failed. ESP-NOW is not initialized.");
ZH_ESPNOW_CHECK(attempts > 0, ESP_ERR_INVALID_ARG, "Number of attempts set failed. Invalid number.");
_init_config.attempts = attempts;
ZH_ESPNOW_LOGI("Number of attempts set successfully.");
return ESP_OK;
}
uint8_t zh_espnow_get_channel(void)
{
if (_is_initialized == false)
{
ZH_ESPNOW_LOGE("ESP-NOW channel receiption failed. ESP-NOW is not initialized.");
return 0;
}
uint8_t prim_channel = 0;
wifi_second_chan_t sec_channel = 0;
esp_err_t err = esp_wifi_get_channel(&prim_channel, &sec_channel);
if (err != ESP_OK)
{
ZH_ESPNOW_LOGE_ERR("ESP-NOW channel receiption failed.", err);
return 0;
}
_init_config.wifi_channel = prim_channel;
ZH_ESPNOW_LOGI("ESP-NOW channel receiption successfully.");
return prim_channel;
}
esp_err_t zh_espnow_set_channel(uint8_t channel)
{
ZH_ESPNOW_CHECK(_is_initialized == true, ESP_ERR_INVALID_STATE, "ESP-NOW channel set failed. ESP-NOW is not initialized.");
ZH_ESPNOW_CHECK(channel > 0 && channel < 15, ESP_ERR_INVALID_ARG, "ESP-NOW channel set failed. Invalid channel.");
esp_err_t err = esp_wifi_set_channel(channel, WIFI_SECOND_CHAN_NONE);
if (err != ESP_OK)
{
ZH_ESPNOW_LOGE_ERR("ESP-NOW channel set failed.", err);
return err;
}
_init_config.wifi_channel = channel;
ZH_ESPNOW_LOGI("ESP-NOW channel set successfully.");
return err;
}
bool zh_espnow_get_battery_mode(void)
{
return _init_config.battery_mode;
}
esp_err_t zh_espnow_set_battery_mode(bool battery_mode)
{
ZH_ESPNOW_CHECK(_is_initialized == true, ESP_ERR_INVALID_STATE, "Battery mode set failed. ESP-NOW is not initialized.");
esp_err_t err = esp_now_unregister_send_cb();
ZH_ESPNOW_CHECK(err == ESP_OK, err, "Battery mode set failed. Failed to unregister send callback.");
if (_init_config.battery_mode == false)
{
err = esp_now_unregister_recv_cb();
ZH_ESPNOW_CHECK(err == ESP_OK, err, "Battery mode set failed. Failed to unregister receive callback.");
}
err = _zh_espnow_register_callbacks(battery_mode);
ZH_ESPNOW_CHECK(err == ESP_OK, err, "Battery mode set failed. Failed to register callbacks.");
_init_config.battery_mode = battery_mode;
ZH_ESPNOW_LOGI("Battery mode set successfully.");
return ESP_OK;
} }