ESPNOW收发测试 基于esp-idf
使用2块ESP32S3模块做EPSNOW收发测试
模块1: ESP32S3R8N8
模块2: 立创开发板实战派( ESP32S3R8N16)
在乐鑫官方例程的基础上进行修改,验证收发
收发公用一套代码,通过调整宏定义来确定收发
代码如下:
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <assert.h>
#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"
static const char *TAG = "espnow_example";
#define ESP32S3R8 1 //recv
#define LICHUANG 0 //send
const char* buffer = "hello world!";
#if ESP32S3R8
const uint8_t my_mac[ESP_NOW_ETH_ALEN] = { 0x3C, 0x84, 0x27, 0xC1, 0x5D, 0x10 };
const uint8_t dest_mac[ESP_NOW_ETH_ALEN] = { 0xD8, 0x3B, 0xDA, 0x4D, 0x1F, 0xBC };
#endif
#if LICHUANG
const uint8_t my_mac[ESP_NOW_ETH_ALEN] = { 0xD8, 0x3B, 0xDA, 0x4D, 0x1F, 0xBC };
const uint8_t dest_mac[ESP_NOW_ETH_ALEN] = { 0x3C, 0x84, 0x27, 0xC1, 0x5D, 0x10 };
#endif
/* 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)
{
if (status == ESP_NOW_SEND_SUCCESS) {
ESP_LOGI(TAG, "Message sent successfully");
} else {
ESP_LOGE(TAG, "Message failed to send");
}
ESP_LOGI(TAG, "Send data to "MACSTR".", MAC2STR(dest_mac));
}
static void example_espnow_recv_cb(const esp_now_recv_info_t *recv_info, const uint8_t *data, int len)
{
ESP_LOGI(TAG, "Received message from: " MACSTR, MAC2STR(recv_info->src_addr));
ESP_LOGI(TAG, "Message: %.*s", len, data);
}
static void example_espnow_task(void *pvParameter)
{
vTaskDelay(2000 / portTICK_PERIOD_MS);
ESP_LOGI(TAG, "Start task");
#if ESP32S3R8
while(1)
{
vTaskDelay(2000 / portTICK_PERIOD_MS);
}
#endif
#if LICHUANG
while(1)
{
if (esp_now_send(dest_mac, (uint8_t *)buffer, strlen(buffer)) != ESP_OK) {
ESP_LOGE(TAG, "Send error");
}
vTaskDelay(2000 / portTICK_PERIOD_MS);
}
#endif
}
static void espnow_init(void)
{
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) );
ESP_ERROR_CHECK( esp_now_set_pmk((uint8_t *)CONFIG_ESPNOW_PMK) );
//判断是否已经配对,没有则直接添加配对。
/* If MAC address does not exist in peer list, add it to peer list. */
if (esp_now_is_peer_exist(dest_mac) == false) {
esp_now_peer_info_t *peer = malloc(sizeof(esp_now_peer_info_t));
if (peer == NULL) {
ESP_LOGE(TAG, "Malloc peer information fail");
}
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, dest_mac, ESP_NOW_ETH_ALEN);
ESP_ERROR_CHECK( esp_now_add_peer(peer) );
free(peer);
}
xTaskCreate(example_espnow_task, "example_espnow_task", 4096, NULL, 4, NULL);
}
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();
espnow_init();
}
接收成功则打印如下:
I (32175) espnow_example: Received message from: d8:3b:da:4d:1f:bc
I (32175) espnow_example: Message: hello world!