ESP8266和ESP32基于Arduino EspNow组播通讯

438 篇文章 1686 订阅 ¥19.90 ¥99.00
254 篇文章 519 订阅 ¥19.90 ¥99.00
本博客介绍如何使用Arduino EspNow实现ESP8266和ESP32之间的组播无线通讯。ESP32C3作为主机,其他设备通过Boot和flash按键触发信息发送,数据在设备间轮流间隔发送。注意,程序烧录前需填写从机MAC地址。案例涉及依赖的WifiEspNow库需手动导入。
摘要由CSDN通过智能技术生成

ESP8266和ESP32基于Arduino EspNow组播通讯


✨本案例分别采用esp32和esp32C3以及ESP8266之间创建组播无线通讯方式。

  • 🍁通讯方式
    在这里插入图片描述
  • 🌼通讯效果:
  • 在这里插入图片描述
    在这里插入图片描述

这里提供一个简单的示例代码,其中ESP32作为主机ESP8266作为从机ESP32代码: #include <esp_now.h> #include <WiFi.h> #define CHANNEL 1 #define PRINTSCANRESULTS 0 #define PRINTDEBUG 1 // Replace with your network credentials const char* ssid = "your_SSID"; const char* password = "your_PASSWORD"; // Set the slave MAC address uint8_t slaveAddress[] = {0x24, 0x0A, 0xC4, 0x01, 0x23, 0x45}; // Create a struct to store data typedef struct struct_message { int id; float temperature; float humidity; } struct_message; // Create a struct_message to send data struct_message myData; // Callback when data is sent void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) { char macStr[18]; Serial.print("Packet to: "); Serial.println(macToStr(mac_addr, macStr)); Serial.print("Status: "); Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Success" : "Fail"); } // Scan for slaves in AP mode void scanForSlave() { int8_t scanResults = WiFi.scanNetworks(); if (scanResults == 0) { Serial.println("No WiFi devices in AP Mode found"); } else { Serial.print("Found "); Serial.print(scanResults); Serial.println(" devices "); for (int i = 0; i < scanResults; ++i) { // Print SSID and RSSI for each device found String SSID = WiFi.SSID(i); int32_t RSSI = WiFi.RSSI(i); String BSSIDstr = WiFi.BSSIDstr(i); Serial.printf("%d: %s (%d) MAC: %s\n", i + 1, SSID.c_str(), RSSI, BSSIDstr.c_str()); delay(10); } } Serial.println(""); // Wait a bit before scanning again delay(5000); } // Convert MAC address to String String macToStr(const uint8_t* mac, char* buf) { sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); return buf; } void setup() { 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; } // Register peer esp_now_peer_info_t peerInfo; memcpy(peerInfo.peer_addr, slaveAddress, 6); peerInfo.channel = CHANNEL; peerInfo.encrypt = false; // Add peer if (esp_now_add_peer(&peerInfo) != ESP_OK) { Serial.println("Failed to add peer"); return; } // Set up Wi-Fi connection WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi..."); } Serial.println("Connected to WiFi"); } void loop() { // Send data via ESP-NOW myData.id = 123; myData.temperature = 25.6; myData.humidity = 60.2; esp_err_t result = esp_now_send(slaveAddress, (uint8_t *) &myData, sizeof(myData)); if (result == ESP_OK) { Serial.println("Sent with success"); } else { Serial.println("Error sending the data"); } // Wait a bit before sending again delay(5000); } ESP8266代码: #include <esp_now.h> #include <WiFi.h> #define CHANNEL 1 #define PRINTSCANRESULTS 0 #define PRINTDEBUG 1 // Replace with your network credentials const char* ssid = "your_SSID"; const char* password = "your_PASSWORD"; // Create a struct to store data typedef struct struct_message { int id; float temperature; float humidity; } struct_message; // Create a struct_message to receive data struct_message myData; // Callback when data is received void OnDataRecv(const uint8_t *mac_addr, const uint8_t *incomingData, int len) { memcpy(&myData, incomingData, sizeof(myData)); Serial.print("Received from: "); char macStr[18]; Serial.println(macToStr(mac_addr, macStr)); Serial.print("ID: "); Serial.println(myData.id); Serial.print("Temperature: "); Serial.println(myData.temperature); Serial.print("Humidity: "); Serial.println(myData.humidity); } // Convert MAC address to String String macToStr(const uint8_t* mac, char* buf) { sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); return buf; } void setup() { 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; } // Register callback function esp_now_register_recv_cb(OnDataRecv); // Set up Wi-Fi connection WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi..."); } Serial.println("Connected to WiFi"); } void loop() { // Do nothing }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值