esp_now.rst 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. ESP-NOW
  2. =======
  3. Overview
  4. --------
  5. ESP-NOW is a kind of connectionless WiFi communication protocol which is defined by Espressif. In ESP-NOW, application data is
  6. encapsulated in vendor-specific action frame and then transmitted from one WiFi device to another without connection.
  7. CTR with CBC-MAC Protocol(CCMP) is used to protect the action frame for security. ESP-NOW is widely used in smart light, remote
  8. controlling, sensor, etc.
  9. Frame Format
  10. ------------
  11. ESP-NOW uses vendor-specific action frame to transmit ESP-NOW data. The format of vendor-specific action frame is as follows:
  12. .. highlight:: none
  13. ::
  14. ----------------------------------------------------------------------------------------
  15. | MAC Header | Category Code | Organization Identifier | Vendor Specific Content | FCS |
  16. ----------------------------------------------------------------------------------------
  17. 1 byte 3 bytes 7~255 bytes
  18. - Category Code: The Category field is set to the value(127) indicating the vendor-specific category.
  19. - Organization Identifier: The Organization Identifier contains a unique identifier(0x18fe34) which is the first three bytes
  20. of MAC address applied by Espressif.
  21. - Vendor Specific Content: The Vendor Specific Content contains vendor-specific field as follows:
  22. .. highlight:: none
  23. ::
  24. -------------------------------------------------------------------------------
  25. | Element ID | Length | Organization Identifier | Type | Version | Body |
  26. -------------------------------------------------------------------------------
  27. 1 byte 1 byte 3 bytes 1 byte 1 byte 0~250 bytes
  28. - Element ID: The Element ID field is set to the value(221) indicating the vendor-specific element.
  29. - Length: The length is the total length of Organization Identifier, Type, Version and Body.
  30. - Organization Identifier: The Organization Identifier contains a unique identifier(0x18fe34) which is the first three bytes
  31. of MAC address applied by Espressif.
  32. - Type: The Type field is set to the value(4) indicating ESP-NOW.
  33. - Version: The Version field is set to the version of ESP-NOW.
  34. - Body: The Body contains the ESP-NOW data.
  35. As ESP-NOW is connectionless, the MAC header is a little different from that of standard frames. The FromDS and ToDS bits of
  36. FrameControl field are both 0. The first address field is set to the destination address. The second address field is set to
  37. the source address. The third address field is set to broadcast address(0xff:0xff:0xff:0xff:0xff:0xff).
  38. Security
  39. --------
  40. ESP-NOW use CCMP method which can be referenced in IEEE Std. 802.11-2012 to protect the vendor-specific action frame. The WiFi
  41. device maintains a Primary Master Key(PMK) and several Local Master Keys(LMK). The lengths of them are 16 bytes. PMK is used
  42. to encrypt LMK with AES-128 algorithm. Call ``esp_now_set_pmk()`` to set PMK. If PMK is not set, a default PMK will be used.
  43. If LMK of the paired device is set, it will be used to encrypt the vendor-specific action frame with CCMP method. The maximum
  44. number of different LMKs is six. Do not support encrypting multicast vendor-specific action frame.
  45. Initialization and De-initialization
  46. ------------------------------------
  47. Call ``esp_now_init()`` to initialize ESP-NOW and ``esp_now_deinit()`` to de-initialize ESP-NOW. ESP-NOW data must be transmitted
  48. after WiFi is started, so it is recommended to start WiFi before initializing ESP-NOW and stop WiFi after de-initializing ESP-NOW.
  49. When ``esp_now_deinit()`` is called, all of the information of paired devices will be deleted.
  50. Add Paired Device
  51. -----------------
  52. Before sending data to other device, call ``esp_now_add_peer()`` to add it to the paired device list first. The maximum number of
  53. paired devices is twenty. If security is enabled, the LMK must be set. ESP-NOW data can be sent from station or softap interface.
  54. Make sure that the interface is enabled before sending ESP-NOW data. A device with broadcast MAC address must be added before
  55. sending broadcast data. The range of the channel of paired device is from 0 to 14. If the channel is set to 0, data will be sent
  56. on the current channel. Otherwise, the channel must be set as the channel that the local device is on.
  57. Send ESP-NOW Data
  58. -----------------
  59. Call ``esp_now_send()`` to send ESP-NOW data and ``esp_now_register_send_cb`` to register sending callback function. It will return
  60. `ESP_NOW_SEND_SUCCESS` in sending callback function if the data is received successfully on MAC layer. Otherwise, it will return
  61. `ESP_NOW_SEND_FAIL`. There are several reasons failing to send ESP-NOW data, for example, the destination device doesn't exist, the
  62. channels of the devices are not the same, the action frame is lost when transmiting on the air, etc. It is not guaranteed that
  63. application layer can receive the data. If necessary, send back ack data when receiving ESP-NOW data. If receiving ack data timeout
  64. happens, retransmit the ESP-NOW data. A sequence number can also be assigned to ESP-NOW data to drop the duplicated data.
  65. If there is a lot of ESP-NOW data to send, call ``esp_now_send()`` to send less than or equal to 250 bytes of data once a time.
  66. Note that too short interval between sending two ESP-NOW datas may lead to disorder of sending callback function. So, it is
  67. recommended that sending the next ESP-NOW data after the sending callback function of previous sending has returned. The sending
  68. callback function runs from a high-priority WiFi task. So, do not do lengthy operations in the callback function. Instead, post
  69. necessary data to a queue and handle it from a lower priority task.
  70. Receiving ESP-NOW Data
  71. ----------------------
  72. Call ``esp_now_register_recv_cb`` to register receiving callback function. When receiving ESP-NOW data, receiving callback function
  73. is called. The receiving callback function also runs from WiFi task. So, do not do lengthy operations in the callback function.
  74. Instead, post necessary data to a queue and handle it from a lower priority task.
  75. API Reference
  76. -------------
  77. .. include:: /_build/inc/esp_now.inc