Chapter 2. API Reference
• Client must be connected to send unsubscribe message
• It is thread safe, please refer to esp_mqtt_client_subscribe for details
Return message_id of the subscribe message on success -1 on failure
Parameters
• client: mqtt client handle
• topic:
int esp_mqtt_client_publish(esp_mqtt_client_handle_t client, const char *topic, const char
*data, int len, int qos, int retain)
Client to send a publish message to the broker.
Notes:
• This API might block for several seconds, either due to network timeout (10s) or if publishing payloads
longer than internal buffer (due to message fragmentation)
• Client doesn’t have to be connected for this API to work, enqueueing the messages with qos>1 (returning
-1 for all the qos=0 messages if disconnected). If MQTT_SKIP_PUBLISH_IF_DISCONNECTED is
enabled, this API will not attempt to publish when the client is not connected and will always return -1.
• It is thread safe, please refer to esp_mqtt_client_subscribe for details
Return message_id of the publish message (for QoS 0 message_id will always be zero) on success. -1 on
failure.
Parameters
• client: mqtt client handle
• topic: topic string
• data: payload string (set to NULL, sending empty payload message)
• len: data length, if set to 0, length is calculated from payload string
• qos: qos of publish message
• retain: retain flag
int esp_mqtt_client_enqueue(esp_mqtt_client_handle_t client, const char *topic, const char
*data, int len, int qos, int retain, bool store)
Enqueue a message to the outbox, to be sent later. Typically used for messages with qos>0, but could be also
used for qos=0 messages if store=true.
This API generates and stores the publish message into the internal outbox and the actual sending to the net-
work is performed in the mqtt-task context (in contrast to the esp_mqtt_client_publish() which sends the pub-
lish message immediately in the user task’s context). Thus, it could be used as a non blocking version of
esp_mqtt_client_publish().
Return message_id if queued successfully, -1 otherwise
Parameters
• client: mqtt client handle
• topic: topic string
• data: payload string (set to NULL, sending empty payload message)
• len: data length, if set to 0, length is calculated from payload string
• qos: qos of publish message
• retain: retain flag
• store: if true, all messages are enqueued; otherwise only qos1 and qos 2 are enqueued
esp_err_t esp_mqtt_client_destroy(esp_mqtt_client_handle_t client)
Destroys the client handle.
Notes:
• Cannot be called from the mqtt event handler
Return ESP_OK ESP_ERR_INVALID_ARG on wrong initialization
Parameters
• client: mqtt client handle
esp_err_t esp_mqtt_set_config(esp_mqtt_client_handle_t client, const esp_mqtt_client_config_t
*config)
Set configuration structure, typically used when updating the config (i.e. on “before_connect”event.
Espressif Systems 512
Submit Document Feedback
Release v4.4