diff --git a/ESP32-IDF_ESPNOW-Transmitter/CMakeLists.txt b/ESP32-IDF_ESPNOW-Transmitter/CMakeLists.txt new file mode 100644 index 000000000..a8178d206 --- /dev/null +++ b/ESP32-IDF_ESPNOW-Transmitter/CMakeLists.txt @@ -0,0 +1,6 @@ +# The following lines of boilerplate have to be in your project's CMakeLists +# in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.16) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(espnow_example) diff --git a/ESP32-IDF_ESPNOW-Transmitter/README.md b/ESP32-IDF_ESPNOW-Transmitter/README.md new file mode 100644 index 000000000..c9880f6fb --- /dev/null +++ b/ESP32-IDF_ESPNOW-Transmitter/README.md @@ -0,0 +1,120 @@ +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | + +# ESPNOW Example + +(See the README.md file in the upper level 'examples' directory for more information about examples.) + +This example shows how to use ESPNOW of wifi. Example does the following steps: + +* Start WiFi. +* Initialize ESPNOW. +* Register ESPNOW sending or receiving callback function. +* Add ESPNOW peer information. +* Send and receive ESPNOW data. + +This example need at least two ESP devices: + +* In order to get the MAC address of the other device, Device1 firstly send broadcast ESPNOW data with 'state' set as 0. +* When Device2 receiving broadcast ESPNOW data from Device1 with 'state' as 0, adds Device1 into the peer list. + Then start sending broadcast ESPNOW data with 'state' set as 1. +* When Device1 receiving broadcast ESPNOW data with 'state' as 1, compares the local magic number with that in the data. + If the local one is bigger than that one, stop sending broadcast ESPNOW data and starts sending unicast ESPNOW data to Device2. +* If Device2 receives unicast ESPNOW data, also stop sending broadcast ESPNOW data. + +In practice, if the MAC address of the other device is known, it's not required to send/receive broadcast ESPNOW data first, +just add the device into the peer list and send/receive unicast ESPNOW data. + +There are a lot of "extras" on top of ESPNOW data, such as type, state, sequence number, CRC and magic in this example. These "extras" are +not required to use ESPNOW. They are only used to make this example to run correctly. However, it is recommended that users add some "extras" +to make ESPNOW data more safe and more reliable. + +## How to use example + +### Configure the project + +``` +idf.py menuconfig +``` + +* Set WiFi mode (station or SoftAP) under Example Configuration Options. +* Set ESPNOW primary master key under Example Configuration Options. + This parameter must be set to the same value for sending and recving devices. +* Set ESPNOW local master key under Example Configuration Options. + This parameter must be set to the same value for sending and recving devices. +* Set Channel under Example Configuration Options. + The sending device and the recving device must be on the same channel. +* Set Send count and Send delay under Example Configuration Options. +* Set Send len under Example Configuration Options. +* Set Enable Long Range Options. + When this parameter is enabled, the ESP32 device will send data at the PHY rate of 512Kbps or 256Kbps + then the data can be transmitted over long range between two ESP32 devices. + +### Build and Flash + +Build the project and flash it to the board, then run monitor tool to view serial output: + +``` +idf.py -p PORT flash monitor +``` + +(To exit the serial monitor, type ``Ctrl-]``.) + +See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects. + +## Example Output + +Here is the example of ESPNOW receiving device console output. + +``` +I (898) phy: phy_version: 3960, 5211945, Jul 18 2018, 10:40:07, 0, 0 +I (898) wifi: mode : sta (30:ae:a4:80:45:68) +I (898) espnow_example: WiFi started +I (898) ESPNOW: espnow [version: 1.0] init +I (5908) espnow_example: Start sending broadcast data +I (6908) espnow_example: send data to ff:ff:ff:ff:ff:ff +I (7908) espnow_example: send data to ff:ff:ff:ff:ff:ff +I (52138) espnow_example: send data to ff:ff:ff:ff:ff:ff +I (52138) espnow_example: Receive 0th broadcast data from: 30:ae:a4:0c:34:ec, len: 200 +I (53158) espnow_example: send data to ff:ff:ff:ff:ff:ff +I (53158) espnow_example: Receive 1th broadcast data from: 30:ae:a4:0c:34:ec, len: 200 +I (54168) espnow_example: send data to ff:ff:ff:ff:ff:ff +I (54168) espnow_example: Receive 2th broadcast data from: 30:ae:a4:0c:34:ec, len: 200 +I (54168) espnow_example: Receive 0th unicast data from: 30:ae:a4:0c:34:ec, len: 200 +I (54678) espnow_example: Receive 1th unicast data from: 30:ae:a4:0c:34:ec, len: 200 +I (55668) espnow_example: Receive 2th unicast data from: 30:ae:a4:0c:34:ec, len: 200 +``` + +Here is the example of ESPNOW sending device console output. + +``` +I (915) phy: phy_version: 3960, 5211945, Jul 18 2018, 10:40:07, 0, 0 +I (915) wifi: mode : sta (30:ae:a4:0c:34:ec) +I (915) espnow_example: WiFi started +I (915) ESPNOW: espnow [version: 1.0] init +I (5915) espnow_example: Start sending broadcast data +I (5915) espnow_example: Receive 41th broadcast data from: 30:ae:a4:80:45:68, len: 200 +I (5915) espnow_example: Receive 42th broadcast data from: 30:ae:a4:80:45:68, len: 200 +I (5925) espnow_example: Receive 44th broadcast data from: 30:ae:a4:80:45:68, len: 200 +I (5935) espnow_example: Receive 45th broadcast data from: 30:ae:a4:80:45:68, len: 200 +I (6965) espnow_example: send data to ff:ff:ff:ff:ff:ff +I (6965) espnow_example: Receive 46th broadcast data from: 30:ae:a4:80:45:68, len: 200 +I (7975) espnow_example: send data to ff:ff:ff:ff:ff:ff +I (7975) espnow_example: Receive 47th broadcast data from: 30:ae:a4:80:45:68, len: 200 +I (7975) espnow_example: Start sending unicast data +I (7975) espnow_example: send data to 30:ae:a4:80:45:68 +I (9015) espnow_example: send data to 30:ae:a4:80:45:68 +I (9015) espnow_example: Receive 48th broadcast data from: 30:ae:a4:80:45:68, len: 200 +I (10015) espnow_example: send data to 30:ae:a4:80:45:68 +I (16075) espnow_example: send data to 30:ae:a4:80:45:68 +I (17075) espnow_example: send data to 30:ae:a4:80:45:68 +I (24125) espnow_example: send data to 30:ae:a4:80:45:68 +``` + +## Troubleshooting + +If ESPNOW data can not be received from another device, maybe the two devices are not +on the same channel or the primary key and local key are different. + +In real application, if the receiving device is in station mode only and it connects to an AP, +modem sleep should be disabled. Otherwise, it may fail to revceive ESPNOW data from other devices. diff --git a/ESP32-IDF_ESPNOW-Transmitter/main/CMakeLists.txt b/ESP32-IDF_ESPNOW-Transmitter/main/CMakeLists.txt new file mode 100644 index 000000000..3aef7036c --- /dev/null +++ b/ESP32-IDF_ESPNOW-Transmitter/main/CMakeLists.txt @@ -0,0 +1,2 @@ +idf_component_register(SRCS "espnow_example_main.c" + INCLUDE_DIRS ".") diff --git a/ESP32-IDF_ESPNOW-Transmitter/main/Kconfig.projbuild b/ESP32-IDF_ESPNOW-Transmitter/main/Kconfig.projbuild new file mode 100644 index 000000000..459e567cc --- /dev/null +++ b/ESP32-IDF_ESPNOW-Transmitter/main/Kconfig.projbuild @@ -0,0 +1,86 @@ +menu "Example Configuration" + + choice ESPNOW_WIFI_MODE + prompt "WiFi mode" + default ESPNOW_WIFI_MODE_STATION + help + WiFi mode(station or softap). + + config ESPNOW_WIFI_MODE_STATION + bool "Station" + config ESPNOW_WIFI_MODE_STATION_SOFTAP + bool "Softap" + endchoice + + config ESPNOW_PMK + string "ESPNOW primary master key" + default "pmk1234567890123" + help + ESPNOW primary master for the example to use. The length of ESPNOW primary master must be 16 bytes. + + config ESPNOW_LMK + string "ESPNOW local master key" + default "lmk1234567890123" + help + ESPNOW local master for the example to use. The length of ESPNOW local master must be 16 bytes. + + config ESPNOW_CHANNEL + int "Channel" + default 1 + range 0 14 + help + The channel on which sending and receiving ESPNOW data. + + config ESPNOW_SEND_COUNT + int "Send count" + default 100 + range 1 65535 + help + Total count of unicast ESPNOW data to be sent. + + config ESPNOW_SEND_DELAY + int "Send delay" + default 1000 + range 0 65535 + help + Delay between sending two ESPNOW data, unit: ms. + + config ESPNOW_SEND_LEN + int "Send len" + range 10 250 + default 10 + help + Length of ESPNOW data to be sent, unit: byte. + + config ESPNOW_ENABLE_LONG_RANGE + bool "Enable Long Range" + default "n" + help + When enable long range, the PHY rate of ESP32 will be 512Kbps or 256Kbps + + config ESPNOW_ENABLE_POWER_SAVE + bool "Enable ESPNOW Power Save" + default "n" + select ESP_WIFI_STA_DISCONNECTED_PM_ENABLE + depends on ESPNOW_WIFI_MODE_STATION + help + With ESPNOW power save enabled, chip would be able to wakeup and sleep periodically + Notice ESP_WIFI_STA_DISCONNECTED_PM_ENABLE is essential at Wi-Fi disconnected + + config ESPNOW_WAKE_WINDOW + int "ESPNOW wake window, unit in millisecond" + range 0 65535 + default 50 + depends on ESPNOW_ENABLE_POWER_SAVE + help + ESPNOW wake window + + config ESPNOW_WAKE_INTERVAL + int "ESPNOW wake interval, unit in millisecond" + range 1 65535 + default 100 + depends on ESPNOW_ENABLE_POWER_SAVE + help + ESPNOW wake interval + +endmenu diff --git a/ESP32-IDF_ESPNOW-Transmitter/main/espnow_example.h b/ESP32-IDF_ESPNOW-Transmitter/main/espnow_example.h new file mode 100644 index 000000000..2de4f4a00 --- /dev/null +++ b/ESP32-IDF_ESPNOW-Transmitter/main/espnow_example.h @@ -0,0 +1,82 @@ +/* ESPNOW Example + + This example code is in the Public Domain (or CC0 licensed, at your option.) + + Unless required by applicable law or agreed to in writing, this + software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + CONDITIONS OF ANY KIND, either express or implied. +*/ + +#ifndef ESPNOW_EXAMPLE_H +#define ESPNOW_EXAMPLE_H + +/* ESPNOW can work in both station and softap mode. It is configured in menuconfig. */ +#if CONFIG_ESPNOW_WIFI_MODE_STATION +#define ESPNOW_WIFI_MODE WIFI_MODE_STA +#define ESPNOW_WIFI_IF ESP_IF_WIFI_STA +#else +#define ESPNOW_WIFI_MODE WIFI_MODE_AP +#define ESPNOW_WIFI_IF ESP_IF_WIFI_AP +#endif + +#define ESPNOW_QUEUE_SIZE 6 + +#define IS_BROADCAST_ADDR(addr) (memcmp(addr, s_example_broadcast_mac, ESP_NOW_ETH_ALEN) == 0) + +typedef enum { + EXAMPLE_ESPNOW_SEND_CB, + EXAMPLE_ESPNOW_RECV_CB, +} example_espnow_event_id_t; + +typedef struct { + uint8_t mac_addr[ESP_NOW_ETH_ALEN]; + esp_now_send_status_t status; +} example_espnow_event_send_cb_t; + +typedef struct { + uint8_t mac_addr[ESP_NOW_ETH_ALEN]; + uint8_t *data; + int data_len; +} example_espnow_event_recv_cb_t; + +typedef union { + example_espnow_event_send_cb_t send_cb; + example_espnow_event_recv_cb_t recv_cb; +} example_espnow_event_info_t; + +/* When ESPNOW sending or receiving callback function is called, post event to ESPNOW task. */ +typedef struct { + example_espnow_event_id_t id; + example_espnow_event_info_t info; +} example_espnow_event_t; + +enum { + EXAMPLE_ESPNOW_DATA_BROADCAST, + EXAMPLE_ESPNOW_DATA_UNICAST, + EXAMPLE_ESPNOW_DATA_MAX, +}; + +/* User defined field of ESPNOW data in this example. */ +typedef struct { + uint8_t type; //Broadcast or unicast ESPNOW data. + uint8_t state; //Indicate that if has received broadcast ESPNOW data or not. + uint16_t seq_num; //Sequence number of ESPNOW data. + uint16_t crc; //CRC16 value of ESPNOW data. + uint32_t magic; //Magic number which is used to determine which device to send unicast ESPNOW data. + uint8_t payload[0]; //Real payload of ESPNOW data. +} __attribute__((packed)) example_espnow_data_t; + +/* Parameters of sending ESPNOW data. */ +typedef struct { + bool unicast; //Send unicast ESPNOW data. + bool broadcast; //Send broadcast ESPNOW data. + uint8_t state; //Indicate that if has received broadcast ESPNOW data or not. + uint32_t magic; //Magic number which is used to determine which device to send unicast ESPNOW data. + uint16_t count; //Total count of unicast ESPNOW data to be sent. + uint16_t delay; //Delay between sending two ESPNOW data, unit: ms. + int len; //Length of ESPNOW data to be sent, unit: byte. + uint8_t *buffer; //Buffer pointing to ESPNOW data. + uint8_t dest_mac[ESP_NOW_ETH_ALEN]; //MAC address of destination device. +} example_espnow_send_param_t; + +#endif diff --git a/ESP32-IDF_ESPNOW-Transmitter/main/espnow_example_main.c b/ESP32-IDF_ESPNOW-Transmitter/main/espnow_example_main.c new file mode 100644 index 000000000..98d4d4769 --- /dev/null +++ b/ESP32-IDF_ESPNOW-Transmitter/main/espnow_example_main.c @@ -0,0 +1,392 @@ +/* ESPNOW Example + + This example code is in the Public Domain (or CC0 licensed, at your option.) + + Unless required by applicable law or agreed to in writing, this + software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + CONDITIONS OF ANY KIND, either express or implied. +*/ + +/* + This example shows how to use ESPNOW. + Prepare two device, one for sending ESPNOW data and another for receiving + ESPNOW data. +*/ +#include +#include +#include +#include +#include "freertos/FreeRTOS.h" +#include "freertos/semphr.h" +#include "freertos/timers.h" +#include "nvs_flash.h" +#include "esp_random.h" +#include "esp_event.h" +#include "esp_netif.h" +#include "esp_wifi.h" +#include "esp_log.h" +#include "esp_mac.h" +#include "esp_now.h" +#include "esp_crc.h" +#include "espnow_example.h" + +#define ESPNOW_MAXDELAY 512 + +static const char *TAG = "espnow_example"; + +static QueueHandle_t s_example_espnow_queue = NULL; + +static uint8_t s_example_broadcast_mac[ESP_NOW_ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; +static uint16_t s_example_espnow_seq[EXAMPLE_ESPNOW_DATA_MAX] = { 0, 0 }; + +static void example_espnow_deinit(example_espnow_send_param_t *send_param); + +/* WiFi should start before using ESPNOW */ +static void example_wifi_init(void) +{ + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK( esp_wifi_init(&cfg) ); + ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) ); + ESP_ERROR_CHECK( esp_wifi_set_mode(ESPNOW_WIFI_MODE) ); + ESP_ERROR_CHECK( esp_wifi_start()); + ESP_ERROR_CHECK( esp_wifi_set_channel(CONFIG_ESPNOW_CHANNEL, WIFI_SECOND_CHAN_NONE)); + +#if CONFIG_ESPNOW_ENABLE_LONG_RANGE + ESP_ERROR_CHECK( esp_wifi_set_protocol(ESPNOW_WIFI_IF, WIFI_PROTOCOL_11B|WIFI_PROTOCOL_11G|WIFI_PROTOCOL_11N|WIFI_PROTOCOL_LR) ); +#endif +} + +/* ESPNOW sending or receiving callback function is called in WiFi task. + * Users should not do lengthy operations from this task. Instead, post + * necessary data to a queue and handle it from a lower priority task. */ +static void example_espnow_send_cb(const uint8_t *mac_addr, esp_now_send_status_t status) +{ + example_espnow_event_t evt; + example_espnow_event_send_cb_t *send_cb = &evt.info.send_cb; + + if (mac_addr == NULL) { + ESP_LOGE(TAG, "Send cb arg error"); + return; + } + + evt.id = EXAMPLE_ESPNOW_SEND_CB; + memcpy(send_cb->mac_addr, mac_addr, ESP_NOW_ETH_ALEN); + send_cb->status = status; + if (xQueueSend(s_example_espnow_queue, &evt, ESPNOW_MAXDELAY) != pdTRUE) { + ESP_LOGW(TAG, "Send send queue fail"); + } +} + +static void example_espnow_recv_cb(const esp_now_recv_info_t *recv_info, const uint8_t *data, int len) +{ + example_espnow_event_t evt; + example_espnow_event_recv_cb_t *recv_cb = &evt.info.recv_cb; + uint8_t * mac_addr = recv_info->src_addr; + uint8_t * des_addr = recv_info->des_addr; + + if (mac_addr == NULL || data == NULL || len <= 0) { + ESP_LOGE(TAG, "Receive cb arg error"); + return; + } + + if (IS_BROADCAST_ADDR(des_addr)) { + /* If added a peer with encryption before, the receive packets may be + * encrypted as peer-to-peer message or unencrypted over the broadcast channel. + * Users can check the destination address to distinguish it. + */ + ESP_LOGD(TAG, "Receive broadcast ESPNOW data"); + } else { + ESP_LOGD(TAG, "Receive unicast ESPNOW data"); + } + + evt.id = EXAMPLE_ESPNOW_RECV_CB; + memcpy(recv_cb->mac_addr, mac_addr, ESP_NOW_ETH_ALEN); + recv_cb->data = malloc(len); + if (recv_cb->data == NULL) { + ESP_LOGE(TAG, "Malloc receive data fail"); + return; + } + memcpy(recv_cb->data, data, len); + recv_cb->data_len = len; + if (xQueueSend(s_example_espnow_queue, &evt, ESPNOW_MAXDELAY) != pdTRUE) { + ESP_LOGW(TAG, "Send receive queue fail"); + free(recv_cb->data); + } +} + +/* Parse received ESPNOW data. */ +int example_espnow_data_parse(uint8_t *data, uint16_t data_len, uint8_t *state, uint16_t *seq, uint32_t *magic) +{ + example_espnow_data_t *buf = (example_espnow_data_t *)data; + uint16_t crc, crc_cal = 0; + + if (data_len < sizeof(example_espnow_data_t)) { + ESP_LOGE(TAG, "Receive ESPNOW data too short, len:%d", data_len); + return -1; + } + + *state = buf->state; + *seq = buf->seq_num; + *magic = buf->magic; + crc = buf->crc; + buf->crc = 0; + crc_cal = esp_crc16_le(UINT16_MAX, (uint8_t const *)buf, data_len); + + if (crc_cal == crc) { + return buf->type; + } + + return -1; +} + +/* Prepare ESPNOW data to be sent. */ +void example_espnow_data_prepare(example_espnow_send_param_t *send_param) +{ + example_espnow_data_t *buf = (example_espnow_data_t *)send_param->buffer; + + assert(send_param->len >= sizeof(example_espnow_data_t)); + + buf->type = IS_BROADCAST_ADDR(send_param->dest_mac) ? EXAMPLE_ESPNOW_DATA_BROADCAST : EXAMPLE_ESPNOW_DATA_UNICAST; + buf->state = send_param->state; + buf->seq_num = s_example_espnow_seq[buf->type]++; + buf->crc = 0; + buf->magic = send_param->magic; + /* Fill all remaining bytes after the data with random values */ + esp_fill_random(buf->payload, send_param->len - sizeof(example_espnow_data_t)); + buf->crc = esp_crc16_le(UINT16_MAX, (uint8_t const *)buf, send_param->len); +} + +static void example_espnow_task(void *pvParameter) +{ + example_espnow_event_t evt; + uint8_t recv_state = 0; + uint16_t recv_seq = 0; + uint32_t recv_magic = 0; + bool is_broadcast = false; + int ret; + + vTaskDelay(5000 / portTICK_PERIOD_MS); + ESP_LOGI(TAG, "Start sending broadcast data"); + + /* Start sending broadcast ESPNOW data. */ + example_espnow_send_param_t *send_param = (example_espnow_send_param_t *)pvParameter; + if (esp_now_send(send_param->dest_mac, send_param->buffer, send_param->len) != ESP_OK) { + ESP_LOGE(TAG, "Send error"); + example_espnow_deinit(send_param); + vTaskDelete(NULL); + } + + while (xQueueReceive(s_example_espnow_queue, &evt, portMAX_DELAY) == pdTRUE) { + switch (evt.id) { + case EXAMPLE_ESPNOW_SEND_CB: + { + example_espnow_event_send_cb_t *send_cb = &evt.info.send_cb; + is_broadcast = IS_BROADCAST_ADDR(send_cb->mac_addr); + + ESP_LOGD(TAG, "Send data to "MACSTR", status1: %d", MAC2STR(send_cb->mac_addr), send_cb->status); + + if (is_broadcast && (send_param->broadcast == false)) { + break; + } + + if (!is_broadcast) { + send_param->count--; + if (send_param->count == 0) { + ESP_LOGI(TAG, "Send done"); + example_espnow_deinit(send_param); + vTaskDelete(NULL); + } + } + + /* Delay a while before sending the next data. */ + if (send_param->delay > 0) { + vTaskDelay(send_param->delay/portTICK_PERIOD_MS); + } + + ESP_LOGI(TAG, "send data to "MACSTR"", MAC2STR(send_cb->mac_addr)); + + memcpy(send_param->dest_mac, send_cb->mac_addr, ESP_NOW_ETH_ALEN); + example_espnow_data_prepare(send_param); + + /* Send the next data after the previous data is sent. */ + if (esp_now_send(send_param->dest_mac, send_param->buffer, send_param->len) != ESP_OK) { + ESP_LOGE(TAG, "Send error"); + example_espnow_deinit(send_param); + vTaskDelete(NULL); + } + break; + } + case EXAMPLE_ESPNOW_RECV_CB: + { + example_espnow_event_recv_cb_t *recv_cb = &evt.info.recv_cb; + + ret = example_espnow_data_parse(recv_cb->data, recv_cb->data_len, &recv_state, &recv_seq, &recv_magic); + free(recv_cb->data); + if (ret == EXAMPLE_ESPNOW_DATA_BROADCAST) { + ESP_LOGI(TAG, "Receive %dth broadcast data from: "MACSTR", len: %d", recv_seq, MAC2STR(recv_cb->mac_addr), recv_cb->data_len); + + /* If MAC address does not exist in peer list, add it to peer list. */ + if (esp_now_is_peer_exist(recv_cb->mac_addr) == false) { + esp_now_peer_info_t *peer = malloc(sizeof(esp_now_peer_info_t)); + if (peer == NULL) { + ESP_LOGE(TAG, "Malloc peer information fail"); + example_espnow_deinit(send_param); + vTaskDelete(NULL); + } + memset(peer, 0, sizeof(esp_now_peer_info_t)); + peer->channel = CONFIG_ESPNOW_CHANNEL; + peer->ifidx = ESPNOW_WIFI_IF; + peer->encrypt = true; + memcpy(peer->lmk, CONFIG_ESPNOW_LMK, ESP_NOW_KEY_LEN); + memcpy(peer->peer_addr, recv_cb->mac_addr, ESP_NOW_ETH_ALEN); + ESP_ERROR_CHECK( esp_now_add_peer(peer) ); + free(peer); + } + + /* Indicates that the device has received broadcast ESPNOW data. */ + if (send_param->state == 0) { + send_param->state = 1; + } + + /* If receive broadcast ESPNOW data which indicates that the other device has received + * broadcast ESPNOW data and the local magic number is bigger than that in the received + * broadcast ESPNOW data, stop sending broadcast ESPNOW data and start sending unicast + * ESPNOW data. + */ + if (recv_state == 1) { + /* The device which has the bigger magic number sends ESPNOW data, the other one + * receives ESPNOW data. + */ + if (send_param->unicast == false && send_param->magic >= recv_magic) { + ESP_LOGI(TAG, "Start sending unicast data"); + ESP_LOGI(TAG, "send data to "MACSTR"", MAC2STR(recv_cb->mac_addr)); + + /* Start sending unicast ESPNOW data. */ + memcpy(send_param->dest_mac, recv_cb->mac_addr, ESP_NOW_ETH_ALEN); + example_espnow_data_prepare(send_param); + if (esp_now_send(send_param->dest_mac, send_param->buffer, send_param->len) != ESP_OK) { + ESP_LOGE(TAG, "Send error"); + example_espnow_deinit(send_param); + vTaskDelete(NULL); + } + else { + send_param->broadcast = false; + send_param->unicast = true; + } + } + } + } + else if (ret == EXAMPLE_ESPNOW_DATA_UNICAST) { + ESP_LOGI(TAG, "Receive %dth unicast data from: "MACSTR", len: %d", recv_seq, MAC2STR(recv_cb->mac_addr), recv_cb->data_len); + + /* If receive unicast ESPNOW data, also stop sending broadcast ESPNOW data. */ + send_param->broadcast = false; + } + else { + ESP_LOGI(TAG, "Receive error data from: "MACSTR"", MAC2STR(recv_cb->mac_addr)); + } + break; + } + default: + ESP_LOGE(TAG, "Callback type error: %d", evt.id); + break; + } + } +} + +static esp_err_t example_espnow_init(void) +{ + example_espnow_send_param_t *send_param; + + s_example_espnow_queue = xQueueCreate(ESPNOW_QUEUE_SIZE, sizeof(example_espnow_event_t)); + if (s_example_espnow_queue == NULL) { + ESP_LOGE(TAG, "Create queue fail"); + return ESP_FAIL; + } + + /* Initialize ESPNOW and register sending and receiving callback function. */ + ESP_ERROR_CHECK( esp_now_init() ); + ESP_ERROR_CHECK( esp_now_register_send_cb(example_espnow_send_cb) ); + ESP_ERROR_CHECK( esp_now_register_recv_cb(example_espnow_recv_cb) ); +#if CONFIG_ESPNOW_ENABLE_POWER_SAVE + ESP_ERROR_CHECK( esp_now_set_wake_window(CONFIG_ESPNOW_WAKE_WINDOW) ); + ESP_ERROR_CHECK( esp_wifi_connectionless_module_set_wake_interval(CONFIG_ESPNOW_WAKE_INTERVAL) ); +#endif + /* Set primary master key. */ + ESP_ERROR_CHECK( esp_now_set_pmk((uint8_t *)CONFIG_ESPNOW_PMK) ); + + /* Add broadcast peer information to peer list. */ + esp_now_peer_info_t *peer = malloc(sizeof(esp_now_peer_info_t)); + if (peer == NULL) { + ESP_LOGE(TAG, "Malloc peer information fail"); + vQueueDelete(s_example_espnow_queue); + s_example_espnow_queue = NULL; + esp_now_deinit(); + return ESP_FAIL; + } + memset(peer, 0, sizeof(esp_now_peer_info_t)); + peer->channel = CONFIG_ESPNOW_CHANNEL; + peer->ifidx = ESPNOW_WIFI_IF; + peer->encrypt = false; + memcpy(peer->peer_addr, s_example_broadcast_mac, ESP_NOW_ETH_ALEN); + ESP_ERROR_CHECK( esp_now_add_peer(peer) ); + free(peer); + + /* Initialize sending parameters. */ + send_param = malloc(sizeof(example_espnow_send_param_t)); + if (send_param == NULL) { + ESP_LOGE(TAG, "Malloc send parameter fail"); + vQueueDelete(s_example_espnow_queue); + s_example_espnow_queue = NULL; + esp_now_deinit(); + return ESP_FAIL; + } + memset(send_param, 0, sizeof(example_espnow_send_param_t)); + send_param->unicast = false; + send_param->broadcast = true; + send_param->state = 0; + send_param->magic = esp_random(); + send_param->count = CONFIG_ESPNOW_SEND_COUNT; + send_param->delay = CONFIG_ESPNOW_SEND_DELAY; + send_param->len = CONFIG_ESPNOW_SEND_LEN; + send_param->buffer = malloc(CONFIG_ESPNOW_SEND_LEN); + if (send_param->buffer == NULL) { + ESP_LOGE(TAG, "Malloc send buffer fail"); + free(send_param); + vQueueDelete(s_example_espnow_queue); + s_example_espnow_queue = NULL; + esp_now_deinit(); + return ESP_FAIL; + } + memcpy(send_param->dest_mac, s_example_broadcast_mac, ESP_NOW_ETH_ALEN); + example_espnow_data_prepare(send_param); + + xTaskCreate(example_espnow_task, "example_espnow_task", 2048, send_param, 4, NULL); + + return ESP_OK; +} + +static void example_espnow_deinit(example_espnow_send_param_t *send_param) +{ + free(send_param->buffer); + free(send_param); + vQueueDelete(s_example_espnow_queue); + s_example_espnow_queue = NULL; + esp_now_deinit(); +} + +void app_main(void) +{ + // Initialize NVS + esp_err_t ret = nvs_flash_init(); + if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { + ESP_ERROR_CHECK( nvs_flash_erase() ); + ret = nvs_flash_init(); + } + ESP_ERROR_CHECK( ret ); + + example_wifi_init(); + example_espnow_init(); +}