esp-wifi-mesh.rst 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. ESP-WIFI-MESH Programming Guide
  2. ===============================
  3. :link_to_translation:`zh_CN:[中文]`
  4. This is a programming guide for ESP-WIFI-MESH, including the API reference and coding examples. This guide is split into the following parts:
  5. 1. :ref:`mesh-programming-model`
  6. 2. :ref:`mesh-writing-mesh-application`
  7. 3. :ref:`mesh-self-organized-behavior`
  8. 4. :ref:`mesh-application-examples`
  9. 5. :ref:`mesh-api-reference`
  10. For documentation regarding the ESP-WIFI-MESH protocol, please see the :doc:`ESP-WIFI-MESH API Guide <../../api-guides/esp-wifi-mesh>`. For more information about ESP-WIFI-MESH Development Framework, please see `ESP-WIFI-MESH Development Framework <https://github.com/espressif/esp-mdf>`_.
  11. .. ---------------------- ESP-WIFI-MESH Programming Model --------------------------
  12. .. _mesh-programming-model:
  13. ESP-WIFI-MESH Programming Model
  14. -------------------------------------
  15. Software Stack
  16. ^^^^^^^^^^^^^^
  17. The ESP-WIFI-MESH software stack is built atop the Wi-Fi Driver/FreeRTOS and may use the LwIP Stack in some instances (i.e., the root node). The following diagram illustrates the ESP-WIFI-MESH software stack.
  18. .. _mesh-going-to-software-stack:
  19. .. figure:: ../../../_static/mesh-software-stack.png
  20. :align: center
  21. :alt: ESP-WIFI-MESH Software Stack
  22. :figclass: align-center
  23. ESP-WIFI-MESH Software Stack
  24. .. _mesh-events:
  25. System Events
  26. ^^^^^^^^^^^^^
  27. An application interfaces with ESP-WIFI-MESH via **ESP-WIFI-MESH Events**. Since ESP-WIFI-MESH is built atop the Wi-Fi stack, it is also possible for the application to interface with the Wi-Fi driver via the **Wi-Fi Event Task**. The following diagram illustrates the interfaces for the various System Events in an ESP-WIFI-MESH application.
  28. .. figure:: ../../../_static/mesh-events-delivery.png
  29. :align: center
  30. :alt: ESP-WIFI-MESH System Events Delivery
  31. :figclass: align-center
  32. ESP-WIFI-MESH System Events Delivery
  33. The :cpp:type:`mesh_event_id_t` defines all possible ESP-WIFI-MESH events and can indicate events such as the connection/disconnection of parent/child. Before ESP-WIFI-MESH events can be used, the application must register a **Mesh Events handler** via :cpp:func:`esp_event_handler_register` to the default event task. The Mesh Events handler that is registered contain handlers for each ESP-WIFI-MESH event relevant to the application.
  34. Typical use cases of mesh events include using events such as :cpp:enumerator:`MESH_EVENT_PARENT_CONNECTED` and :cpp:enumerator:`MESH_EVENT_CHILD_CONNECTED` to indicate when a node can begin transmitting data upstream and downstream respectively. Likewise, :cpp:enumerator:`IP_EVENT_STA_GOT_IP` and :cpp:enumerator:`IP_EVENT_STA_LOST_IP` can be used to indicate when the root node can and cannot transmit data to the external IP network.
  35. .. warning::
  36. When using ESP-WIFI-MESH under self-organized mode, users must ensure that no calls to Wi-Fi API are made. This is due to the fact that the self-organizing mode will internally make Wi-Fi API calls to connect/disconnect/scan etc. **Any Wi-Fi calls from the application (including calls from callbacks and handlers of Wi-Fi events) may interfere with ESP-WIFI-MESH's self-organizing behavior**. Therefore, users should not call Wi-Fi APIs after :cpp:func:`esp_mesh_start` is called, and before :cpp:func:`esp_mesh_stop` is called.
  37. LwIP & ESP-WIFI-MESH
  38. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  39. The application can access the ESP-WIFI-MESH stack directly without having to go through the LwIP stack. The LwIP stack is only required by the root node to transmit/receive data to/from an external IP network. However, since every node can potentially become the root node (due to automatic root node selection), each node must still initialize the LwIP stack.
  40. **Each node that could become root is required to initialize LwIP by calling** :cpp:func:`esp_netif_init`. In order to prevent non-root node access to LwIP, the application should not create or register any network interfaces using esp_netif APIs.
  41. ESP-WIFI-MESH requires a root node to be connected with a router. Therefore, in the event that a node becomes the root, **the corresponding handler must start the DHCP client service and immediately obtain an IP address**. Doing so will allow other nodes to begin transmitting/receiving packets to/from the external IP network. However, this step is unnecessary if static IP settings are used.
  42. .. ---------------------- Writing a Mesh Application --------------------------
  43. .. _mesh-writing-mesh-application:
  44. Writing an ESP-WIFI-MESH Application
  45. -------------------------------------------
  46. The prerequisites for starting ESP-WIFI-MESH is to initialize LwIP and Wi-Fi, The following code snippet demonstrates the necessary prerequisite steps before ESP-WIFI-MESH itself can be initialized.
  47. .. code-block:: c
  48. ESP_ERROR_CHECK(esp_netif_init());
  49. /* event initialization */
  50. ESP_ERROR_CHECK(esp_event_loop_create_default());
  51. /* Wi-Fi initialization */
  52. wifi_init_config_t config = WIFI_INIT_CONFIG_DEFAULT();
  53. ESP_ERROR_CHECK(esp_wifi_init(&config));
  54. /* register IP events handler */
  55. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &ip_event_handler, NULL));
  56. ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_FLASH));
  57. ESP_ERROR_CHECK(esp_wifi_start());
  58. After initializing LwIP and Wi-Fi, the process of getting an ESP-WIFI-MESH network up and running can be summarized into the following three steps:
  59. 1. :ref:`mesh-initialize-mesh`
  60. 2. :ref:`mesh-configuring-mesh`
  61. 3. :ref:`mesh-start-mesh`
  62. .. _mesh-initialize-mesh:
  63. Initialize Mesh
  64. ^^^^^^^^^^^^^^^
  65. The following code snippet demonstrates how to initialize ESP-WIFI-MESH
  66. .. code-block:: c
  67. /* mesh initialization */
  68. ESP_ERROR_CHECK(esp_mesh_init());
  69. /* register mesh events handler */
  70. ESP_ERROR_CHECK(esp_event_handler_register(MESH_EVENT, ESP_EVENT_ANY_ID, &mesh_event_handler, NULL));
  71. .. _mesh-configuring-mesh:
  72. Configuring an ESP-WIFI-MESH Network
  73. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  74. .. todo - Add note about unified configuration
  75. ESP-WIFI-MESH is configured via :cpp:func:`esp_mesh_set_config` which receives its arguments using the :cpp:type:`mesh_cfg_t` structure. The structure contains the following parameters used to configure ESP-WIFI-MESH:
  76. .. list-table::
  77. :header-rows: 1
  78. :widths: 15 25
  79. * - Parameter
  80. - Description
  81. * - Channel
  82. - Range from 1 to 14
  83. * - Mesh ID
  84. - ID of ESP-WIFI-MESH Network, see :cpp:type:`mesh_addr_t`
  85. * - Router
  86. - Router Configuration, see :cpp:type:`mesh_router_t`
  87. * - Mesh AP
  88. - Mesh AP Configuration, see :cpp:type:`mesh_ap_cfg_t`
  89. * - Crypto Functions
  90. - Crypto Functions for Mesh IE, see :cpp:type:`mesh_crypto_funcs_t`
  91. The following code snippet demonstrates how to configure ESP-WIFI-MESH.
  92. .. code-block:: c
  93. /* Enable the Mesh IE encryption by default */
  94. mesh_cfg_t cfg = MESH_INIT_CONFIG_DEFAULT();
  95. /* mesh ID */
  96. memcpy((uint8_t *) &cfg.mesh_id, MESH_ID, 6);
  97. /* channel (must match the router's channel) */
  98. cfg.channel = CONFIG_MESH_CHANNEL;
  99. /* router */
  100. cfg.router.ssid_len = strlen(CONFIG_MESH_ROUTER_SSID);
  101. memcpy((uint8_t *) &cfg.router.ssid, CONFIG_MESH_ROUTER_SSID, cfg.router.ssid_len);
  102. memcpy((uint8_t *) &cfg.router.password, CONFIG_MESH_ROUTER_PASSWD,
  103. strlen(CONFIG_MESH_ROUTER_PASSWD));
  104. /* mesh softAP */
  105. cfg.mesh_ap.max_connection = CONFIG_MESH_AP_CONNECTIONS;
  106. memcpy((uint8_t *) &cfg.mesh_ap.password, CONFIG_MESH_AP_PASSWD,
  107. strlen(CONFIG_MESH_AP_PASSWD));
  108. ESP_ERROR_CHECK(esp_mesh_set_config(&cfg));
  109. .. _mesh-start-mesh:
  110. Start Mesh
  111. ^^^^^^^^^^
  112. The following code snippet demonstrates how to start ESP-WIFI-MESH.
  113. .. code-block:: c
  114. /* mesh start */
  115. ESP_ERROR_CHECK(esp_mesh_start());
  116. After starting ESP-WIFI-MESH, the application should check for ESP-WIFI-MESH events to determine when it has connected to the network. After connecting, the application can start transmitting and receiving packets over the ESP-WIFI-MESH network using :cpp:func:`esp_mesh_send` and :cpp:func:`esp_mesh_recv`.
  117. .. --------------------- ESP-WIFI-MESH Application Examples ------------------------
  118. .. _mesh-self-organized-behavior:
  119. Self-Organized Networking
  120. -------------------------
  121. Self-organized networking is a feature of ESP-WIFI-MESH where nodes can autonomously scan/select/connect/reconnect to other nodes and routers. This feature allows an ESP-WIFI-MESH network to operate with high degree of autonomy by making the network robust to dynamic network topologies and conditions. With self-organized networking enabled, nodes in an ESP-WIFI-MESH network are able to carry out the following actions without autonomously:
  122. - Selection or election of the root node (see **Automatic Root Node Selection** in :ref:`mesh-building-a-network`)
  123. - Selection of a preferred parent node (see **Parent Node Selection** in :ref:`mesh-building-a-network`)
  124. - Automatic reconnection upon detecting a disconnection (see **Intermediate Parent Node Failure** in :ref:`mesh-managing-a-network`)
  125. When self-organized networking is enabled, the ESP-WIFI-MESH stack will internally make calls to Wi-Fi APIs. Therefore, **the application layer should not make any calls to Wi-Fi APIs whilst self-organized networking is enabled as doing so would risk interfering with ESP-WIFI-MESH**.
  126. Toggling Self-Organized Networking
  127. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  128. Self-organized networking can be enabled or disabled by the application at runtime by calling the :cpp:func:`esp_mesh_set_self_organized` function. The function has the two following parameters:
  129. - ``bool enable`` specifies whether to enable or disable self-organized networking.
  130. - ``bool select_parent`` specifies whether a new parent node should be selected when enabling self-organized networking. Selecting a new parent has different effects depending the node type and the node's current state. This parameter is unused when disabling self-organized networking.
  131. Disabling Self-Organized Networking
  132. """""""""""""""""""""""""""""""""""
  133. The following code snippet demonstrates how to disable self-organized networking.
  134. .. code-block:: c
  135. //Disable self-organized networking
  136. esp_mesh_set_self_organized(false, false);
  137. ESP-WIFI-MESH will attempt to maintain the node's current Wi-Fi state when disabling self-organized networking.
  138. - If the node was previously connected to other nodes, it will remain connected.
  139. - If the node was previously disconnected and was scanning for a parent node or router, it will stop scanning.
  140. - If the node was previously attempting to reconnect to a parent node or router, it will stop reconnecting.
  141. Enabling Self-Organized Networking
  142. """"""""""""""""""""""""""""""""""
  143. ESP-WIFI-MESH will attempt to maintain the node's current Wi-Fi state when enabling self-organized networking. However, depending on the node type and whether a new parent is selected, the Wi-Fi state of the node can change. The following table shows effects of enabling self-organized networking.
  144. +---------------+--------------+------------------------------------------------------------------------------------------------------------------+
  145. | Select Parent | Is Root Node | Effects |
  146. +===============+==============+==================================================================================================================+
  147. | N | N | - Nodes already connected to a parent node will remain connected. |
  148. | | | - Nodes previously scanning for a parent nodes will stop scanning. Call :cpp:func:`esp_mesh_connect` to restart. |
  149. | +--------------+------------------------------------------------------------------------------------------------------------------+
  150. | | Y | - A root node already connected to router will stay connected. |
  151. | | | - A root node disconnected from router will need to call :cpp:func:`esp_mesh_connect` to reconnect. |
  152. +---------------+--------------+------------------------------------------------------------------------------------------------------------------+
  153. | Y | N | - Nodes without a parent node will automatically select a preferred parent and connect. |
  154. | | | - Nodes already connected to a parent node will disconnect, reselect a preferred parent node, and connect. |
  155. | +--------------+------------------------------------------------------------------------------------------------------------------+
  156. | | Y | - For a root node to connect to a parent node, it must give up it's role as root. Therefore, a root node will |
  157. | | | disconnect from the router and all child nodes, select a preferred parent node, and connect. |
  158. +---------------+--------------+------------------------------------------------------------------------------------------------------------------+
  159. The following code snipping demonstrates how to enable self-organized networking.
  160. .. code-block:: c
  161. //Enable self-organized networking and select a new parent
  162. esp_mesh_set_self_organized(true, true);
  163. ...
  164. //Enable self-organized networking and manually reconnect
  165. esp_mesh_set_self_organized(true, false);
  166. esp_mesh_connect();
  167. Calling Wi-Fi API
  168. ^^^^^^^^^^^^^^^^^
  169. There can be instances in which an application may want to directly call Wi-Fi API whilst using ESP-WIFI-MESH. For example, an application may want to manually scan for neighboring APs. However, **self-organized networking must be disabled before the application calls any Wi-Fi APIs**. This will prevent the ESP-WIFI-MESH stack from attempting to call any Wi-Fi APIs and potentially interfering with the application's calls.
  170. Therefore, application calls to Wi-Fi APIs should be placed in between calls of :cpp:func:`esp_mesh_set_self_organized` which disable and enable self-organized networking. The following code snippet demonstrates how an application can safely call :cpp:func:`esp_wifi_scan_start` whilst using ESP-WIFI-MESH.
  171. .. code-block:: c
  172. //Disable self-organized networking
  173. esp_mesh_set_self_organized(0, 0);
  174. //Stop any scans already in progress
  175. esp_wifi_scan_stop();
  176. //Manually start scan. Will automatically stop when run to completion
  177. esp_wifi_scan_start();
  178. //Process scan results
  179. ...
  180. //Re-enable self-organized networking if still connected
  181. esp_mesh_set_self_organized(1, 0);
  182. ...
  183. //Re-enable self-organized networking if non-root and disconnected
  184. esp_mesh_set_self_organized(1, 1);
  185. ...
  186. //Re-enable self-organized networking if root and disconnected
  187. esp_mesh_set_self_organized(1, 0); //Do not select new parent
  188. esp_mesh_connect(); //Manually reconnect to router
  189. .. --------------------- ESP-WIFI-MESH Application Examples ------------------------
  190. .. _mesh-application-examples:
  191. Application Examples
  192. --------------------
  193. ESP-IDF contains these ESP-WIFI-MESH example projects:
  194. :example:`The Internal Communication Example <mesh/internal_communication>` demonstrates how to set up a ESP-WIFI-MESH network and have the root node send a data packet to every node within the network.
  195. :example:`The Manual Networking Example <mesh/manual_networking>` demonstrates how to use ESP-WIFI-MESH without the self-organizing features. This example shows how to program a node to manually scan for a list of potential parent nodes and select a parent node based on custom criteria.
  196. .. ------------------------- ESP-WIFI-MESH API Reference ---------------------------
  197. .. _mesh-api-reference:
  198. API Reference
  199. --------------
  200. .. include-build-file:: inc/esp_mesh.inc