Chapter 2. API Reference
const esp_mqtt_client_config_t mqtt_cfg = {
.uri = "mqtt://mqtt.eclipseprojects.io",
// .user_context = (void *)your_context
};
esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);
esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler,␣
,→client);
esp_mqtt_client_start(client);
• Note: By default mqtt client uses event loop library to post related mqtt events (connected, subscribed, pub-
lished, etc.)
SSL
• Get certificate from server, example: mqtt.eclipseprojects.io openssl s_client -
showcerts -connect mqtt.eclipseprojects.io:8883 </dev/null 2>/dev/
null|openssl x509 -outform PEM >mqtt_eclipse_org.pem
• Check the sample application: examples/mqtt_ssl
• Configuration:
const esp_mqtt_client_config_t mqtt_cfg = {
.uri = "mqtts://mqtt.eclipseprojects.io:8883",
.event_handle = mqtt_event_handler,
.cert_pem = (const char *)mqtt_eclipse_org_pem_start,
};
If the certificate is not null-terminated then cert_len should also be set. Other SSL related configuration param-
eters are:
• use_global_ca_store: use the global certificate store to verify server certificate, see esp-tls.h for
more information
• 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.
• client_cert_len: length of the buffer pointed to by client_cert_pem. May be 0 for null-terminated pem.
• 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.
• client_key_len: length of the buffer pointed to by client_key_pem. May be 0 for null-terminated pem.
• 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
• alpn_protos: NULL-terminated list of protocols to be used for ALPN.
Last Will and Testament 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 in the esp_mqtt_client_config_t-
struct.
• lwt_topic: pointer to the LWT message topic
• lwt_msg: pointer to the LWT message
• lwt_msg_len: length of the LWT message, required if lwt_msg is not null-terminated
• lwt_qos: quality of service for the LWT message
• lwt_retain: specifies the retain flag of the LWT message
Other Configuration Parameters
• disable_clean_session: determines the clean session flag for the connect message, defaults to a clean
session
• keepalive: determines how many seconds the client will wait for a ping response before disconnecting,
default is 120 seconds.
• disable_auto_reconnect: enable to stop the client from reconnecting to server after errors or discon-
nects
Espressif Systems 509
Submit Document Feedback
Release v4.4