久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 7436|回復: 0
打印 上一主題 下一主題
收起左側

【零知ESP8266】教程:MESH 組網示例

[復制鏈接]
跳轉到指定樓層
樓主
本帖最后由 roc2 于 2019-6-19 11:54 編輯

1、概述
MESH組網技術在IOT領域具有非常大的作用,應用非常廣泛,主流的無線技術從最開始的Zigbee,到藍牙,到這里的WIFI都實現了MESH組網技術。在這里使用零知開源平臺演示WIFI WESH組網的使用。
2、軟件和硬件
硬件使用零知-ESP8266;

軟件使用零知開發工具,自帶示例:

3、方法步驟
(1)先在零知開發工具中打開HelloMesh示例,或者復制下面的代碼到零知開發工具中:
  1. /**********************************************************
  2. *    文件: x.ino      by 零知實驗室
  3. *    -^^- 零知開源,讓電子制作變得更簡單! -^^-
  4. *    時間: 2019/05/28 12:22
  5. *    說明:
  6. ************************************************************/
  7. #include <ESP8266WiFi.h>
  8. #include <ESP8266WiFiMesh.h>
  9. #include <TypeConversionFunctions.h>
  10. #include <assert.h>

  11. const char exampleMeshName[] PROGMEM = "MeshNode_";
  12. const char exampleWiFiPassword[] PROGMEM = "123456789";//ChangeThisWiFiPassword_TODO

  13. unsigned int requestNumber = 0;
  14. unsigned int responseNumber = 0;

  15. String manageRequest(const String &request, ESP8266WiFiMesh &meshInstance);
  16. transmission_status_t manageResponse(const String &response, ESP8266WiFiMesh &meshInstance);
  17. void networkFilter(int numberOfNetworks, ESP8266WiFiMesh &meshInstance);

  18. /* Create the mesh node object */
  19. ESP8266WiFiMesh meshNode = ESP8266WiFiMesh(manageRequest, manageResponse, networkFilter, FPSTR(exampleWiFiPassword), FPSTR(exampleMeshName), "", true);

  20. /**
  21.    Callback for when other nodes send you a request

  22.    @param request The request string received from another node in the mesh
  23.    @param meshInstance The ESP8266WiFiMesh instance that called the function.
  24.    @returns The string to send back to the other node
  25. */
  26. String manageRequest(const String &request, ESP8266WiFiMesh &meshInstance) {
  27.   // We do not store strings in flash (via F()) in this function.
  28.   // The reason is that the other node will be waiting for our response,
  29.   // so keeping the strings in RAM will give a (small) improvement in response time.
  30.   // Of course, it is advised to adjust this approach based on RAM requirements.

  31.   /* Print out received message */
  32.   Serial.print("Request received: ");
  33.   Serial.println(request);

  34.   /* return a string to send back */
  35.   return ("Hello world response #" + String(responseNumber++) + " from " + meshInstance.getMeshName() + meshInstance.getNodeID() + ".");
  36. }

  37. /**
  38.    Callback for when you get a response from other nodes

  39.    @param response The response string received from another node in the mesh
  40.    @param meshInstance The ESP8266WiFiMesh instance that called the function.
  41.    @returns The status code resulting from the response, as an int
  42. */
  43. transmission_status_t manageResponse(const String &response, ESP8266WiFiMesh &meshInstance) {
  44.   transmission_status_t statusCode = TS_TRANSMISSION_COMPLETE;

  45.   /* Print out received message */
  46.   Serial.print(F("Request sent: "));
  47.   Serial.println(meshInstance.getMessage());
  48.   Serial.print(F("Response received: "));
  49.   Serial.println(response);

  50.   // Our last request got a response, so time to create a new request.
  51.   meshInstance.setMessage(String(F("Hello world request #")) + String(++requestNumber) + String(F(" from "))
  52.                           + meshInstance.getMeshName() + meshInstance.getNodeID() + String(F(".")));

  53.   // (void)meshInstance; // This is useful to remove a "unused parameter" compiler warning. Does nothing else.
  54.   return statusCode;
  55. }

  56. /**
  57.    Callback used to decide which networks to connect to once a WiFi scan has been completed.

  58.    @param numberOfNetworks The number of networks found in the WiFi scan.
  59.    @param meshInstance The ESP8266WiFiMesh instance that called the function.
  60. */
  61. void networkFilter(int numberOfNetworks, ESP8266WiFiMesh &meshInstance) {
  62.   for (int networkIndex = 0; networkIndex < numberOfNetworks; ++networkIndex) {
  63.     String currentSSID = WiFi.SSID(networkIndex);
  64.     int meshNameIndex = currentSSID.indexOf(meshInstance.getMeshName());

  65.     /* Connect to any _suitable_ APs which contain meshInstance.getMeshName() */
  66.     if (meshNameIndex >= 0) {
  67.       uint64_t targetNodeID = stringToUint64(currentSSID.substring(meshNameIndex + meshInstance.getMeshName().length()));

  68.       if (targetNodeID < stringToUint64(meshInstance.getNodeID())) {
  69.         ESP8266WiFiMesh::connectionQueue.push_back(NetworkInfo(networkIndex));
  70.       }
  71.     }
  72.   }
  73. }

  74. void setup() {
  75.   // Prevents the flash memory from being worn out, see: https://github.com/esp8266/Arduino/issues/1054 .
  76.   // 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.
  77.   WiFi.persistent(false);

  78.   Serial.begin(115200);
  79.   delay(50); // Wait for Serial.

  80.   //yield(); // Use this if you don't want to wait for Serial.

  81.   // The WiFi.disconnect() ensures that the WiFi is working correctly. If this is not done before receiving WiFi connections,
  82.   // those WiFi connections will take a long time to make or sometimes will not work at all.
  83.   WiFi.disconnect();

  84.   Serial.println();
  85.   Serial.println();

  86.   Serial.println(F("Note that this library can use static IP:s for the nodes to speed up connection times.\n"
  87.                    "Use the setStaticIP method as shown in this example to enable this.\n"
  88.                    "Ensure that nodes connecting to the same AP have distinct static IP:s.\n"
  89.                    "Also, remember to change the default mesh network password!\n\n"));

  90.   Serial.println(F("Setting up mesh node..."));

  91.   /* Initialise the mesh node */
  92.   meshNode.begin();
  93.   meshNode.activateAP(); // Each AP requires a separate server port.
  94. //  meshNode.setStaticIP(IPAddress(192, 168, 4, 22)); // Activate static IP mode to speed up connection times.
  95. }

  96. int32_t timeOfLastScan = -10000;
  97. void loop() {
  98.   if (millis() - timeOfLastScan > 3000 // Give other nodes some time to connect between data transfers.
  99.       || (WiFi.status() != WL_CONNECTED && millis() - timeOfLastScan > 2000)) { // Scan for networks with two second intervals when not already connected.
  100.     String request = String(F("Hello world request #")) + String(requestNumber) + String(F(" from ")) + meshNode.getMeshName() + meshNode.getNodeID() + String(F("."));
  101.     meshNode.attemptTransmission(request, false);
  102.     timeOfLastScan = millis();

  103.     // One way to check how attemptTransmission worked out
  104.     if (ESP8266WiFiMesh::latestTransmissionSuccessful()) {
  105.       Serial.println(F("Transmission successful."));
  106.     }

  107.     // Another way to check how attemptTransmission worked out
  108.     if (ESP8266WiFiMesh::latestTransmissionOutcomes.empty()) {
  109.       Serial.println(F("No mesh AP found."));
  110.     } else {
  111.       for (TransmissionResult &transmissionResult : ESP8266WiFiMesh::latestTransmissionOutcomes) {
  112.         if (transmissionResult.transmissionStatus == TS_TRANSMISSION_FAILED) {
  113.           Serial.println(String(F("Transmission failed to mesh AP ")) + transmissionResult.SSID);
  114.         } else if (transmissionResult.transmissionStatus == TS_CONNECTION_FAILED) {
  115.           Serial.println(String(F("Connection failed to mesh AP ")) + transmissionResult.SSID);
  116.         } else if (transmissionResult.transmissionStatus == TS_TRANSMISSION_COMPLETE) {
  117.           // No need to do anything, transmission was successful.
  118.         } else {
  119.           Serial.println(String(F("Invalid transmission status for ")) + transmissionResult.SSID + String(F("!")));
  120.           assert(F("Invalid transmission status returned from responseHandler!") && false);
  121.         }
  122.       }
  123.     }
  124.     Serial.println();
  125.   } else {
  126.     /* Accept any incoming connections */
  127.     meshNode.acceptRequest();
  128.   }
  129. }
