目录
- 简介
- 读取ESP32/ESP8266接收方Receiver的MAC地址
- ESP32发送方Sender程序
- ESP32/ESP8266接收方Receiver程序
- ESP-NOW通信验证
- 总结
简介
本实验通过ESP-NOW无线通信协议实现ESP32开发板向多个ESP32/ESP 8266开发板发送数据。
读取ESP32/ESP8266接收方Receiver的MAC地址
读取ESP32开发板的代码
#include <WiFi.h>
#include <esp_wifi.h>void readMacAddress(){uint8_t baseMac[6];esp_err_t ret = esp_wifi_get_mac(WIFI_IF_STA, baseMac);if (ret == ESP_OK) {Serial.printf("%02x:%02x:%02x:%02x:%02x:%02x\n",baseMac[0], baseMac[1], baseMac[2],baseMac[3], baseMac[4], baseMac[5]);} else {Serial.println("Failed to read MAC address");}
}void setup(){Serial.begin(115200);WiFi.mode(WIFI_STA);WiFi.STA.begin();Serial.print("[DEFAULT] ESP32 Board MAC Address: ");readMacAddress();
}void loop(){}
通过串口读取到接收方ESP32开发板1的MAC地址为:
同样的方式读取到接收方ESP32开发板2的MAC地址为:
读取ESP8266开发板的代码
#include <ESP8266WiFi.h>void setup(){Serial.begin(115200);Serial.println();Serial.print("ESP Board MAC Address: ");Serial.println(WiFi.macAddress());
}void loop(){}
读取到接收方ESP8266开发板的MAC地址为:
ESP32发送方Sender程序
把ESP32开发板1,ESP开发板2,ESP8266开发板的MAC地址分别填入到下列代码的broadcastAddress1[ ],broadcastAddress2[ ],broadcastAddress3[ ]数组中
#include <esp_now.h>
#include <WiFi.h>// REPLACE WITH YOUR ESP RECEIVER'S MAC ADDRESS
uint8_t broadcastAddress1[] = {0x54, 0x43, 0xb2, 0x7f, 0x00, 0x60};
uint8_t broadcastAddress2[] = {0x34, 0x5f, 0x45, 0xac, 0x16, 0xc0};
uint8_t broadcastAddress3[] = {0xdc, 0x4f, 0x22, 0x23, 0xca, 0x10};typedef struct test_struct {int x;int y;
} test_struct;esp_now_peer_info_t peerInfo;void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {char macStr[18];Serial.print("Packet to: ");// Copies the sender mac address to a stringsnprintf(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.print(macStr);Serial.print(" send status:\t");Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}void setup() {Serial.begin(115200);WiFi.mode(WIFI_STA);if (esp_now_init() != ESP_OK) {Serial.println("Error initializing ESP-NOW");return;}esp_now_register_send_cb(OnDataSent);// register peerpeerInfo.channel = 0; peerInfo.encrypt = false;memcpy(peerInfo.peer_addr, broadcastAddress1, 6);if (esp_now_add_peer(&peerInfo) != ESP_OK){Serial.println("Failed to add peer");return;}memcpy(peerInfo.peer_addr, broadcastAddress2, 6);if (esp_now_add_peer(&peerInfo) != ESP_OK){Serial.println("Failed to add peer");return;}memcpy(peerInfo.peer_addr, broadcastAddress3, 6);if (esp_now_add_peer(&peerInfo) != ESP_OK){Serial.println("Failed to add peer");return;}
}void loop() {test_struct test;test_struct test2;test_struct test3;test.x = random(0,20);test.y = random(0,20);test2.x = random(0,20);test2.y = random(0,20);test3.x = random(0,20);test3.y = random(0,20);esp_err_t result1 = esp_now_send(broadcastAddress1, (uint8_t *) &test,sizeof(test_struct));if (result1 == ESP_OK) {Serial.println("Sent with success");}else {Serial.println("Error sending the data");}delay(500);esp_err_t result2 = esp_now_send(broadcastAddress2, (uint8_t *) &test2,sizeof(test_struct));if (result2 == ESP_OK) {Serial.println("Sent with success");}else {Serial.println("Error sending the data");}delay(500); esp_err_t result3 = esp_now_send(broadcastAddress3, (uint8_t *) &test3,sizeof(test_struct));if (result3 == ESP_OK) {Serial.println("Sent with success");}else {Serial.println("Error sending the data");}delay(2000);
}
ESP32/ESP8266接收方Receiver程序
上传代码到ESP32开发板1和ESP3开发板2
#include <esp_now.h>
#include <WiFi.h>//Structure example to receive data
//Must match the sender structure
typedef struct test_struct {int x;int y;
} test_struct;//Create a struct_message called myData
test_struct 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("x: ");Serial.println(myData.x);Serial.print("y: ");Serial.println(myData.y);Serial.println();
}void setup() {//Initialize Serial MonitorSerial.begin(115200);//Set device as a Wi-Fi StationWiFi.mode(WIFI_STA);//Init ESP-NOWif (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 infoesp_now_register_recv_cb(esp_now_recv_cb_t(OnDataRecv));
}void loop() {}
ESP8266开发板接收代码
#include <ESP8266WiFi.h>
#include <espnow.h>//Structure example to receive data
//Must match the sender structure
typedef struct test_struct {int x;int y;
} test_struct;//Create a struct_message called myData
test_struct myData;//callback function that will be executed when data is received
void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t len) {memcpy(&myData, incomingData, sizeof(myData));Serial.print("Bytes received: ");Serial.println(len);Serial.print("x: ");Serial.println(myData.x);Serial.print("y: ");Serial.println(myData.y);Serial.println();
}void setup() {//Initialize Serial MonitorSerial.begin(115200);//Set device as a Wi-Fi StationWiFi.mode(WIFI_STA);//Init ESP-NOWif (esp_now_init() != 0) {Serial.println("Error initializing ESP-NOW");return;}// Once ESPNow is successfully Init, we will register for recv CB to// get recv packer infoesp_now_set_self_role(ESP_NOW_ROLE_SLAVE);esp_now_register_recv_cb(OnDataRecv);
}void loop() {}
ESP-NOW通信验证
同时给发送方ESP32开发板,接收方ESP32开发板1,接收方ESP32开发板2,接收方ESP8266开发板供电,并打开四个串口助手观察四个开发板的数据收发情况
若接收方都收到数据,发送方ESP32开发板会收到应答信息,串口打印各个MAC地址的接收方数据分发成功:
若有接收方没有收到数据,发送方ESP32开发板会收到应答信息,串口打印对应的MAC地址的接收方数据分发失败:
接收方ESP32开发板1串口打印接收到发送方发过来的随机数据
接收方ESP32开发板2串口打印接收到发送方发过来的随机数据
接收方ESP8266开发板串口打印接收到发送方发过来的随机数据
总结
通过以上例程验证了ESP32/ESP开发板之间单向一对多的ESP-NOW无线通信,接下来的篇章将继续验证多个的ESP32开发板之间实现多对一的ESP-NOW无线通信。