为什么我的esp now无法实现通信,是因为我的库不兼容的问题吗。你们用的是什么版本的库
#include <WiFi.h>
#include <esp_now.h>
// 设置掌控板声音传感器与光线传感器引脚编号
const int soundPin = 33;
const int lightPin = 25;
// 设置数据结构体
typedef struct struct_message {
String board_name;
double light;
double sound;
} struct_message;
struct_message myData;
// 接收设备的 MAC 地址
uint8_t broadcastAddress[] = {0xe8, 0x31, 0xcd, 0x32, 0x1c, 0x28};
//内置天线模块MAC e8:31:cd:32:1c:28
//外置天线模块MAC 0x78, 0x21, 0x84, 0x7C, 0x74, 0xA8
// 数据发送回调函数
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
char macStr[18];
Serial.print("Packet to: ");
snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
Serial.println(macStr);
Serial.print("Send status: ");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
Serial.println();
}
void setup() {
Serial.begin(9600);
// 初始化 ESP-NOW
WiFi.mode(WIFI_STA);
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// 设置发送数据回调函数
esp_now_register_send_cb(OnDataSent);
// 绑定数据接收端
esp_now_peer_info_t peerInfo;
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
// 检查设备是否配对成功
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println("Failed to add peer");
return;
}
}
void loop() {
// 设置要发送的数据
myData.board_name = "Happyboard_#1";
myData.light = 15;//analogRead(lightPin);
myData.sound = 78;//analogRead(soundPin);
// 发送数据
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");
}
delay(1000);
}
用的是这个例程,
https://www.arduino.cn/forum.php ... highlight=esp%2Bnow |