esp-now广播

NIANXIN30W
Posts: 9
Joined: Fri Sep 01, 2023 1:39 am

esp-now广播

Postby NIANXIN30W » Fri Aug 02, 2024 1:10 am

主控1 和主控2之间espnow通信,附近只有这两个设备,主控1 广播,主控2 在接收回调中打印收到广播的源MAC地址,为什么除了主控1 的MAC,还有其他莫名其妙的MAC,会有很多个MAC地址?是我的配置哪里有问题吗?请求帮助!
这是我的发送端主控1代码:
typedef struct data_message{
char id;
char ssid[20];
char passwd[20];
}data_message;


static const char *TAG = "ESPNOW_SEND";
static QueueHandle_t s_example_espnow_queue;

static uint8_t s_example_broadcast_mac[ESP_NOW_ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; //对方MAC地址

static void example_wifi_init(void) //在使用ESP-NOW前先初始化wifi
{
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(WIFI_MODE_STA) );
ESP_ERROR_CHECK( esp_wifi_start());
ESP_ERROR_CHECK( esp_wifi_set_channel(1, WIFI_SECOND_CHAN_NONE));

#if CONFIG_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
}

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; //将参数的mac_addr和status存到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, portMAX_DELAY) != pdTRUE) {
ESP_LOGW(TAG, "Send send queue fail");
}
}
static void example_espnow_task(void *pvParameter)
{
example_espnow_event_t evt;

data_message *Data = (data_message *)pvParameter;

if (esp_now_send(s_example_broadcast_mac,(uint8_t *)Data,sizeof(data_message)) != ESP_OK) {
ESP_LOGE(TAG, "Send error");
}
while (xQueueReceive(s_example_espnow_queue, &evt, portMAX_DELAY) == pdTRUE) { //接收
/*if(esp_now_send(s_example_broadcast_mac,(uint8_t *)&Data,sizeof(Data))) {
ESP_LOGE(TAG, "Send error");
}
else
{*/
ESP_LOGI(TAG, "Start sending data");
example_espnow_event_send_cb_t *send_cb = &evt.info.send_cb;
ESP_LOGE(TAG, "Send success");
ESP_LOGI(TAG, "Send data to "MACSTR", status1: %d", MAC2STR(send_cb->mac_addr), send_cb->status);
ESP_LOGI(TAG, "send data to "MACSTR"", MAC2STR(s_example_broadcast_mac));
/*}*/
}
}
static esp_err_t example_espnow_init(void) //ESP-NOW初始化
{

data_message *Data;
s_example_espnow_queue = xQueueCreate(ESPNOW_QUEUE_SIZE,sizeof(data_message)); //创建消息队列
if (s_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() ); //初始化ESP-NOW
ESP_ERROR_CHECK( esp_now_register_send_cb(example_espnow_send_cb) );//注册发送和接收回调函数

/* Set primary master key. */
// ESP_ERROR_CHECK( esp_now_set_pmk((uint8_t *)CONFIG_ESPNOW_PMK) ); //设置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");

esp_now_deinit(); //当调用 esp_now_deinit() 时,配对设备的所有信息都将被删除。
return ESP_FAIL;
}
memset(peer, 0, sizeof(esp_now_peer_info_t));
peer->channel = 0;
// peer->ifidx = ESPNOW_WIFI_IF; //启用AP接口,才可发送ESP-NOW数据
peer->ifidx = ESP_IF_WIFI_STA;
peer->encrypt = false; //此对等方发送/接收的ESPNOW数据是否加密
memcpy(peer->peer_addr, s_example_broadcast_mac, ESP_NOW_ETH_ALEN);
ESP_ERROR_CHECK( esp_now_add_peer(peer) ); //将广播对等方信息添加到对等方列表中。
free(peer);
//set发送ESPNOW数据的参数
Data = malloc(sizeof(data_message)); //发送ESPNOW数据的参数
memset(Data, 0, sizeof(data_message)); //清零,重新配置参数
if (Data == NULL) {
ESP_LOGE(TAG, "Malloc send parameter fail");
vSemaphoreDelete(s_example_espnow_queue);
return ESP_FAIL;
}

Data->id = 2;
strcpy(Data->ssid,"daikin!!!!");
strcpy(Data->passwd,"123456789");
ESP_LOGI(TAG, "id3:%d", Data->id);
ESP_LOGI(TAG, "ssid:%s", Data->ssid);
ESP_LOGI(TAG, "passwd:%s", Data->passwd);
xTaskCreate(example_espnow_task, "example_espnow_task", 2048, Data, 4, NULL); //创建任务
return ESP_OK;
}

void app_main()
{
// 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();
}

这是我的接收端主控2代码:
typedef struct data_message{
char id;
char ssid[20];
char passwd[20];
}data_message;

static const char *TAG = "ESPNOW_RECEIVE";
static QueueHandle_t s_example_espnow_queue;

static void example_wifi_init(void) //在使用ESP-NOW前先初始化wifi
{
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(WIFI_MODE_STA) );
ESP_ERROR_CHECK( esp_wifi_start());
ESP_ERROR_CHECK( esp_wifi_set_channel(1, WIFI_SECOND_CHAN_NONE));

#if CONFIG_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
}
static void example_espnow_recv_cb(const uint8_t *mac_addr, const uint8_t *data, int len) //接受回调函数
{
data_message *Rec_Data = (data_message *)data;
ESP_LOGI(TAG, "RECEIVE data from: "MACSTR",len:%d", MAC2STR(mac_addr), strlen((char *)mac_addr));
for (size_t i = 0; i < strlen((char *)mac_addr); i++)
{
ESP_LOGI(TAG, "%02x", mac_addr);
}

ESP_LOGI(TAG, "id2:%d", Rec_Data->id);
ESP_LOGI(TAG, "ssid:%s", Rec_Data->ssid);
ESP_LOGI(TAG, "passwd:%s", Rec_Data->passwd);
if (xQueueSend(s_example_espnow_queue, Rec_Data, portMAX_DELAY) != pdTRUE) {
ESP_LOGW(TAG, "Send receive queue fail");
}
}
static void example_espnow_task(void *pvParameter)
{
data_message *Rec_Data;
Rec_Data = malloc(sizeof(data_message));
ESP_LOGI(TAG, "Start receiving data");
while (xQueueReceive(s_example_espnow_queue, Rec_Data, portMAX_DELAY) == pdTRUE) {
ESP_LOGI(TAG, "receiving success!!!");

ESP_LOGI(TAG, "id9:%d", Rec_Data->id);
ESP_LOGI(TAG, "ssid:%s", (*Rec_Data).ssid);
ESP_LOGI(TAG, "passwd:%s", Rec_Data->passwd);
}
}

static esp_err_t example_espnow_init(void) //ESP-NOW初始化
{
s_example_espnow_queue = xQueueCreate(ESPNOW_QUEUE_SIZE, sizeof(data_message)); //创建消息队列
if (s_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() ); //初始化ESP-NOW
//注册发送和接收回调函数
ESP_ERROR_CHECK( esp_now_register_recv_cb(example_espnow_recv_cb) );

/* Set primary master key. */
// ESP_ERROR_CHECK( esp_now_set_pmk((uint8_t *)CONFIG_ESPNOW_PMK) ); //设置PMK

xTaskCreate(example_espnow_task, "example_espnow_task", 2048, NULL, 4, NULL); //创建任务
return ESP_OK;
}

void app_main()
{
// 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();
}

Who is online

Users browsing this forum: No registered users and 72 guests