星座 网站 建设,一千块钱能注册公司吗,网站建设扌金手指六六,网站建设 页面ESP32 Arduino EspNow点对点双向通讯✨本案例分别采用esp32和esp32C3之间点对点单播无线通讯方式。 #x1f33f;esp32开发板 #x1f33e;esp32c3开发板 #x1f527;所需库(需要自行导入到Arduino IDE library文件夹中#xff0c;无法在IDE 管理库界面搜索下载到该库)esp32开发板 esp32c3开发板 所需库(需要自行导入到Arduino IDE library文件夹中无法在IDE 管理库界面搜索下载到该库)WifiEspNow库 – 库下载地址GitHubhttps://github.com/yoursunny/WifiEspNow ✨本案例实现的是单播通讯方式。 通讯效果
⚡数据长度可以根据使用需求自定义修改变量msg数组接收长度。需要注意的是数据长度限制 250
搭建环境说明 先烧录程序通过串口打印的本机MAC地址信息记录下来同理记下两个设备的MAC地址,然后将获取的对方的MAC地址填写入代码当中。A 设备使用 B 设备的MAC B 设备使用 A 设备的MAC地址 ESP8266只支持单播。ESP32支持单播和组播。 单播、广播、多播(组播)的概念和区别 一台机器和一台机器通信这是单播;一台机器发出的数据包能被多台机器收到这就叫组播,一个机器发送多台机器接收但是又不同于广播;一台机器发出的数据包能被一个网段的机器收到这叫广播.多播又叫组播 可以说广播是多播的特例多播就是给一组特定的主机多播组发送数据. 参考《单播、广播、多播(组播)的概念和区别》
程序代码
程序都是一样区别只是里面定义的存放MAC地址的数组为对方的MAC地址。
/*** file** EspNowUnicast.ino demonstrates how to transmit unicast ESP-NOW messages with c WifiEspNow .* You need two ESP8266 or ESP32 devices to run this example.** 单播通信要求发送方指定接收方的MAC地址。* 因此您必须为每个设备修改这个程序。** The recommended workflow is:* li 1. Flash the program onto device A.* li 2. Run the program on device A, look at serial console for its MAC address.* li 3. Copy the MAC address of device A, paste it in the c PEER variable below.* li 4. Flash the program that contains As MAC address onto device B.* li 5. Run the program on device A, look at serial console for its MAC address.* li 6. Copy the MAC address of device B, paste it in the c PEER variable below.* li 7. Flash the program that contains Bs MAC address onto device A.* li;将程序闪到设备A上。
* li 2;在设备A上运行程序查看串行控制台的MAC地址。
* li;复制设备A的MAC地址粘贴到下面的c PEER变量中。
* li;将包含A的MAC地址的程序闪到设备B上。
* li;在设备A上运行程序查看串行控制台的MAC地址。
* li。复制设备B的MAC地址粘贴到下面的c PEER变量中。
* li。将包含B的MAC地址的程序闪到设备A上。*/#include WifiEspNow.h
#if defined(ARDUINO_ARCH_ESP8266)
#include ESP8266WiFi.h
#elif defined(ARDUINO_ARCH_ESP32)
#include WiFi.h
#endif// 接收MAC地址。必须针对每个设备进行修改。
// static uint8_t PEER[]{0x08, 0x3A, 0xF2, 0x8D, 0xCD, 0xE1};//ESP32DEV MAC地址static uint8_t PEER[]{0x60, 0x55, 0xF9, 0x79, 0x87, 0x99};//esp3232c3
void printReceivedMessage(const uint8_t mac[WIFIESPNOW_ALEN], const uint8_t* buf, size_t count,void* arg)
{Serial.printf(Message from %02X:%02X:%02X:%02X:%02X:%02X\n, mac[0], mac[1], mac[2], mac[3],mac[4], mac[5]);for (int i 0; i static_castint(count); i) {Serial.print(static_castchar(buf[i]));}Serial.println();
}void setup()
{Serial.begin(115200);Serial.println();WiFi.persistent(false);WiFi.mode(WIFI_AP);WiFi.disconnect();WiFi.softAP(ESPNOW, nullptr, 3);WiFi.softAPdisconnect(false);// WiFi must be powered on to use ESP-NOW unicast.// It could be either AP or STA mode, and does not have to be connected.// For best results, ensure both devices are using the same WiFi channel.Serial.print(MAC address of this node is );Serial.println(WiFi.softAPmacAddress());uint8_t mac[6];WiFi.softAPmacAddress(mac);Serial.println();Serial.println(You can paste the following into the program for the other device:);Serial.printf(static uint8_t PEER[]{0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X};\n, mac[0],mac[1], mac[2], mac[3], mac[4], mac[5]);Serial.println();bool ok WifiEspNow.begin();if (!ok) {Serial.println(WifiEspNow.begin() failed);ESP.restart();}WifiEspNow.onReceive(printReceivedMessage, nullptr);ok WifiEspNow.addPeer(PEER);if (!ok) {Serial.println(WifiEspNow.addPeer() failed);ESP.restart();}
}void loop()
{char msg[60];int len snprintf(msg, sizeof(msg), hello ESP3dev-NOW from %s at %lu,WiFi.softAPmacAddress().c_str(), millis());WifiEspNow.send(PEER, reinterpret_castconst uint8_t*(msg), len);delay(1000);
}