Using esp_mqtt_cxx with SSL
Posted: Mon Dec 23, 2024 1:34 pm
I want to connect with my ESP32 and a Quectel modem to an MQTT broker using TLS (port 8883). I can't get a working combination of code.
What I have (based on the SSL example):
But it can't connect, it silently fails. I also don't know the correct command to generate the .pem file I need. Does anybody now this?
What I have (based on the SSL example):
Code: mqtt.cpp Select all
// Configure the MQTT client
esp_transport_handle_t at =
esp_transport_at_init(Modem::getInstance().getDCE());
if (!at) {
ESP_LOGE(TAG, "Failed to initialize the AT transport!");
return false;
}
// Configure the TLS transport
esp_transport_handle_t ssl = esp_transport_tls_init(at);
if (!ssl) {
ESP_LOGE(TAG, "Failed to initialize the SSL transport!");
return false;
}
idf::mqtt::BrokerConfiguration broker{
.address = {idf::mqtt::URI{"mqtts://" + std::string{MQTT_BROKER_URL} +
":" + std::to_string(MQTT_PORT)}},
.security = idf::mqtt::CryptographicInformation{
idf::mqtt::PEM{.data = mqtt_pem_start}}};
idf::mqtt::ClientCredentials credentials{
.username = MQTT_SUID,
.authentication = idf::mqtt::Password{.data = MQTT_PASSWORD},
.client_id = MQTT_SUID};
idf::mqtt::Network network{.transport = ssl};
idf::mqtt::Configuration config{.network = network};
// Store the client as a member variable
mqtt_client = std::make_unique<MQTTClient>(broker, credentials, config);