簡介
本實驗通過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:%02xn",
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 string
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.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 peer
peerInfo.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 Monitor
Serial.begin(115200);
//Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
//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(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 Monitor
Serial.begin(115200);
//Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
//Init ESP-NOW
if (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 info
esp_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無線通信。
審核編輯 黃宇
-
開發板
+關注
關注
26文章
6307瀏覽量
118538 -
ESP8266
+關注
關注
51文章
971瀏覽量
49545 -
ESP32
+關注
關注
26文章
1201瀏覽量
21888
發布評論請先 登錄
ESP32/ESP8266開發板ESP-NOW無線通信
【ESP32-C5系列】WT9932C5-TINY開發板上手指南
【ESP32-P4系列】WT9932P4-MINI開發板上手指南
【ESP32-P4系列】WT9932P4-MINI開發板規格書
無需安裝!在瀏覽器里就能玩轉ESP32/ESP8266,這個神器絕了!
樂鑫科技ESP-AMP框架詳解!兩款已支持的ESP32-P4C5開發板推薦!啟明云端樂鑫科技代理
ESP32-P4全功能開發板和ESP32-P4-TINY開發板該怎么選?看這篇就夠了!
晶科鑫 | 國產26MHz晶振匹配Espressif(樂鑫) ESP8285/ESP8266芯片案例
ESP8266和ESP32開發板常見的2種下載方式
ESP8266和ESP32開發板常見的2種下載方式
ESP32的ESP-NOW通訊踩坑記
ESP32-P4-MINI開發板開箱和上手指南來了!速速碼住!
ESP32-P4 C5開發板燒錄小智全流程!速看!
ESP32-S3開發板燒錄小智AI系統全流程指南
ESP32/ESP8266開發板單向一對多ESP-NOW無線通信
評論