1 /* ESPNOW Example
2
3 This example code is in the Public Domain (or CC0 licensed, at your option.)
4
5 Unless required by applicable law or agreed to in writing, this
6 software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
7 CONDITIONS OF ANY KIND, either express or implied.
8 */
9
10 /*
11 This example shows how to use ESPNOW.
12 Prepare two device, one for sending ESPNOW data and another for receiving
13 ESPNOW data.
14 */
15 #include <stdlib.h>
16 #include <time.h>
17 #include <string.h>
18 #include <assert.h>
19 #include "freertos/FreeRTOS.h"
20 #include "freertos/semphr.h"
21 #include "freertos/timers.h"
22 #include "nvs_flash.h"
23 #include "esp_event.h"
24 #include "esp_netif.h"
25 #include "esp_wifi.h"
26 #include "esp_log.h"
27 #include "esp_system.h"
28 #include "esp_now.h"
29 #include "esp_crc.h"
30 #include "espnow_example.h"
31
32 #define ESPNOW_MAXDELAY 512
33
34 static const char *TAG = "espnow_example";
35
36 static xQueueHandle s_example_espnow_queue;
37
38 static uint8_t s_example_broadcast_mac[ESP_NOW_ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
39 static uint16_t s_example_espnow_seq[EXAMPLE_ESPNOW_DATA_MAX] = { 0, 0 };
40
41 static void example_espnow_deinit(example_espnow_send_param_t *send_param);
42
43 /* WiFi should start before using ESPNOW */
example_wifi_init(void)44 static void example_wifi_init(void)
45 {
46 ESP_ERROR_CHECK(esp_netif_init());
47 ESP_ERROR_CHECK(esp_event_loop_create_default());
48 wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
49 ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
50 ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
51 ESP_ERROR_CHECK( esp_wifi_set_mode(ESPNOW_WIFI_MODE) );
52 ESP_ERROR_CHECK( esp_wifi_start());
53
54 #if CONFIG_ESPNOW_ENABLE_LONG_RANGE
55 ESP_ERROR_CHECK( esp_wifi_set_protocol(ESPNOW_WIFI_IF, WIFI_PROTOCOL_11B|WIFI_PROTOCOL_11G|WIFI_PROTOCOL_11N|WIFI_PROTOCOL_LR) );
56 #endif
57 }
58
59 /* ESPNOW sending or receiving callback function is called in WiFi task.
60 * Users should not do lengthy operations from this task. Instead, post
61 * necessary data to a queue and handle it from a lower priority task. */
example_espnow_send_cb(const uint8_t * mac_addr,esp_now_send_status_t status)62 static void example_espnow_send_cb(const uint8_t *mac_addr, esp_now_send_status_t status)
63 {
64 example_espnow_event_t evt;
65 example_espnow_event_send_cb_t *send_cb = &evt.info.send_cb;
66
67 if (mac_addr == NULL) {
68 ESP_LOGE(TAG, "Send cb arg error");
69 return;
70 }
71
72 evt.id = EXAMPLE_ESPNOW_SEND_CB;
73 memcpy(send_cb->mac_addr, mac_addr, ESP_NOW_ETH_ALEN);
74 send_cb->status = status;
75 if (xQueueSend(s_example_espnow_queue, &evt, ESPNOW_MAXDELAY) != pdTRUE) {
76 ESP_LOGW(TAG, "Send send queue fail");
77 }
78 }
79
example_espnow_recv_cb(const uint8_t * mac_addr,const uint8_t * data,int len)80 static void example_espnow_recv_cb(const uint8_t *mac_addr, const uint8_t *data, int len)
81 {
82 example_espnow_event_t evt;
83 example_espnow_event_recv_cb_t *recv_cb = &evt.info.recv_cb;
84
85 if (mac_addr == NULL || data == NULL || len <= 0) {
86 ESP_LOGE(TAG, "Receive cb arg error");
87 return;
88 }
89
90 evt.id = EXAMPLE_ESPNOW_RECV_CB;
91 memcpy(recv_cb->mac_addr, mac_addr, ESP_NOW_ETH_ALEN);
92 recv_cb->data = malloc(len);
93 if (recv_cb->data == NULL) {
94 ESP_LOGE(TAG, "Malloc receive data fail");
95 return;
96 }
97 memcpy(recv_cb->data, data, len);
98 recv_cb->data_len = len;
99 if (xQueueSend(s_example_espnow_queue, &evt, ESPNOW_MAXDELAY) != pdTRUE) {
100 ESP_LOGW(TAG, "Send receive queue fail");
101 free(recv_cb->data);
102 }
103 }
104
105 /* Parse received ESPNOW data. */
example_espnow_data_parse(uint8_t * data,uint16_t data_len,uint8_t * state,uint16_t * seq,int * magic)106 int example_espnow_data_parse(uint8_t *data, uint16_t data_len, uint8_t *state, uint16_t *seq, int *magic)
107 {
108 example_espnow_data_t *buf = (example_espnow_data_t *)data;
109 uint16_t crc, crc_cal = 0;
110
111 if (data_len < sizeof(example_espnow_data_t)) {
112 ESP_LOGE(TAG, "Receive ESPNOW data too short, len:%d", data_len);
113 return -1;
114 }
115
116 *state = buf->state;
117 *seq = buf->seq_num;
118 *magic = buf->magic;
119 crc = buf->crc;
120 buf->crc = 0;
121 crc_cal = esp_crc16_le(UINT16_MAX, (uint8_t const *)buf, data_len);
122
123 if (crc_cal == crc) {
124 return buf->type;
125 }
126
127 return -1;
128 }
129
130 /* Prepare ESPNOW data to be sent. */
example_espnow_data_prepare(example_espnow_send_param_t * send_param)131 void example_espnow_data_prepare(example_espnow_send_param_t *send_param)
132 {
133 example_espnow_data_t *buf = (example_espnow_data_t *)send_param->buffer;
134
135 assert(send_param->len >= sizeof(example_espnow_data_t));
136
137 buf->type = IS_BROADCAST_ADDR(send_param->dest_mac) ? EXAMPLE_ESPNOW_DATA_BROADCAST : EXAMPLE_ESPNOW_DATA_UNICAST;
138 buf->state = send_param->state;
139 buf->seq_num = s_example_espnow_seq[buf->type]++;
140 buf->crc = 0;
141 buf->magic = send_param->magic;
142 /* Fill all remaining bytes after the data with random values */
143 esp_fill_random(buf->payload, send_param->len - sizeof(example_espnow_data_t));
144 buf->crc = esp_crc16_le(UINT16_MAX, (uint8_t const *)buf, send_param->len);
145 }
146
example_espnow_task(void * pvParameter)147 static void example_espnow_task(void *pvParameter)
148 {
149 example_espnow_event_t evt;
150 uint8_t recv_state = 0;
151 uint16_t recv_seq = 0;
152 int recv_magic = 0;
153 bool is_broadcast = false;
154 int ret;
155
156 vTaskDelay(5000 / portTICK_RATE_MS);
157 ESP_LOGI(TAG, "Start sending broadcast data");
158
159 /* Start sending broadcast ESPNOW data. */
160 example_espnow_send_param_t *send_param = (example_espnow_send_param_t *)pvParameter;
161 if (esp_now_send(send_param->dest_mac, send_param->buffer, send_param->len) != ESP_OK) {
162 ESP_LOGE(TAG, "Send error");
163 example_espnow_deinit(send_param);
164 vTaskDelete(NULL);
165 }
166
167 while (xQueueReceive(s_example_espnow_queue, &evt, portMAX_DELAY) == pdTRUE) {
168 switch (evt.id) {
169 case EXAMPLE_ESPNOW_SEND_CB:
170 {
171 example_espnow_event_send_cb_t *send_cb = &evt.info.send_cb;
172 is_broadcast = IS_BROADCAST_ADDR(send_cb->mac_addr);
173
174 ESP_LOGD(TAG, "Send data to "MACSTR", status1: %d", MAC2STR(send_cb->mac_addr), send_cb->status);
175
176 if (is_broadcast && (send_param->broadcast == false)) {
177 break;
178 }
179
180 if (!is_broadcast) {
181 send_param->count--;
182 if (send_param->count == 0) {
183 ESP_LOGI(TAG, "Send done");
184 example_espnow_deinit(send_param);
185 vTaskDelete(NULL);
186 }
187 }
188
189 /* Delay a while before sending the next data. */
190 if (send_param->delay > 0) {
191 vTaskDelay(send_param->delay/portTICK_RATE_MS);
192 }
193
194 ESP_LOGI(TAG, "send data to "MACSTR"", MAC2STR(send_cb->mac_addr));
195
196 memcpy(send_param->dest_mac, send_cb->mac_addr, ESP_NOW_ETH_ALEN);
197 example_espnow_data_prepare(send_param);
198
199 /* Send the next data after the previous data is sent. */
200 if (esp_now_send(send_param->dest_mac, send_param->buffer, send_param->len) != ESP_OK) {
201 ESP_LOGE(TAG, "Send error");
202 example_espnow_deinit(send_param);
203 vTaskDelete(NULL);
204 }
205 break;
206 }
207 case EXAMPLE_ESPNOW_RECV_CB:
208 {
209 example_espnow_event_recv_cb_t *recv_cb = &evt.info.recv_cb;
210
211 ret = example_espnow_data_parse(recv_cb->data, recv_cb->data_len, &recv_state, &recv_seq, &recv_magic);
212 free(recv_cb->data);
213 if (ret == EXAMPLE_ESPNOW_DATA_BROADCAST) {
214 ESP_LOGI(TAG, "Receive %dth broadcast data from: "MACSTR", len: %d", recv_seq, MAC2STR(recv_cb->mac_addr), recv_cb->data_len);
215
216 /* If MAC address does not exist in peer list, add it to peer list. */
217 if (esp_now_is_peer_exist(recv_cb->mac_addr) == false) {
218 esp_now_peer_info_t *peer = malloc(sizeof(esp_now_peer_info_t));
219 if (peer == NULL) {
220 ESP_LOGE(TAG, "Malloc peer information fail");
221 example_espnow_deinit(send_param);
222 vTaskDelete(NULL);
223 }
224 memset(peer, 0, sizeof(esp_now_peer_info_t));
225 peer->channel = CONFIG_ESPNOW_CHANNEL;
226 peer->ifidx = ESPNOW_WIFI_IF;
227 peer->encrypt = true;
228 memcpy(peer->lmk, CONFIG_ESPNOW_LMK, ESP_NOW_KEY_LEN);
229 memcpy(peer->peer_addr, recv_cb->mac_addr, ESP_NOW_ETH_ALEN);
230 ESP_ERROR_CHECK( esp_now_add_peer(peer) );
231 free(peer);
232 }
233
234 /* Indicates that the device has received broadcast ESPNOW data. */
235 if (send_param->state == 0) {
236 send_param->state = 1;
237 }
238
239 /* If receive broadcast ESPNOW data which indicates that the other device has received
240 * broadcast ESPNOW data and the local magic number is bigger than that in the received
241 * broadcast ESPNOW data, stop sending broadcast ESPNOW data and start sending unicast
242 * ESPNOW data.
243 */
244 if (recv_state == 1) {
245 /* The device which has the bigger magic number sends ESPNOW data, the other one
246 * receives ESPNOW data.
247 */
248 if (send_param->unicast == false && send_param->magic >= recv_magic) {
249 ESP_LOGI(TAG, "Start sending unicast data");
250 ESP_LOGI(TAG, "send data to "MACSTR"", MAC2STR(recv_cb->mac_addr));
251
252 /* Start sending unicast ESPNOW data. */
253 memcpy(send_param->dest_mac, recv_cb->mac_addr, ESP_NOW_ETH_ALEN);
254 example_espnow_data_prepare(send_param);
255 if (esp_now_send(send_param->dest_mac, send_param->buffer, send_param->len) != ESP_OK) {
256 ESP_LOGE(TAG, "Send error");
257 example_espnow_deinit(send_param);
258 vTaskDelete(NULL);
259 }
260 else {
261 send_param->broadcast = false;
262 send_param->unicast = true;
263 }
264 }
265 }
266 }
267 else if (ret == EXAMPLE_ESPNOW_DATA_UNICAST) {
268 ESP_LOGI(TAG, "Receive %dth unicast data from: "MACSTR", len: %d", recv_seq, MAC2STR(recv_cb->mac_addr), recv_cb->data_len);
269
270 /* If receive unicast ESPNOW data, also stop sending broadcast ESPNOW data. */
271 send_param->broadcast = false;
272 }
273 else {
274 ESP_LOGI(TAG, "Receive error data from: "MACSTR"", MAC2STR(recv_cb->mac_addr));
275 }
276 break;
277 }
278 default:
279 ESP_LOGE(TAG, "Callback type error: %d", evt.id);
280 break;
281 }
282 }
283 }
284
example_espnow_init(void)285 static esp_err_t example_espnow_init(void)
286 {
287 example_espnow_send_param_t *send_param;
288
289 s_example_espnow_queue = xQueueCreate(ESPNOW_QUEUE_SIZE, sizeof(example_espnow_event_t));
290 if (s_example_espnow_queue == NULL) {
291 ESP_LOGE(TAG, "Create mutex fail");
292 return ESP_FAIL;
293 }
294
295 /* Initialize ESPNOW and register sending and receiving callback function. */
296 ESP_ERROR_CHECK( esp_now_init() );
297 ESP_ERROR_CHECK( esp_now_register_send_cb(example_espnow_send_cb) );
298 ESP_ERROR_CHECK( esp_now_register_recv_cb(example_espnow_recv_cb) );
299
300 /* Set primary master key. */
301 ESP_ERROR_CHECK( esp_now_set_pmk((uint8_t *)CONFIG_ESPNOW_PMK) );
302
303 /* Add broadcast peer information to peer list. */
304 esp_now_peer_info_t *peer = malloc(sizeof(esp_now_peer_info_t));
305 if (peer == NULL) {
306 ESP_LOGE(TAG, "Malloc peer information fail");
307 vSemaphoreDelete(s_example_espnow_queue);
308 esp_now_deinit();
309 return ESP_FAIL;
310 }
311 memset(peer, 0, sizeof(esp_now_peer_info_t));
312 peer->channel = CONFIG_ESPNOW_CHANNEL;
313 peer->ifidx = ESPNOW_WIFI_IF;
314 peer->encrypt = false;
315 memcpy(peer->peer_addr, s_example_broadcast_mac, ESP_NOW_ETH_ALEN);
316 ESP_ERROR_CHECK( esp_now_add_peer(peer) );
317 free(peer);
318
319 /* Initialize sending parameters. */
320 send_param = malloc(sizeof(example_espnow_send_param_t));
321 memset(send_param, 0, sizeof(example_espnow_send_param_t));
322 if (send_param == NULL) {
323 ESP_LOGE(TAG, "Malloc send parameter fail");
324 vSemaphoreDelete(s_example_espnow_queue);
325 esp_now_deinit();
326 return ESP_FAIL;
327 }
328 send_param->unicast = false;
329 send_param->broadcast = true;
330 send_param->state = 0;
331 send_param->magic = esp_random();
332 send_param->count = CONFIG_ESPNOW_SEND_COUNT;
333 send_param->delay = CONFIG_ESPNOW_SEND_DELAY;
334 send_param->len = CONFIG_ESPNOW_SEND_LEN;
335 send_param->buffer = malloc(CONFIG_ESPNOW_SEND_LEN);
336 if (send_param->buffer == NULL) {
337 ESP_LOGE(TAG, "Malloc send buffer fail");
338 free(send_param);
339 vSemaphoreDelete(s_example_espnow_queue);
340 esp_now_deinit();
341 return ESP_FAIL;
342 }
343 memcpy(send_param->dest_mac, s_example_broadcast_mac, ESP_NOW_ETH_ALEN);
344 example_espnow_data_prepare(send_param);
345
346 xTaskCreate(example_espnow_task, "example_espnow_task", 2048, send_param, 4, NULL);
347
348 return ESP_OK;
349 }
350
example_espnow_deinit(example_espnow_send_param_t * send_param)351 static void example_espnow_deinit(example_espnow_send_param_t *send_param)
352 {
353 free(send_param->buffer);
354 free(send_param);
355 vSemaphoreDelete(s_example_espnow_queue);
356 esp_now_deinit();
357 }
358
app_main(void)359 void app_main(void)
360 {
361 // Initialize NVS
362 esp_err_t ret = nvs_flash_init();
363 if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
364 ESP_ERROR_CHECK( nvs_flash_erase() );
365 ret = nvs_flash_init();
366 }
367 ESP_ERROR_CHECK( ret );
368
369 example_wifi_init();
370 example_espnow_init();
371 }
372