mqtt.rst 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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, 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/ws`: MQTT over Websocket, default port 80
  17. * :example:`protocols/mqtt/wss`: MQTT over Websocket Secure, default port 443
  18. Configuration
  19. -------------
  20. URI
  21. ^^^
  22. - Curently support ``mqtt``, ``mqtts``, ``ws``, ``wss`` schemes
  23. - MQTT over TCP samples:
  24. - ``mqtt://mqtt.eclipse.org``: MQTT over TCP, default port 1883:
  25. - ``mqtt://mqtt.eclipse.org:1884`` MQTT over TCP, port 1884:
  26. - ``mqtt://username:password@mqtt.eclipse.org:1884`` MQTT over TCP,
  27. port 1884, with username and password
  28. - MQTT over SSL samples:
  29. - ``mqtts://mqtt.eclipse.org``: MQTT over SSL, port 8883
  30. - ``mqtts://mqtt.eclipse.org:8884``: MQTT over SSL, port 8884
  31. - MQTT over Websocket samples:
  32. - ``ws://mqtt.eclipse.org:80/mqtt``
  33. - MQTT over Websocket Secure samples:
  34. - ``wss://mqtt.eclipse.org:443/mqtt``
  35. - Minimal configurations:
  36. .. code:: c
  37. const esp_mqtt_client_config_t mqtt_cfg = {
  38. .uri = "mqtt://mqtt.eclipse.org",
  39. // .user_context = (void *)your_context
  40. };
  41. esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);
  42. esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client);
  43. esp_mqtt_client_start(client);
  44. - Note: By default mqtt client uses event loop library to post related mqtt events (connected, subsribed, published, etc.)
  45. - If there are any options related to the URI in
  46. ``esp_mqtt_client_config_t``, the option defined by the URI will be
  47. overridden. Sample:
  48. .. code:: c
  49. const esp_mqtt_client_config_t mqtt_cfg = {
  50. .uri = "mqtt://mqtt.eclipse.org:1234",
  51. .port = 4567,
  52. };
  53. //MQTT client will connect to mqtt.eclipse.org using port 4567
  54. SSL
  55. ^^^
  56. - Get certificate from server, example: ``mqtt.eclipse.org``
  57. ``openssl s_client -showcerts -connect mqtt.eclipse.org:8883 </dev/null 2>/dev/null|openssl x509 -outform PEM >mqtt_eclipse_org.pem``
  58. - Check the sample application: ``examples/mqtt_ssl``
  59. - Configuration:
  60. .. code:: cpp
  61. const esp_mqtt_client_config_t mqtt_cfg = {
  62. .uri = "mqtts://mqtt.eclipse.org:8883",
  63. .event_handle = mqtt_event_handler,
  64. .cert_pem = (const char *)mqtt_eclipse_org_pem_start,
  65. };
  66. For more options on ``esp_mqtt_client_config_t``, please refer to API reference below
  67. Change settings in Project Configuration Menu
  68. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  69. ::
  70. idf.py menuconfig
  71. -> Component config -> ESP-MQTT Configuration
  72. - :ref:`CONFIG_MQTT_PROTOCOL_311`: Enables 3.1.1 version of MQTT protocol
  73. - :ref:`CONFIG_MQTT_TRANSPORT_SSL`, :ref:`CONFIG_MQTT_TRANSPORT_WEBSOCKET`: Enables specific MQTT transport layer, such as SSL, WEBSOCKET, WEBSOCKET_SECURE
  74. - :ref:`CONFIG_MQTT_CUSTOM_OUTBOX`: Disables default implementation of mqtt_outbox, so a specific implementaion can be supplied
  75. API Reference
  76. -------------
  77. .. include:: /_build/inc/mqtt_client.inc