ESP8266-Arduino网络编程实例-ESP-MESH传感器数据发送与接收 ESP-MESH传感器数据发送与接收 在前面的文章中,我们对ESP-MESH网络协议的使用有了一定的了解,本文将在这个基础上,进一步介绍如何在ESP-MESH网络中,不同设备(节点)交换传感器数据,即广播本节点传感器数据和接收其他节点的传感器数据。
1、硬件准备
ESP8266 NodeMCU开发板两块
数据线两条
2、软件准备
Arduino IDE或VSCode + PlatformIO
在前面的文章中,对如何搭建ESP8266开发环境做了详细的介绍,请参考:
ESP8266 NodeMCU的引脚介绍在前面的文章中做了详细的介绍,请参考:
本次实例将使用BME280传感器,其硬件接线图如下:
3、代码实现 本次实例使用到的开源库如下:
示例代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 #include <Adafruit\_Sensor.h> #include <Adafruit\_BME280.h> #include "painlessMesh.h" #include <Arduino\_JSON.h> // MESH网络配置 #define MESH\_PREFIX "\*\*\*\*\*" // MESH网络名称 #define MESH\_PASSWORD "\*\*\*\*\*" // MESH网络密码 #define MESH\_PORT 5555 // 默认端口 // BME280传感器对象 Adafruit_BME280 bme; // 本设备节点编号,不同设备,请更改 int nodeNumber = 2; String readings; Scheduler userScheduler; // MESH网络对象 painlessMesh mesh; void sendMessage() ; String getReadings(); Task taskSendMessage(TASK_SECOND \* 5 , TASK_FOREVER, &sendMessage); // 读取传感器数据 String getReadings () { JSONVar jsonReadings; jsonReadings["node"] = nodeNumber; jsonReadings["temp"] = bme.readTemperature(); jsonReadings["hum"] = bme.readHumidity(); jsonReadings["pres"] = bme.readPressure()/100.0F; // 转换成JSON字符串 readings = JSON.stringify(jsonReadings); return readings; } // 发送消息 void sendMessage () { String msg = getReadings(); mesh.sendBroadcast(msg); } // 初始化BME280 void initBME(){ if (!bme.begin(0x76)) { Serial.println("Could not find a valid BME280 sensor, check wiring!"); while (1); } } // 数据接收回调函数 void receivedCallback( uint32\_t from, String &msg ) { Serial.printf("Received from %u msg=%s\n", from, msg.c\_str()); // 解析JSON数据 JSONVar myObject = JSON.parse(msg.c\_str()); // 访问JSON数据 int node = myObject["node"]; double temp = myObject["temp"]; double hum = myObject["hum"]; double pres = myObject["pres"]; // 打印接收结果 Serial.print("Node: "); Serial.println(node); Serial.print("Temperature: "); Serial.print(temp); Serial.println(" C"); Serial.print("Humidity: "); Serial.print(hum); Serial.println(" %"); Serial.print("Pressure: "); Serial.print(pres); Serial.println(" hpa"); } // 网络连接回调 void newConnectionCallback(uint32\_t nodeId) { Serial.printf("New Connection, nodeId = %u\n", nodeId); } // 网络连接状态变化回调 void changedConnectionCallback() { Serial.printf("Changed connections\n"); } // 节点时间调整回调 void nodeTimeAdjustedCallback(int32\_t offset) { Serial.printf("Adjusted time %u. Offset = %d\n", mesh.getNodeTime(),offset); } void setup() { // 初始化串口 Serial.begin(115200); // 初始化BME280传感器 initBME(); //mesh.setDebugMsgTypes( ERROR | MESH\_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG\_TYPES | REMOTE ); // all types on // 设置消息调试类型 mesh.setDebugMsgTypes( ERROR | STARTUP ); // set before init() so that you can see startup messages // 初始化MESH网络 mesh.init( MESH_PREFIX, MESH_PASSWORD, &userScheduler, MESH_PORT ); // 注册各事件回调函数 mesh.onReceive(&receivedCallback); mesh.onNewConnection(&newConnectionCallback); mesh.onChangedConnections(&changedConnectionCallback); mesh.onNodeTimeAdjusted(&nodeTimeAdjustedCallback); // 启动消息定时发送任务 userScheduler.addTask(taskSendMessage); taskSendMessage.enable(); } void loop() { // 维护并更新MESH网络 mesh.update(); }
文章来源: https://iotsmart.blog.csdn.net/article/details/127743419
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!