3804|0

85

帖子

0

TA的资源

一粒金砂(中级)

楼主
 

【零知ESP8266】教程:MESH 组网示例 [复制链接]

1、概述

MESH组网技术在IOT领域具有非常大的作用,应用非常广泛,主流的无线技术从最开始的Zigbee,到蓝牙,到这里的WIFI都实现了MESH组网技术。在这里使用零知开源平台演示WIFI WESH组网的使用。

2、软件和硬件

硬件使用零知-ESP8266开发板;

软件使用零知开发工具,自带示例:

3、方法步骤

(1)先在零知开发工具中打开HelloMesh示例,或者复制下面的代码到零知开发工具中:

  • /**********************************************************
  • * 文件: x.ino by 零知实验室
  • * -^^- 零知开源,让电子制作变得更简单! -^^-
  • * 时间: 2019/05/28 12:22
  • * 说明:
  • ************************************************************/
  • #include <ESP8266WiFi.h>
  • #include <ESP8266WiFiMesh.h>
  • #include <TypeConversionFunctions.h>
  • #include <assert.h>
  • const char exampleMeshName[] PROGMEM = "MeshNode_";
  • const char exampleWiFiPassword[] PROGMEM = "123456789";//ChangeThisWiFiPassword_TODO
  • unsigned int requestNumber = 0;
  • unsigned int responseNumber = 0;
  • String manageRequest(const String &request, ESP8266WiFiMesh &meshInstance);
  • transmission_status_t manageResponse(const String &response, ESP8266WiFiMesh &meshInstance);
  • void networkFilter(int numberOfNetworks, ESP8266WiFiMesh &meshInstance);
  • /* Create the mesh node object */
  • ESP8266WiFiMesh meshNode = ESP8266WiFiMesh(manageRequest, manageResponse, networkFilter, FPSTR(exampleWiFiPassword), FPSTR(exampleMeshName), "", true);
  • /**
  • Callback for when other nodes send you a request
  • @param request The request string received from another node in the mesh
  • @param meshInstance The ESP8266WiFiMesh instance that called the function.
  • @returns The string to send back to the other node
  • */
  • String manageRequest(const String &request, ESP8266WiFiMesh &meshInstance) {
  • // We do not store strings in flash (via F()) in this function.
  • // The reason is that the other node will be waiting for our response,
  • // so keeping the strings in RAM will give a (small) improvement in response time.
  • // Of course, it is advised to adjust this approach based on RAM requirements.
  • /* Print out received message */
  • Serial.print("Request received: ");
  • Serial.println(request);
  • /* return a string to send back */
  • return ("Hello world response #" + String(responseNumber++) + " from " + meshInstance.getMeshName() + meshInstance.getNodeID() + ".");
  • }
  • /**
  • Callback for when you get a response from other nodes
  • @param response The response string received from another node in the mesh
  • @param meshInstance The ESP8266WiFiMesh instance that called the function.
  • @returns The status code resulting from the response, as an int
  • */
  • transmission_status_t manageResponse(const String &response, ESP8266WiFiMesh &meshInstance) {
  • transmission_status_t statusCode = TS_TRANSMISSION_COMPLETE;
  • /* Print out received message */
  • Serial.print(F("Request sent: "));
  • Serial.println(meshInstance.getMessage());
  • Serial.print(F("Response received: "));
  • Serial.println(response);
  • // Our last request got a response, so time to create a new request.
  • meshInstance.setMessage(String(F("Hello world request #")) + String(++requestNumber) + String(F(" from "))
  • + meshInstance.getMeshName() + meshInstance.getNodeID() + String(F(".")));
  • // (void)meshInstance; // This is useful to remove a "unused parameter" compiler warning. Does nothing else.
  • return statusCode;
  • }
  • /**
  • Callback used to decide which networks to connect to once a WiFi scan has been completed.
  • @param numberOfNetworks The number of networks found in the WiFi scan.
  • @param meshInstance The ESP8266WiFiMesh instance that called the function.
  • */
  • void networkFilter(int numberOfNetworks, ESP8266WiFiMesh &meshInstance) {
  • for (int networkIndex = 0; networkIndex < numberOfNetworks; ++networkIndex) {
  • String currentSSID = WiFi.SSID(networkIndex);
  • int meshNameIndex = currentSSID.indexOf(meshInstance.getMeshName());
  • /* Connect to any _suitable_ APs which contain meshInstance.getMeshName() */
  • if (meshNameIndex >= 0) {
  • uint64_t targetNodeID = stringToUint64(currentSSID.substring(meshNameIndex + meshInstance.getMeshName().length()));
  • if (targetNodeID < stringToUint64(meshInstance.getNodeID())) {
  • ESP8266WiFiMesh::connectionQueue.push_back(NetworkInfo(networkIndex));
  • }
  • }
  • }
  • }
  • void setup() {
  • // Prevents the flash memory from being worn out, see: https://github.com/esp8266/Arduino/issues/1054 .
  • // This will however delay node WiFi start-up by about 700 ms. The delay is 900 ms if we otherwise would have stored the WiFi network we want to connect to.
  • WiFi.persistent(false);
  • Serial.begin(115200);
  • delay(50); // Wait for Serial.
  • //yield(); // Use this if you don't want to wait for Serial.
  • // The WiFi.disconnect() ensures that the WiFi is working correctly. If this is not done before receiving WiFi connections,
  • // those WiFi connections will take a long time to make or sometimes will not work at all.
  • WiFi.disconnect();
  • Serial.println();
  • Serial.println();
  • Serial.println(F("Note that this library can use static IP:s for the nodes to speed up connection times.\n"
  • "Use the setStaticIP method as shown in this example to enable this.\n"
  • "Ensure that nodes connecting to the same AP have distinct static IP:s.\n"
  • "Also, remember to change the default mesh network password!\n\n"));
  • Serial.println(F("Setting up mesh node..."));
  • /* Initialise the mesh node */
  • meshNode.begin();
  • meshNode.activateAP(); // Each AP requires a separate server port.
  • // meshNode.setStaticIP(IPAddress(192, 168, 4, 22)); // Activate static IP mode to speed up connection times.
  • }
  • int32_t timeOfLastScan = -10000;
  • void loop() {
  • if (millis() - timeOfLastScan > 3000 // Give other nodes some time to connect between data transfers.
  • || (WiFi.status() != WL_CONNECTED && millis() - timeOfLastScan > 2000)) { // Scan for networks with two second intervals when not already connected.
  • String request = String(F("Hello world request #")) + String(requestNumber) + String(F(" from ")) + meshNode.getMeshName() + meshNode.getNodeID() + String(F("."));
  • meshNode.attemptTransmission(request, false);
  • timeOfLastScan = millis();
  • // One way to check how attemptTransmission worked out
  • if (ESP8266WiFiMesh::latestTransmissionSuccessful()) {
  • Serial.println(F("Transmission successful."));
  • }
  • // Another way to check how attemptTransmission worked out
  • if (ESP8266WiFiMesh::latestTransmissionOutcomes.empty()) {
  • Serial.println(F("No mesh AP found."));
  • } else {
  • for (TransmissionResult &transmissionResult : ESP8266WiFiMesh::latestTransmissionOutcomes) {
  • if (transmissionResult.transmissionStatus == TS_TRANSMISSION_FAILED) {
  • Serial.println(String(F("Transmission failed to mesh AP ")) + transmissionResult.SSID);
  • } else if (transmissionResult.transmissionStatus == TS_CONNECTION_FAILED) {
  • Serial.println(String(F("Connection failed to mesh AP ")) + transmissionResult.SSID);
  • } else if (transmissionResult.transmissionStatus == TS_TRANSMISSION_COMPLETE) {
  • // No need to do anything, transmission was successful.
  • } else {
  • Serial.println(String(F("Invalid transmission status for ")) + transmissionResult.SSID + String(F("!")));
  • assert(F("Invalid transmission status returned from responseHandler!") && false);
  • }
  • }
  • }
  • Serial.println();
  • } else {
  • /* Accept any incoming connections */
  • meshNode.acceptRequest();
  • }
  • }

(2)验证并上传上述代码到零知-ESP8266开发板;

(3)测试:分别把上述代码上传到两个零知-ESP8266开发板,然后分别连接两个板子的串口调试窗口,然后就可以看到两个节点数据传输信息了:

更多详细资料可到零知实验室官网免费获取。


此内容由EEWORLD论坛网友roc2原创,如需转载或用于商业用途需征得作者同意并注明出处

查看本帖全部内容,请登录或者注册
点赞 关注
 

回复
举报
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/10 下一条
有奖直播报名| TI 面向楼宇和工厂自动化行业的毫米波雷达解决方案
【内容简介】TI 60GHz IWRL6432和 IWRL1432毫米波雷达传感器如何帮助解决楼宇和工厂自动化应用中的感应难题
【直播时间】5月28日(周三)上午10:00
【直播礼品】小米双肩包、contigo水杯、胶囊伞、安克充电器

查看 »

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

 
机器人开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网 4

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表