復制代碼
(2)驗證并上傳上述代碼到零知-ESP8266開發板;
(3)測試:分別把上述代碼上傳到兩個零知-ESP8266開發板,然后分別連接兩個板子的串口調試窗口,然后就可以看到兩個節點數據傳輸信息了:

注意:密碼修改時候字符要大于8個字符,不然會一直提示No mesh AP found。

更多詳細資料可到零知實驗室官網免費獲取。





分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 午夜精品久久久久久不卡欧美一级 | 精品国产乱码久久久久久蜜柚 | 欧美日韩在线一区二区三区 | 中文字幕免费在线 | 日韩精品一区二区三区四区视频 | 中文字幕一区二区不卡 | 高清色| 成人影视网 | 欧美久久不卡 | 亚洲91| 福利av在线| 国产福利91精品 | 精品视频免费 | 中文字幕在线人 | 精品中文字幕一区 | 国产精品无码久久久久 | 成人免费高清 | 久久国产精品72免费观看 | 国产精品久久久久aaaa樱花 | 粉嫩国产精品一区二区在线观看 | 国产小视频在线看 | 99视频精品 | 亚洲精品国产偷自在线观看 | 国产成人综合久久 | 午夜伦理影院 | 国产在线资源 | 久久99精品久久久久久青青日本 | 久久精品中文字幕 | 精品视频在线免费观看 | 亚洲综合视频 | 欧美成人高清视频 | 精品视频在线观看 | 亚洲 自拍 另类 欧美 丝袜 | 操久久| 亚洲人成一区二区三区性色 | 国产亚洲精品成人av久久ww | 中文二区 | 日本精a在线观看 | 欧美激情视频一区二区三区在线播放 | 国产在线麻豆精品入口 | 亚洲精品亚洲人成人网 |