esp_mesh.rst 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. ESP-MESH Programming Guide
  2. ==========================
  3. This is a programming guide for ESP-MESH, including the API reference and coding examples. This guide is split into the following parts:
  4. 1. :ref:`mesh-programming-model`
  5. 2. :ref:`mesh-writing-mesh-application`
  6. 3. :ref:`mesh-self-organized-behavior`
  7. 4. :ref:`mesh-application-examples`
  8. 5. :ref:`mesh-api-reference`
  9. For documentation regarding the ESP-MESH protocol, please see the :doc:`ESP-MESH API Guide<../../api-guides/mesh>`.
  10. .. ---------------------- ESP-MESH Programming Model --------------------------
  11. .. _mesh-programming-model:
  12. ESP-MESH Programming Model
  13. --------------------------
  14. Software Stack
  15. ^^^^^^^^^^^^^^
  16. The ESP-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-MESH software stack.
  17. .. _mesh-going-to-software-stack:
  18. .. figure:: ../../../_static/mesh-software-stack.png
  19. :align: center
  20. :alt: ESP-MESH Software Stack
  21. :figclass: align-center
  22. ESP-MESH Software Stack
  23. System Events
  24. ^^^^^^^^^^^^^
  25. An application interfaces with ESP-MESH via **ESP-MESH Events**. Since ESP-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-MESH application.
  26. .. figure:: ../../../_static/mesh-events-delivery.png
  27. :align: center
  28. :alt: ESP-MESH System Events Delivery
  29. :figclass: align-center
  30. ESP-MESH System Events Delivery
  31. The :cpp:type:`mesh_event_id_t` defines all possible ESP-MESH system events and can indicate events such as the connection/disconnection of parent/child. Before ESP-MESH system events can be used, the application must register a **Mesh Event Callback** via :cpp:func:`esp_mesh_set_config`. The callback is used to receive events from the ESP-MESH stack as well as the LwIP Stack and should contain handlers for each event relevant to the application.
  32. Typical use cases of system 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:`MESH_EVENT_ROOT_GOT_IP` and :cpp:enumerator:`MESH_EVENT_ROOT_LOST_IP` can be used to indicate when the root node can and cannot transmit data to the external IP network.
  33. .. warning::
  34. When using ESP-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-MESH's self-organizing behavior**. Therefore, user's should not call Wi-Fi APIs after :cpp:func:`esp_mesh_start` is called, and before :cpp:func:`esp_mesh_stop` is called.
  35. LwIP & ESP-MESH
  36. ^^^^^^^^^^^^^^^
  37. The application can access the ESP-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.
  38. **Each node is required to initialize LwIP by calling** :cpp:func:`tcpip_adapter_init`. In order to prevent non-root node access to LwIP, the application should stop the following services after LwIP initialization:
  39. - DHCP server service on the softAP interface.
  40. - DHCP client service on the station interface.
  41. The following code snippet demonstrates how to initialize LwIP for ESP-MESH applications.
  42. .. code-block:: c
  43. /* tcpip initialization */
  44. tcpip_adapter_init();
  45. /*
  46. * for mesh
  47. * stop DHCP server on softAP interface by default
  48. * stop DHCP client on station interface by default
  49. */
  50. ESP_ERROR_CHECK(tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP));
  51. ESP_ERROR_CHECK(tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA));
  52. /* do not specify system event callback, use NULL instead. */
  53. ESP_ERROR_CHECK(esp_event_loop_init(NULL, NULL));
  54. .. note::
  55. ESP-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.
  56. .. ---------------------- Writing a Mesh Application --------------------------
  57. .. _mesh-writing-mesh-application:
  58. Writing an ESP-MESH Application
  59. -------------------------------
  60. The prerequisites for starting ESP-MESH is to initialize LwIP and Wi-Fi, The following code snippet demonstrates the necessary prerequisite steps before ESP-MESH itself can be initialized.
  61. .. code-block:: c
  62. tcpip_adapter_init();
  63. /*
  64. * for mesh
  65. * stop DHCP server on softAP interface by default
  66. * stop DHCP client on station interface by default
  67. */
  68. ESP_ERROR_CHECK(tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP));
  69. ESP_ERROR_CHECK(tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA));
  70. /* do not specify system event callback, use NULL instead. */
  71. ESP_ERROR_CHECK(esp_event_loop_init(NULL, NULL));
  72. /* Wi-Fi initialization */
  73. wifi_init_config_t config = WIFI_INIT_CONFIG_DEFAULT();
  74. ESP_ERROR_CHECK(esp_wifi_init(&config));
  75. ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_FLASH));
  76. ESP_ERROR_CHECK(esp_wifi_start());
  77. After initializing LwIP and Wi-Fi, the process of getting an ESP-MESH network up and running can be summarized into the following three steps:
  78. 1. :ref:`mesh-initialize-mesh`
  79. 2. :ref:`mesh-configuring-mesh`
  80. 3. :ref:`mesh-start-mesh`
  81. .. _mesh-initialize-mesh:
  82. Initialize Mesh
  83. ^^^^^^^^^^^^^^^
  84. The following code snippet demonstrates how to initialize ESP-MESH
  85. .. code-block:: c
  86. /* mesh initialization */
  87. ESP_ERROR_CHECK(esp_mesh_init());
  88. .. _mesh-configuring-mesh:
  89. Configuring an ESP-MESH Network
  90. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  91. .. todo - Add note about unified configuration
  92. ESP-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-MESH:
  93. +------------------+-------------------------------------+
  94. | Parameter | Description |
  95. +==================+=====================================+
  96. | Channel | Range from 1 to 14 |
  97. +------------------+-------------------------------------+
  98. | Event Callback | Callback for Mesh Events, |
  99. | | see :cpp:type:`mesh_event_cb_t` |
  100. +------------------+-------------------------------------+
  101. | Mesh ID | ID of ESP-MESH Network, |
  102. | | see :cpp:type:`mesh_addr_t` |
  103. +------------------+-------------------------------------+
  104. | Router | Router Configuration, |
  105. | | see :cpp:type:`mesh_router_t` |
  106. +------------------+-------------------------------------+
  107. | Mesh AP | Mesh AP Configuration, |
  108. | | see :cpp:type:`mesh_ap_cfg_t` |
  109. +------------------+-------------------------------------+
  110. | Crypto Functions | Crypto Functions for Mesh IE, |
  111. | | see :cpp:type:`mesh_crypto_funcs_t` |
  112. +------------------+-------------------------------------+
  113. The following code snippet demonstrates how to configure ESP-MESH.
  114. .. code-block:: c
  115. /* Enable the Mesh IE encryption by default */
  116. mesh_cfg_t cfg = MESH_INIT_CONFIG_DEFAULT();
  117. /* mesh ID */
  118. memcpy((uint8_t *) &cfg.mesh_id, MESH_ID, 6);
  119. /* mesh event callback */
  120. cfg.event_cb = &mesh_event_handler;
  121. /* channel (must match the router's channel) */
  122. cfg.channel = CONFIG_MESH_CHANNEL;
  123. /* router */
  124. cfg.router.ssid_len = strlen(CONFIG_MESH_ROUTER_SSID);
  125. memcpy((uint8_t *) &cfg.router.ssid, CONFIG_MESH_ROUTER_SSID, cfg.router.ssid_len);
  126. memcpy((uint8_t *) &cfg.router.password, CONFIG_MESH_ROUTER_PASSWD,
  127. strlen(CONFIG_MESH_ROUTER_PASSWD));
  128. /* mesh softAP */
  129. cfg.mesh_ap.max_connection = CONFIG_MESH_AP_CONNECTIONS;
  130. memcpy((uint8_t *) &cfg.mesh_ap.password, CONFIG_MESH_AP_PASSWD,
  131. strlen(CONFIG_MESH_AP_PASSWD));
  132. ESP_ERROR_CHECK(esp_mesh_set_config(&cfg));
  133. .. _mesh-start-mesh:
  134. Start Mesh
  135. ^^^^^^^^^^
  136. The following code snippet demonstrates how to start ESP-MESH.
  137. .. code-block:: c
  138. /* mesh start */
  139. ESP_ERROR_CHECK(esp_mesh_start());
  140. After starting ESP-MESH, the application should check for ESP-MESH events to determine when it has connected to the network. After connecting, the application can start transmitting and receiving packets over the ESP-MESH network using :cpp:func:`esp_mesh_send` and :cpp:func:`esp_mesh_recv`.
  141. .. --------------------- ESP-MESH Application Examples ------------------------
  142. .. _mesh-self-organized-behavior:
  143. Self Organized Networking
  144. -------------------------
  145. Self organized networking is a feature of ESP-MESH where nodes can autonomously scan/select/connect/reconnect to other nodes and routers. This feature allows an ESP-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-MESH network are able to carryout the following actions without autonomously:
  146. - Selection or election of the root node (see **Automatic Root Node Selection** in :ref:`mesh-building-a-network`)
  147. - Selection of a preferred parent node (see **Parent Node Selection** in :ref:`mesh-building-a-network`)
  148. - Automatic reconnection upon detecting a disconnection (see **Intermediate Parent Node Failure** in :ref:`mesh-managing-a-network`)
  149. When self organized networking is enabled, the ESP-MESH stack will internally make calls to Wi-Fi driver APIs. Therefore, **the application layer should not make any calls to Wi-Fi driver APIs whilst self organized networking is enabled as doing so would risk interfering with ESP-MESH**.
  150. Toggling Self Organized Networking
  151. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  152. 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:
  153. - ``bool enable`` specifies whether to enable or disable self organized networking.
  154. - ``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.
  155. Disabling Self Organized Networking
  156. """""""""""""""""""""""""""""""""""
  157. The following code snippet demonstrates how to disable self organized networking.
  158. .. code-block:: c
  159. //Disable self organized networking
  160. esp_mesh_set_self_organized(false, false);
  161. ESP-MESH will attempt to maintain the node's current Wi-Fi state when disabling self organized networking.
  162. - If the node was previously connected to other nodes, it will remain connected.
  163. - If the node was previously disconnected and was scanning for a parent node or router, it will stop scanning.
  164. - If the node was previously attempting to reconnect to a parent node or router, it will stop reconnecting.
  165. Enabling Self Organized Networking
  166. """"""""""""""""""""""""""""""""""
  167. ESP-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.
  168. +---------------+--------------+------------------------------------------------------------------------------------------------------------------+
  169. | Select Parent | Is Root Node | Effects |
  170. +===============+==============+==================================================================================================================+
  171. | N | N | - Nodes already connected to a parent node will remain connected. |
  172. | | | - Nodes previously scanning for a parent nodes will stop scanning. Call :cpp:func:`esp_mesh_connect` to restart. |
  173. | +--------------+------------------------------------------------------------------------------------------------------------------+
  174. | | Y | - A root node already connected to router will stay connected. |
  175. | | | - A root node disconnected from router will need to call :cpp:func:`esp_mesh_connect` to reconnect. |
  176. +---------------+--------------+------------------------------------------------------------------------------------------------------------------+
  177. | Y | N | - Nodes without a parent node will automatically select a preferred parent and connect. |
  178. | | | - Nodes already connected to a parent node will disconnect, reselect a preferred parent node, and connect. |
  179. | +--------------+------------------------------------------------------------------------------------------------------------------+
  180. | | 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 |
  181. | | | disconnect from the router and all child nodes, select a preferred parent node, and connect. |
  182. +---------------+--------------+------------------------------------------------------------------------------------------------------------------+
  183. The following code snipping demonstrates how to enable self organized networking.
  184. .. code-block:: c
  185. //Enable self organized networking and select a new parent
  186. esp_mesh_set_self_organized(true, true);
  187. ...
  188. //Enable self organized networking and manually reconnect
  189. esp_mesh_set_self_organized(true, false);
  190. esp_mesh_connect();
  191. Calling Wi-Fi Driver API
  192. ^^^^^^^^^^^^^^^^^^^^^^^^
  193. There can be instances in which an application may want to directly call Wi-Fi driver API whilst using ESP-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 driver APIs**. This will prevent the ESP-MESH stack from attempting to call any Wi-Fi driver APIs and potentially interfering with the application's calls.
  194. Therefore, application calls to Wi-Fi driver 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-MESH.
  195. .. code-block:: c
  196. //Disable self organized networking
  197. esp_mesh_set_self_organized(0, 0);
  198. //Stop any scans already in progress
  199. esp_wifi_scan_stop();
  200. //Manually start scan. Will automatically stop when run to completion
  201. esp_wifi_scan_start();
  202. //Process scan results
  203. ...
  204. //Re-enable self organized networking if still connected
  205. esp_mesh_set_self_organized(1, 0);
  206. ...
  207. //Re-enable self organized networking if non-root and disconnected
  208. esp_mesh_set_self_organized(1, 1);
  209. ...
  210. //Re-enable self organized networking if root and disconnected
  211. esp_mesh_set_self_organized(1, 0); //Don't select new parent
  212. esp_mesh_connect(); //Manually reconnect to router
  213. .. --------------------- ESP-MESH Application Examples ------------------------
  214. .. _mesh-application-examples:
  215. Application Examples
  216. --------------------
  217. ESP-IDF contains these ESP-MESH example projects:
  218. :example:`The Internal Communication Example<mesh/internal_communication>` demonstrates how to setup a ESP-MESH network and have the root node send a data packet to every node within the network.
  219. :example:`The Manual Networking Example<mesh/manual_networking>` demonstrates how to use ESP-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.
  220. .. ------------------------- ESP-MESH API Reference ---------------------------
  221. .. _mesh-api-reference:
  222. API Reference
  223. --------------
  224. .. include:: /_build/inc/esp_mesh.inc