[已解决] esp-now通信不成功
[已解决] esp-now通信不成功
请问各位有谁玩过esp-now通信协议,是不是任何一个esp32都可以使用这个协议,目前我使用两个esp32的板子间做esp-now通信,但是一直都不通,显示peer interface is invalid,failed to add peer,也不知道什么原因。我按照官网的例子对了好几遍,都没有错,我只是改了mac地址,其他都没有动,都不行,有谁能帮分析分析吗
-
- Posts: 786
- Joined: Wed Nov 14, 2018 8:45 am
Re: esp-now通信不成功,
esp32、esp32-s、esp32-c、esp8266 之间都可以通信。你先基于原始 demo,什么都不改,跑通了再看下一步。
Re: esp-now通信不成功,
我基本都没有改,只是改了mac地址。打印出来错误为:Peer interface is invalid和Failed to add peer不懂是什么原因。是接受方哪个地方设置的不正确吗。以下是我发送端和接收端的程序,不忙的大神可以帮忙看看哪里出了问题。
以下为发送端的程序
--------------------------------------------------------------------------------------------------------------------------------
以下为接受端的程序。
以下为发送端的程序
Code: Select all
// Own macAddress:98:F4:AB:3A:77:0C
// recver MAC Address: 98:F4:AB:39:4C:C0
#include <esp_now.h>
#include <WiFi.h>
// REPLACE WITH YOUR RECEIVER MAC Address
uint8_t broadcastAddress[] = {0x98, 0xF4, 0xAB, 0x39, 0x4C, 0xC0};
// Structure example to send data
typedef struct struct_message {
char a[32];
int b;
float c;
String d;
bool e;
} struct_message;
// Create a struct_message called myData
struct_message myData;
// callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
Serial.print("\r\nLast Packet Send Status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}
void setup() {
// Init Serial Monitor
Serial.begin(115200);
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
Serial.println(WiFi.macAddress());
// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Once ESPNow is successfully Init, we will register for Send CB to
// get the status of Trasnmitted packet
esp_now_register_send_cb(OnDataSent);
// Register peer
esp_now_peer_info_t peerInfo;
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
// Add peer
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println("Failed to add peer");
return;
}
}
void loop() {
// Set values to send
strcpy(myData.a, "THIS IS A CHAR");
myData.b = random(1,20);
myData.c = 1.2;
myData.d = "Hello";
myData.e = false;
// Send message via ESP-NOW
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
if (result == ESP_OK) {
Serial.println("Sent with success");
}
else {
Serial.println("Error sending the data");
Serial.print("error code is : ");
Serial.println(result);
}
delay(2000);
}
以下为接受端的程序。
Code: Select all
// send board MAC Address:98:F4:AB:3A:77:0C
// Recv board MAC Address: 98:F4:AB:39:4C:C0
#include <esp_now.h>
#include <WiFi.h>
// Structure example to receive data
typedef struct struct_message {
char a[32];
int b;
float c;
String d;
bool e;
} struct_message;
// Create a struct_message called myData
struct_message myData;
// callback function that will be executed when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
memcpy(&myData, incomingData, sizeof(myData));
Serial.print("Bytes received: ");
Serial.println(len);
Serial.print("Char: ");
Serial.println(myData.a);
Serial.print("Int: ");
Serial.println(myData.b);
Serial.print("Float: ");
Serial.println(myData.c);
Serial.print("String: ");
Serial.println(myData.d);
Serial.print("Bool: ");
Serial.println(myData.e);
Serial.println();
}
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
Serial.println(WiFi.macAddress());
// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Once ESPNow is successfully Init, we will register for recv CB to
// get recv packer info
esp_now_register_recv_cb(OnDataRecv);
}
void loop() { }
Re: esp-now通信不成功,--现在已解决问题。
仔细研究了esp关于wifi方面的文档,并且仔细理解了下peer interface is invalid这句话到底是哪里出错了,后来推测是不是esp-now需要指定ifidx这个参数,
wifi_interface_t ifidx:说明如下
Wi-Fi interface that peer uses to send/receive ESPNOW data
程序中是没有指定这个参数的,所以参考文档,指定了这个参数,在 peer information里面加了一句:
peerInfo.ifidx = WIFI_IF_STA;
最后测试成功了。不知道为什么很多例程都没有,反而都可以通信成功。
wifi_interface_t ifidx:说明如下
Wi-Fi interface that peer uses to send/receive ESPNOW data
程序中是没有指定这个参数的,所以参考文档,指定了这个参数,在 peer information里面加了一句:
peerInfo.ifidx = WIFI_IF_STA;
最后测试成功了。不知道为什么很多例程都没有,反而都可以通信成功。
Who is online
Users browsing this forum: No registered users and 56 guests