Arduino ESP8266 ESP-NOW 通信:使用 MAC 地址 实现 多设备 数据传输
Arduino ESP8266 ESP-NOW 多设备数据传输示例
本文提供了使用 Arduino ESP8266 的 ESP-NOW 技术实现多个设备之间互相发送数据的代码示例。代码利用 MAC 地址进行设备识别,并使用同一个程序兼容多个设备。
代码示例
1. 针对 2, 3, 4 设备发送数据给 1 设备
#include <ESP8266WiFi.h>
#include <espnow.h>
#define A0_PIN A0
// 定义要发送给 1 设备的数据结构体
struct sendData {
int data;
};
// 定义 1 设备的 MAC 地址
uint8_t esp_1_address[] = {0x84, 0xCC, 0xA8, 0x84, 0x20, 0x58};
// 定义 2, 3, 4 设备的 MAC 地址数组
uint8_t esp_other_address[][6] = {{0xC8, 0xC9, 0xA3, 0xB9, 0x16, 0xE7},
{0x48, 0xE7, 0x29, 0x48, 0xC9, 0xB3},
{0x8C, 0xAA, 0xB5, 0x7B, 0x88, 0xA0}};
// 定义发送数据结构体对象
sendData dataToSend;
void setup() {
Serial.begin(115200);
// 初始化 espnow
WiFi.mode(WIFI_STA);
if (esp_now_init() != 0) {
Serial.println("ESP-NOW init failed");
return;
}
// 注册回调函数
esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
esp_now_register_recv_cb([](uint8_t *mac_addr, uint8_t *data, uint8_t len) {
Serial.print("Received data from: ");
for (int i = 0; i < 6; i++) {
Serial.print(mac_addr[i], HEX);
if (i < 5) Serial.print(":");
}
Serial.print(", data: ");
Serial.println(*data);
});
}
void loop() {
// 检测是否有 2, 3, 4 设备发送过来的数据
if (esp_now_recv(esp_other_address[0], (uint8_t*)&dataToSend, sizeof(sendData)) == 0) {
// 将接收到的数据发送到 1 设备
esp_now_send(esp_1_address, (uint8_t*)&dataToSend, sizeof(sendData));
Serial.print("Sending data to 1 device: ");
Serial.println(dataToSend.data);
}
else if (esp_now_recv(esp_other_address[1], (uint8_t*)&dataToSend, sizeof(sendData)) == 0) {
esp_now_send(esp_1_address, (uint8_t*)&dataToSend, sizeof(sendData));
Serial.print("Sending data to 1 device: ");
Serial.println(dataToSend.data);
}
else if (esp_now_recv(esp_other_address[2], (uint8_t*)&dataToSend, sizeof(sendData)) == 0) {
esp_now_send(esp_1_address, (uint8_t*)&dataToSend, sizeof(sendData));
Serial.print("Sending data to 1 device: ");
Serial.println(dataToSend.data);
}
// 检测 1 设备是否有数据发送过来
if (esp_now_recv(esp_1_address, (uint8_t*)&dataToSend, sizeof(sendData)) == 0) {
// 将接收到的数据发送给 2, 3, 4 设备
esp_now_send(esp_other_address[0], (uint8_t*)&dataToSend, sizeof(sendData));
esp_now_send(esp_other_address[1], (uint8_t*)&dataToSend, sizeof(sendData));
esp_now_send(esp_other_address[2], (uint8_t*)&dataToSend, sizeof(sendData));
Serial.print("Sending data to 2, 3, 4 devices: ");
Serial.println(dataToSend.data);
}
// 定时发送 A0 口的数据给 1 设备
dataToSend.data = analogRead(A0_PIN);
esp_now_send(esp_1_address, (uint8_t*)&dataToSend, sizeof(sendData));
Serial.print("Sending data to 1 device: ");
Serial.println(dataToSend.data);
delay(1000);
}
2. 针对 1 设备发送数据
#include <ESP8266WiFi.h>
#include <espnow.h>
// 定义要发送给 2, 3, 4 设备的数据结构体
struct sendData {
int data;
};
// 定义 1 设备的 MAC 地址
uint8_t esp_1_address[] = {0x84, 0xCC, 0xA8, 0x84, 0x20, 0x58};
// 定义 2, 3, 4 设备的 MAC 地址数组
uint8_t esp_other_address[][6] = {{0xC8, 0xC9, 0xA3, 0xB9, 0x16, 0xE7},
{0x48, 0xE7, 0x29, 0x48, 0xC9, 0xB3},
{0x8C, 0xAA, 0xB5, 0x7B, 0x88, 0xA0}};
// 定义发送数据结构体对象
sendData dataToSend;
void setup() {
Serial.begin(115200);
// 初始化 espnow
WiFi.mode(WIFI_STA);
if (esp_now_init() != 0) {
Serial.println("ESP-NOW init failed");
return;
}
// 注册回调函数
esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
esp_now_register_recv_cb([](uint8_t *mac_addr, uint8_t *data, uint8_t len) {
Serial.print("Received data from: ");
for (int i = 0; i < 6; i++) {
Serial.print(mac_addr[i], HEX);
if (i < 5) Serial.print(":");
}
Serial.print(", data: ");
Serial.println(*data);
});
}
void loop() {
// 检测是否有 1 设备发送过来的数据
if (esp_now_recv(esp_1_address, (uint8_t*)&dataToSend, sizeof(sendData)) == 0) {
// 将接收到的数据发送给 2, 3, 4 设备
esp_now_send(esp_other_address[0], (uint8_t*)&dataToSend, sizeof(sendData));
esp_now_send(esp_other_address[1], (uint8_t*)&dataToSend, sizeof(sendData));
esp_now_send(esp_other_address[2], (uint8_t*)&dataToSend, sizeof(sendData));
Serial.print("Sending data to 2, 3, 4 devices: ");
Serial.println(dataToSend.data);
}
}
说明
- 以上代码示例中,设备的 MAC 地址已在代码中硬编码,您需要根据您的实际情况进行修改。
- 代码中使用
esp_now_recv函数来接收数据,使用esp_now_send函数来发送数据。 esp_now_recv函数需要传入目标设备的 MAC 地址,以及用于存储接收数据的变量指针。esp_now_send函数需要传入目标设备的 MAC 地址,以及要发送的数据指针和数据大小。- 代码中使用了回调函数来处理接收到的数据。回调函数会根据接收到的数据进行相应的处理。
其他注意事项
- ESP-NOW 协议仅支持点对点通信,不支持广播。
- ESP-NOW 协议需要在同一网络中才能通信。
- ESP-NOW 协议的通信距离受环境影响,一般情况下不超过 100 米。
- 在使用 ESP-NOW 协议时,建议您使用独立的 pin 脚作为 ESP-NOW 的通信引脚,避免与其他功能冲突。
总结
本文提供了一个简单的 ESP-NOW 多设备数据传输示例,您可以根据实际情况进行修改和扩展。ESP-NOW 协议是一种简单易用的无线通信协议,非常适合用于简单的物联网应用。
原文地址: http://www.cveoy.top/t/topic/lUGo 著作权归作者所有。请勿转载和采集!