esp_mesh.rst 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. ESP-MESH Programming Guide
  2. ==========================
  3. This is a programming guide for ESP-MESH, including the API reference and coding
  4. 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-application-examples`
  8. 4. :ref:`mesh-api-reference`
  9. For documentation regarding the ESP-MESH protocol, please see the
  10. :doc:`ESP-MESH API Guide<../../api-guides/mesh>`.
  11. .. ---------------------- ESP-MESH Programming Model --------------------------
  12. .. _mesh-programming-model:
  13. ESP-MESH Programming Model
  14. --------------------------
  15. Software Stack
  16. ^^^^^^^^^^^^^^
  17. The ESP-MESH software stack is built atop the Wi-Fi Driver/FreeRTOS and may use
  18. the LwIP Stack in some instances (i.e. the root node). The following diagram
  19. illustrates the ESP-MESH software stack.
  20. .. _mesh-going-to-software-stack:
  21. .. figure:: ../../../_static/mesh-software-stack.png
  22. :align: center
  23. :alt: ESP-MESH Software Stack
  24. :figclass: align-center
  25. ESP-MESH Software Stack
  26. System Events
  27. ^^^^^^^^^^^^^
  28. An application interfaces with ESP-MESH via **ESP-MESH Events**. Since ESP-MESH
  29. is built atop the Wi-Fi stack, it is also possible for the application to interface
  30. with the Wi-Fi driver via the **Wi-Fi Event Task**. The following diagram illustrates
  31. the interfaces for the various System Events in an ESP-MESH application.
  32. .. figure:: ../../../_static/mesh-events-delivery.png
  33. :align: center
  34. :alt: ESP-MESH System Events Delivery
  35. :figclass: align-center
  36. ESP-MESH System Events Delivery
  37. The :cpp:type:`mesh_event_id_t` defines all possible ESP-MESH system events and
  38. can indicate events such as the connection/disconnection of parent/child. Before
  39. ESP-MESH system events can be used, the application must register a **Mesh Event
  40. Callback** via :cpp:func:`esp_mesh_set_config`. The callback is used to receive
  41. events from the ESP-MESH stack as well as the LwIP Stack and should contain handlers
  42. for each event relevant to the application.
  43. Typical use cases of system events include using events such as
  44. :cpp:enumerator:`MESH_EVENT_PARENT_CONNECTED` and :cpp:enumerator:`MESH_EVENT_CHILD_CONNECTED`
  45. to indicate when a node can begin transmitting data upstream and downstream respectively. Likewise,
  46. :cpp:enumerator:`MESH_EVENT_ROOT_GOT_IP` and :cpp:enumerator:`MESH_EVENT_ROOT_LOST_IP` can be
  47. used to indicate when the root node can and cannot transmit data to the external IP
  48. network.
  49. .. warning::
  50. When using ESP-MESH under self-organized mode, users must ensure that no calls
  51. to Wi-Fi API are made. This is due to the fact that the self-organizing mode
  52. will internally make Wi-Fi API calls to connect/disconnect/scan etc.
  53. **Any Wi-Fi calls from the application (including calls from callbacks and
  54. handlers of Wi-Fi events) may interfere with ESP-MESH's self-organizing behavior**.
  55. Therefore, user's should not call Wi-Fi APIs after :cpp:func:`esp_mesh_start`
  56. is called, and before :cpp:func:`esp_mesh_stop` is called.
  57. LwIP & ESP-MESH
  58. ^^^^^^^^^^^^^^^
  59. The application can access the ESP-MESH stack directly without having to go through
  60. the LwIP stack. The LwIP stack is only required by the root node to transmit/receive
  61. data to/from an external IP network. However, since every node can potentially
  62. become the root node (due to automatic root node selection), each node must still
  63. initialize the LwIP stack.
  64. **Each node is required to initialize LwIP by calling** :cpp:func:`tcpip_adapter_init`.
  65. In order to prevent non-root node access to LwIP, the application should stop the
  66. following services after LwIP initialization:
  67. - DHCP server service on the softAP interface.
  68. - DHCP client service on the station interface.
  69. The following code snippet demonstrates how to initialize LwIP for ESP-MESH applications.
  70. .. code-block:: c
  71. /* tcpip initialization */
  72. tcpip_adapter_init();
  73. /*
  74. * for mesh
  75. * stop DHCP server on softAP interface by default
  76. * stop DHCP client on station interface by default
  77. */
  78. ESP_ERROR_CHECK(tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP));
  79. ESP_ERROR_CHECK(tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA));
  80. /* do not specify system event callback, use NULL instead. */
  81. ESP_ERROR_CHECK(esp_event_loop_init(NULL, NULL));
  82. .. note::
  83. ESP-MESH requires a root node to be connected with a router. Therefore, in
  84. the event that a node becomes the root, **the corresponding handler must start
  85. the DHCP client service and immediately obtain an IP address**. Doing so will
  86. allow other nodes to begin transmitting/receiving packets to/from the external
  87. IP network. However, this step is unnecessary if static IP settings are used.
  88. .. ---------------------- Writing a Mesh Application --------------------------
  89. .. _mesh-writing-mesh-application:
  90. Writing an ESP-MESH Application
  91. -------------------------------
  92. The prerequisites for starting ESP-MESH is to initialize LwIP and Wi-Fi, The
  93. following code snippet demonstrates the necessary prerequisite steps before
  94. ESP-MESH itself can be initialized.
  95. .. code-block:: c
  96. tcpip_adapter_init();
  97. /*
  98. * for mesh
  99. * stop DHCP server on softAP interface by default
  100. * stop DHCP client on station interface by default
  101. */
  102. ESP_ERROR_CHECK(tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP));
  103. ESP_ERROR_CHECK(tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA));
  104. /* do not specify system event callback, use NULL instead. */
  105. ESP_ERROR_CHECK(esp_event_loop_init(NULL, NULL));
  106. /* Wi-Fi initialization */
  107. wifi_init_config_t config = WIFI_INIT_CONFIG_DEFAULT();
  108. ESP_ERROR_CHECK(esp_wifi_init(&config));
  109. ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_FLASH));
  110. ESP_ERROR_CHECK(esp_wifi_start());
  111. After initializing LwIP and Wi-Fi, the process of getting an ESP-MESH network
  112. up and running can be summarized into the following three steps:
  113. 1. :ref:`mesh-initialize-mesh`
  114. 2. :ref:`mesh-configuring-mesh`
  115. 3. :ref:`mesh-start-mesh`
  116. .. _mesh-initialize-mesh:
  117. Initialize Mesh
  118. ^^^^^^^^^^^^^^^
  119. The following code snippet demonstrates how to initialize ESP-MESH
  120. .. code-block:: c
  121. /* mesh initialization */
  122. ESP_ERROR_CHECK(esp_mesh_init());
  123. .. _mesh-configuring-mesh:
  124. Configuring an ESP-MESH Network
  125. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  126. .. todo - Add note about unified configuration
  127. ESP-MESH is configured via :cpp:func:`esp_mesh_set_config` which receives its arguments
  128. using the :cpp:type:`mesh_cfg_t` structure. The structure contains the following
  129. parameters used to configure ESP-MESH:
  130. +------------------+-------------------------------------+
  131. | Parameter | Description |
  132. +==================+=====================================+
  133. | Channel | Range from 1 to 14 |
  134. +------------------+-------------------------------------+
  135. | Event Callback | Callback for Mesh Events, |
  136. | | see :cpp:type:`mesh_event_cb_t` |
  137. +------------------+-------------------------------------+
  138. | Mesh ID | ID of ESP-MESH Network, |
  139. | | see :cpp:type:`mesh_addr_t` |
  140. +------------------+-------------------------------------+
  141. | Router | Router Configuration, |
  142. | | see :cpp:type:`mesh_router_t` |
  143. +------------------+-------------------------------------+
  144. | Mesh AP | Mesh AP Configuration, |
  145. | | see :cpp:type:`mesh_ap_cfg_t` |
  146. +------------------+-------------------------------------+
  147. | Crypto Functions | Crypto Functions for Mesh IE, |
  148. | | see :cpp:type:`mesh_crypto_funcs_t` |
  149. +------------------+-------------------------------------+
  150. The following code snippet demonstrates how to configure ESP-MESH.
  151. .. code-block:: c
  152. /* Enable the Mesh IE encryption by default */
  153. mesh_cfg_t cfg = MESH_INIT_CONFIG_DEFAULT();
  154. /* mesh ID */
  155. memcpy((uint8_t *) &cfg.mesh_id, MESH_ID, 6);
  156. /* mesh event callback */
  157. cfg.event_cb = &mesh_event_handler;
  158. /* channel (must match the router's channel) */
  159. cfg.channel = CONFIG_MESH_CHANNEL;
  160. /* router */
  161. cfg.router.ssid_len = strlen(CONFIG_MESH_ROUTER_SSID);
  162. memcpy((uint8_t *) &cfg.router.ssid, CONFIG_MESH_ROUTER_SSID, cfg.router.ssid_len);
  163. memcpy((uint8_t *) &cfg.router.password, CONFIG_MESH_ROUTER_PASSWD,
  164. strlen(CONFIG_MESH_ROUTER_PASSWD));
  165. /* mesh softAP */
  166. cfg.mesh_ap.max_connection = CONFIG_MESH_AP_CONNECTIONS;
  167. memcpy((uint8_t *) &cfg.mesh_ap.password, CONFIG_MESH_AP_PASSWD,
  168. strlen(CONFIG_MESH_AP_PASSWD));
  169. ESP_ERROR_CHECK(esp_mesh_set_config(&cfg));
  170. .. _mesh-start-mesh:
  171. Start Mesh
  172. ^^^^^^^^^^
  173. The following code snippet demonstrates how to start ESP-MESH.
  174. .. code-block:: c
  175. /* mesh start */
  176. ESP_ERROR_CHECK(esp_mesh_start());
  177. After starting ESP-MESH, the application should check for ESP-MESH events to determine
  178. when it has connected to the network. After connecting, the application can start
  179. transmitting and receiving packets over the ESP-MESH network using
  180. :cpp:func:`esp_mesh_send` and :cpp:func:`esp_mesh_recv`.
  181. .. --------------------- ESP-MESH Application Examples ------------------------
  182. .. _mesh-application-examples:
  183. Application Examples
  184. --------------------
  185. ESP-IDF contains these ESP-MESH example projects:
  186. :example:`The Internal Communication Example<mesh/internal_communication>` demonstrates
  187. how to setup a ESP-MESH network and have the root node send a data packet to
  188. every node within the network.
  189. :example:`The Manual Networking Example<mesh/manual_networking>` demonstrates
  190. how to use ESP-MESH without the self-organizing features. This example shows how
  191. to program a node to manually scan for a list of potential parent nodes and select
  192. a parent node based on custom criteria.
  193. .. ------------------------- ESP-MESH API Reference ---------------------------
  194. .. _mesh-api-reference:
  195. API Reference
  196. --------------
  197. .. include:: /_build/inc/esp_mesh.inc