0
votes

I am trying to connect to 2 MQTT brokers at the same time using ESP-IDF. One over TLS port 8883 and the other is not secure on port 1883.

I have declared 2 client instances, but somehow i cannot get them connect to the brokers.

If I comment out one or the other, it works just fine.

Source Code:

// Device manager configuration
    esp_mqtt_client_config_t mqtt_device_manager_cfg = {
        .uri = MQTT_DEVICE_MANAGER_URI,
        .port = MQTT_DEVICE_MANAGER_PORT,
        .username = (const char*) device_UUID,
        .password = MQTT_DEVICE_MANAGER_PASSWORD,
        .client_id = (const char*) device_UUID,
        .disable_clean_session = 1,
        .cert_pem = client_cert_pem,
        .transport = MQTT_TRANSPORT_OVER_SSL
    }; 
    device_manager_mqtt_client = esp_mqtt_client_init(&mqtt_device_manager_cfg);
    esp_mqtt_client_register_event(device_manager_mqtt_client, ESP_EVENT_ANY_ID, mqtt_device_manager_event_handler, NULL);
    esp_mqtt_client_start(device_manager_mqtt_client);
    
// MQTT configuration
    esp_mqtt_client_config_t mqtt_cfg = {
        .uri = MQTT_URI,
        .port = MQTT_PORT,
        .username = MQTT_USERNAME,
        .password = MQTT_PASSWORD,
        .transport = MQTT_TRANSPORT_OVER_TCP
    };
    mqtt_client = esp_mqtt_client_init(&mqtt_cfg);
    esp_mqtt_client_register_event(mqtt_client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL);
    esp_mqtt_client_start(mqtt_client);

Errors:

E (5078) esp-tls: mbedtls_ssl_handshake returned -0x2700
I (5078) wifi:I (5078) esp-tls: Failed to verify peer certificate!
int: state=0 i=0
I (5078) esp-tls: verification info:   ! The certificate is not correctly signed by the trusted CA

E (5088) esp-tls: Failed to open new connection
E (5098) TRANS_SSL: Failed to open a new connection
E (5098) MQTT_CLIENT: Error transport connect

Somehow it seems that the non secure client, thinks it is secure and is trying to use the certificate, which it should not.

What am I doing wrong?

1
if I add a delay of 5 seconds between them I get the following error instead: E (17918) TRANS_SSL: ssl_poll_read select error 113, errno = Software caused connection abort, fd = 54 E (17928) MQTT_CLIENT: Poll read error: 0, aborting connection E (17928) TRANS_TCP: tcp_poll_read select error 113, errno = Software caused connection abort, fd = 55 E (17948) MQTT_CLIENT: Poll read error: 0, aborting connection E (32948) MQTT_CLIENT: Error transport connect - Simon

1 Answers

0
votes

Somehow it works, if I remove the .cert_pem value.