当前位置: 首页 > news >正文

ESP32 Arduino EspNow点对点双向通讯

ESP32 Arduino EspNow点对点双向通讯


✨本案例分别采用esp32esp32C3之间点对点单播无线通讯方式。

  • 🌿esp32开发板
    在这里插入图片描述
  • 🌾esp32c3开发板
    在这里插入图片描述
  • 🔧所需库(需要自行导入到Arduino IDE library文件夹中,无法在IDE 管理库界面搜索下载到该库):WifiEspNow库
    – 📍库下载地址GitHub:https://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 A's 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 B's 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_cast<int>(count); ++i) {Serial.print(static_cast<char>(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_cast<const uint8_t*>(msg), len);delay(1000);
}
http://www.lryc.cn/news/18298.html

相关文章:

  • Linux SID 开发指南
  • Matlab进阶绘图第2期—线型热图
  • 【Redis中bigkey你了解吗?bigkey的危害?】
  • C++回顾(一)——从C到C++
  • CRF条件随机场 | 关键原理+面试知识点
  • 秒懂算法 | 回归算法中的贝叶斯
  • 用Netty实现物联网01:XML-RPC和JSON-RPC
  • 腾讯云服务器centos7安装python3.7+,解决ssl问题
  • C++【模板STL简介】
  • 该学会是自己找bug了(vs调试技巧)
  • Redis大全(概念与下载安装)
  • 指针的进阶【上篇】
  • MATLAB | 如何用MATLAB绘制花里胡哨的山脊图
  • .Net与程序集
  • 软考中级之数据库系统(重点)
  • 界面控件DevExtreme的Data Grid组件——让业务信息管理更轻松!
  • 【架构师】零基础到精通——网关策略
  • 【java 8】方法引用与构造器引用
  • SGI 空间配置器
  • 2023年白酒行业研究报告
  • 华为OD机试 -合规数组(Java) | 机试题+算法思路+考点+代码解析 【2023】
  • 华为OD机试真题Python实现【英文输入法】真题+解题思路+代码(20222023)
  • 改进YOLO系列 | 添加轻量化Decouple_Head 和 ASFF_Head
  • LLFlow沦为和代码解读
  • C语言学习及复习笔记-【9】数组
  • Kubernetes入门教程 --- 使用kubeadm进行集群安装
  • 华为OD机试真题Python实现【相对开音节】真题+解题思路+代码(20222023)
  • 海思SD3403/SS928V100开发(5)MIPI_YUV相机vio sample开发----修改思路
  • javaee之node.js与es6
  • 11 nacos源码开篇