mqtt.rst 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. ESP-MQTT
  2. ========
  3. Overview
  4. --------
  5. ESP-MQTT is an implementation of MQTT protocol client (MQTT is a lightweight publish/subscribe messaging protocol).
  6. Features
  7. --------
  8. * Supports MQTT over TCP, SSL with mbedtls, MQTT over Websocket, MQTT over Websocket Secure.
  9. * Easy to setup with URI
  10. * Multiple instances (Multiple clients in one application)
  11. * Support subscribing, publishing, authentication, last will messages, keep alive pings and all 3 QoS levels (it should be a fully functional client).
  12. Application Example
  13. -------------------
  14. * :example:`protocols/mqtt/tcp`: MQTT over tcp, default port 1883
  15. * :example:`protocols/mqtt/ssl`: MQTT over tcp, default port 8883
  16. * :example:`protocols/mqtt/ssl_psk`: MQTT over tcp using pre-shared keys for authentication, default port 8883
  17. * :example:`protocols/mqtt/ws`: MQTT over Websocket, default port 80
  18. * :example:`protocols/mqtt/wss`: MQTT over Websocket Secure, default port 443
  19. Configuration
  20. -------------
  21. URI
  22. ^^^
  23. - Curently support ``mqtt``, ``mqtts``, ``ws``, ``wss`` schemes
  24. - MQTT over TCP samples:
  25. - ``mqtt://mqtt.eclipseprojects.io``: MQTT over TCP, default port 1883:
  26. - ``mqtt://mqtt.eclipseprojects.io:1884`` MQTT over TCP, port 1884:
  27. - ``mqtt://username:password@mqtt.eclipseprojects.io:1884`` MQTT over TCP,
  28. port 1884, with username and password
  29. - MQTT over SSL samples:
  30. - ``mqtts://mqtt.eclipseprojects.io``: MQTT over SSL, port 8883
  31. - ``mqtts://mqtt.eclipseprojects.io:8884``: MQTT over SSL, port 8884
  32. - MQTT over Websocket samples:
  33. - ``ws://mqtt.eclipseprojects.io:80/mqtt``
  34. - MQTT over Websocket Secure samples:
  35. - ``wss://mqtt.eclipseprojects.io:443/mqtt``
  36. - Minimal configurations:
  37. .. code:: c
  38. const esp_mqtt_client_config_t mqtt_cfg = {
  39. .uri = "mqtt://mqtt.eclipseprojects.io",
  40. // .user_context = (void *)your_context
  41. };
  42. esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);
  43. esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client);
  44. esp_mqtt_client_start(client);
  45. - Note: By default mqtt client uses event loop library to post related mqtt events (connected, subscribed, published, etc.)
  46. SSL
  47. ^^^
  48. - Get certificate from server, example: ``mqtt.eclipseprojects.io``
  49. ``openssl s_client -showcerts -connect mqtt.eclipseprojects.io:8883 </dev/null 2>/dev/null|openssl x509 -outform PEM >mqtt_eclipse_org.pem``
  50. - Check the sample application: ``examples/mqtt_ssl``
  51. - Configuration:
  52. .. code:: c
  53. const esp_mqtt_client_config_t mqtt_cfg = {
  54. .uri = "mqtts://mqtt.eclipseprojects.io:8883",
  55. .event_handle = mqtt_event_handler,
  56. .cert_pem = (const char *)mqtt_eclipse_org_pem_start,
  57. };
  58. If the certificate is not null-terminated then ``cert_len`` should also be set.
  59. Other SSL related configuration parameters are:
  60. * ``use_global_ca_store``: use the global certificate store to verify server certificate, see ``esp-tls.h`` for more information
  61. * ``client_cert_pem``: pointer to certificate data in PEM or DER format for SSL mutual authentication, default is NULL, not required if mutual authentication is not needed.
  62. * ``client_cert_len``: length of the buffer pointed to by client_cert_pem. May be 0 for null-terminated pem.
  63. * ``client_key_pem``: pointer to private key data in PEM or DER format for SSL mutual authentication, default is NULL, not required if mutual authentication is not needed.
  64. * ``client_key_len``: length of the buffer pointed to by client_key_pem. May be 0 for null-terminated pem.
  65. * ``psk_hint_key``: pointer to PSK struct defined in esp_tls.h to enable PSK authentication (as alternative to certificate verification). If not NULL and server/client certificates are NULL, PSK is enabled
  66. * ``alpn_protos``: NULL-terminated list of protocols to be used for ALPN.
  67. Last Will and Testament
  68. ^^^^^^^^^^^^^^^^^^^^^^^
  69. MQTT allows for a last will and testament (LWT) message to notify other clients when a client ungracefully disconnects. This is configured by the following fields
  70. in the ``esp_mqtt_client_config_t``-struct.
  71. * ``lwt_topic``: pointer to the LWT message topic
  72. * ``lwt_msg``: pointer to the LWT message
  73. * ``lwt_msg_len``: length of the LWT message, required if ``lwt_msg`` is not null-terminated
  74. * ``lwt_qos``: quality of service for the LWT message
  75. * ``lwt_retain``: specifies the retain flag of the LWT message
  76. Other Configuration Parameters
  77. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  78. * ``disable_clean_session``: determines the clean session flag for the connect message, defaults to a clean session
  79. * ``keepalive``: determines how many seconds the client will wait for a ping response before disconnecting, default is 120 seconds.
  80. * ``disable_auto_reconnect``: enable to stop the client from reconnecting to server after errors or disconnects
  81. * ``user_context``: custom context that will be passed to the event handler
  82. * ``task_prio``: MQTT task priority, defaults to 5
  83. * ``task_stack``: MQTT task stack size, defaults to 6144 bytes, setting this will override setting from menuconfig
  84. * ``buffer_size``: size of MQTT send/receive buffer, default is 1024 bytes
  85. * ``username``: pointer to the username used for connecting to the broker
  86. * ``password``: pointer to the password used for connecting to the broker
  87. * ``client_id``: pointer to the client id, defaults to ``ESP32_%CHIPID%`` where %CHIPID% are the last 3 bytes of MAC address in hex format
  88. * ``host``: MQTT broker domain (ipv4 as string), setting the uri will override this
  89. * ``port``: MQTT broker port, specifying the port in the uri will override this
  90. * ``transport``: sets the transport protocol, setting the uri will override this
  91. * ``refresh_connection_after_ms``: refresh connection after this value (in milliseconds)
  92. * ``event_handle``: handle for MQTT events as a callback in legacy mode
  93. * ``event_loop_handle``: handle for MQTT event loop library
  94. For more options on ``esp_mqtt_client_config_t``, please refer to API reference below
  95. Change settings in Project Configuration Menu
  96. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  97. The settings for MQTT can be found using ``idf.py menuconfig``, under Component config -> ESP-MQTT Configuration
  98. The following settings are available:
  99. - :ref:`CONFIG_MQTT_PROTOCOL_311`: Enables 3.1.1 version of MQTT protocol
  100. - :ref:`CONFIG_MQTT_TRANSPORT_SSL`, :ref:`CONFIG_MQTT_TRANSPORT_WEBSOCKET`: Enables specific MQTT transport layer, such as SSL, WEBSOCKET, WEBSOCKET_SECURE
  101. - :ref:`CONFIG_MQTT_CUSTOM_OUTBOX`: Disables default implementation of mqtt_outbox, so a specific implementaion can be supplied
  102. Events
  103. ------
  104. The following events may be posted by the MQTT client:
  105. * ``MQTT_EVENT_BEFORE_CONNECT``: The client is initialized and about to start connecting to the broker.
  106. * ``MQTT_EVENT_CONNECTED``: The client has successfully established a connection to the broker. The client is now ready to send and receive data.
  107. * ``MQTT_EVENT_DISCONNECTED``: The client has aborted the connection due to being unable to read or write data, e.g. because the server is unavailable.
  108. * ``MQTT_EVENT_SUBSCRIBED``: The broker has acknowledged the client's subscribe request. The event data will contain the message ID of the subscribe message.
  109. * ``MQTT_EVENT_UNSUBSCRIBED``: The broker has acknowledged the client's unsubscribe request. The event data will contain the message ID of the unsubscribe message.
  110. * ``MQTT_EVENT_PUBLISHED``: The broker has acknowledged the client's publish message. This will only be posted for Quality of Service level 1 and 2, as level 0 does not use acknowledgements. The event data will contain the message ID of the publish message.
  111. * ``MQTT_EVENT_DATA``: The client has received a publish message. The event data contains: message ID, name of the topic it was published to, received data and its length. For data that exceeds the internal buffer multiple `MQTT_EVENT_DATA` will be posted and `current_data_offset` and `total_data_len` from event data updated to keep track of the fragmented message.
  112. * ``MQTT_EVENT_ERROR``: The client has encountered an error. `esp_mqtt_error_type_t` from `error_handle` in the event data can be used to further determine the type of the error. The type of error will determine which parts of the `error_handle` struct is filled.
  113. API Reference
  114. -------------
  115. .. include-build-file:: inc/mqtt_client.inc