espnow 例程解析

1、什么是espnow

短数据传输、无连接的快速通讯技术

2、应用范围智能照明控制

(一)、 ESPNOW例程。

下面的例程展示了怎样使用ESPNOW

步骤:

1、初始化wifi

A、TCP/ip适配器初始化

a、注册事件处理函数初始化。

b、设置配置信息

c、选择存储类型

d、选择wifi模式AP

e、启动wifi

f、启动wifi后设置通道,假如两个设备在同一个通道上就不需要设置

2、初始化espnow

a、创建个队列

b、注册发送数据回调函数

c、注册接受数据回调函数

d、设置主秘钥

e、添加广播点信息来点列表

f、初始化发送参数

g、创建espnow任务

*/

#include

#include

#include

#include

#include "freertos/FreeRTOS.h"

#include "freertos/semphr.h"

#include "freertos/timers.h"

#include "nvs_flash.h"

#include "esp_event_loop.h"

#include "tcpip_adapter.h"

#include "esp_wifi.h"

#include "esp_log.h"

#include "esp_system.h"

#include "esp_now.h"

#include "rom/ets_sys.h"

#include "rom/crc.h"

#include "espnow_example.h"


static const char *TAG = "espnow_example";


static xQueueHandle example_espnow_queue;


static uint8_t 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);

1、事件处理函数

static esp_err_t example_event_handler(void *ctx, system_event_t *event)

{

  switch(event->event_id) {

  case SYSTEM_EVENT_STA_START:                    wifi启动完成

    ESP_LOGI(TAG, "WiFi started");

    break;

  default:

    break;

  }

  return ESP_OK;

}


2、wifi初始化

static void example_wifi_init(void)

{

  tcpip_adapter_init(); TCP/ip适配器初始化

  ESP_ERROR_CHECK( esp_event_loop_init(example_event_handler, NULL) ); 注册事件处理函数初始化。

  wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); 获取wifi配置信息

  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) ); 选择wifi模式AP

  ESP_ERROR_CHECK( esp_wifi_start()); 启动wifi

  ESP_ERROR_CHECK( esp_wifi_set_channel(CONFIG_ESPNOW_CHANNEL, 0) ); 启动wifi后设置通道,假如两个设备在同一个通道上就不需要设置

}


1、发送数据回调函数

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(example_espnow_queue, &evt, portMAX_DELAY) != pdTRUE) {

    ESP_LOGW(TAG, "Send send queue fail");

  }

}

2、接受回调函数

static void example_espnow_recv_cb(const uint8_t *mac_addr, const uint8_t *data, int len)

{

  example_espnow_event_t evt;

  example_espnow_event_recv_cb_t *recv_cb = &evt.info.recv_cb;


  if (mac_addr == NULL || data == NULL || len <= 0) {

    ESP_LOGE(TAG, "Receive cb arg error");

    return;

  }

设置接受事件,并添加到队列中

  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(example_espnow_queue, &evt, portMAX_DELAY) != pdTRUE) {

    ESP_LOGW(TAG, "Send receive queue fail");

    free(recv_cb->data);

  }

}


3、解析接收到的 ESPNOW数据(验证crc校验)

int example_espnow_data_parse(uint8_t *data, uint16_t data_len, uint8_t *state, uint16_t *seq, int *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 = crc16_le(UINT16_MAX, (uint8_t const *)buf, data_len);


  if (crc_cal == crc) {

    return buf->type;

  }


  return -1;

}


4、准备 ESPNOW 发送的数据

void example_espnow_data_prepare(example_espnow_send_param_t *send_param)

{

  example_espnow_data_t *buf = (example_espnow_data_t *)send_param->buffer;

  int i = 0;


  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;

  for (i = 0; i < send_param->len - sizeof(example_espnow_data_t); i++) {

    buf->payload[i] = (uint8_t)esp_random();

  }

  buf->crc = crc16_le(UINT16_MAX, (uint8_t const *)buf, send_param->len);

}

1、espnow任务处理函数

static void example_espnow_task(void *pvParameter)

{

  example_espnow_event_t evt;

  uint8_t recv_state = 0;

  uint16_t recv_seq = 0;

  int recv_magic = 0;

  bool is_broadcast = false;

  int ret;


  vTaskDelay(5000 / portTICK_RATE_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; 1、获取发送参数

  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);

  } 3、开始发送数据


  while (xQueueReceive(example_espnow_queue, &evt, portMAX_DELAY) == pdTRUE) {  4、判断是否有事件产生

    switch (evt.id) {

      case EXAMPLE_ESPNOW_SEND_CB:       5、发送事件处理函数

      {

        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_RATE_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:    5、接受事件处理函数

      {

        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;

  example_espnow_queue = xQueueCreate(ESPNOW_QUEUE_SIZE, sizeof(example_espnow_event_t)); 创建个队列

  if (example_espnow_queue == NULL) {

    ESP_LOGE(TAG, "Create mutex fail");

    return ESP_FAIL;

  }


  /* Initialize ESPNOW and register sending and receiving callback function. */

  ESP_ERROR_CHECK( esp_now_init() ); 初始哈espnow

  ESP_ERROR_CHECK( esp_now_register_send_cb(example_espnow_send_cb) ); 注册发送数据回调函数

  ESP_ERROR_CHECK( esp_now_register_recv_cb(example_espnow_recv_cb) );注册接受数据回调函数


  

  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");

    vSemaphoreDelete(example_espnow_queue);

    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, 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));

  memset(send_param, 0, sizeof(example_espnow_send_param_t));

  if (send_param == NULL) {

    ESP_LOGE(TAG, "Malloc send parameter fail");

    vSemaphoreDelete(example_espnow_queue);

    esp_now_deinit();

    return ESP_FAIL;

  }

  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);

    vSemaphoreDelete(example_espnow_queue);

    esp_now_deinit();

    return ESP_FAIL;

  }

  memcpy(send_param->dest_mac, 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);

创建espnow任务

  return ESP_OK;

}

5、释放资源

static void example_espnow_deinit(example_espnow_send_param_t *send_param)

{

  free(send_param->buffer);

  free(send_param);

  vSemaphoreDelete(example_espnow_queue);

  esp_now_deinit();

}


void app_main()

{

  // 1、nvs初始化

  esp_err_t ret = nvs_flash_init();

  if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {

    ESP_ERROR_CHECK( nvs_flash_erase() );

    ret = nvs_flash_init();

  }

  ESP_ERROR_CHECK( ret );

// 2、wifi初始化

  example_wifi_init();

// 3 espnow初始化

  example_espnow_init();

}


你可能感兴趣的:(ESP32)