connect to multiple mqtt brokers TLS/non TLS

mt-simon
Posts: 1
Joined: Mon Aug 17, 2020 8:13 am

connect to multiple mqtt brokers TLS/non TLS

Postby mt-simon » Mon Aug 17, 2020 8:18 am

Hi,


I am having some troubles understanding, why I cannot connect to two independent MQTT brokers at the same time.
One broker is configured with TLS, and the other is not.

Code: Select all

    // 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
    }; 
    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,
    };
    
    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);
If i comment out one or the other, it works fine, else I get this:

Code: Select all

E (14272) TRANS_TCP: tcp_poll_read select error 113, errno = Software caused connection abort, fd = 55
E (14282) MQTT_CLIENT: mqtt_message_receive: transport_read() error: errno=0
E (14282) TRANS_SSL: ssl_poll_read select error 113, errno = Software caused connection abort, fd = 54
E (14292) MQTT_CLIENT: esp_mqtt_connect: mqtt_message_receive() returned -1
E (14302) MQTT_CLIENT: Poll read error: 0, aborting connection
Any ideas?

vibro_tech
Posts: 7
Joined: Wed May 27, 2020 2:07 pm

Re: connect to multiple mqtt brokers TLS/non TLS

Postby vibro_tech » Fri Nov 27, 2020 12:22 pm

Hi,
Did you solve this? I am having nearly the same issue. Trying to connect 2 mqtt broker. Actually code is below and very simple :(

Code: Select all

esp_mqtt_client_config_t mqtt_cfg = {
		.uri = mqttBrokerUrl,
		.port = 1883,
		.username = mqttUsername,
		.password = mqttPassword,
	};
    clientData = esp_mqtt_client_init(&mqtt_cfg);
	esp_mqtt_client_register_event(clientData, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL);
	esp_mqtt_client_start(clientData);
	vTaskDelay(10000/portTICK_RATE_MS);

	esp_mqtt_client_config_t mqtt_cfg_admin = {
		.uri = adminBrokerUrl,
		.port = 1883,
		.username = adminMqttUser,
		.password = adminMqttPass,
		.client_id = (const char*) "WEDSDV",
	};
	clientAdmin = esp_mqtt_client_init(&mqtt_cfg_admin);
	esp_mqtt_client_register_event(clientAdmin, ESP_EVENT_ANY_ID, mqtt_event_handler_admin, NULL);
	esp_mqtt_client_start(clientAdmin);

Thanks in advance

ESP_cermak
Posts: 69
Joined: Thu Nov 01, 2018 8:32 am

Re: connect to multiple mqtt brokers TLS/non TLS

Postby ESP_cermak » Tue Dec 01, 2020 9:00 am

This should work okay without any trouble. Just checked on the latest master. Could you please share the IDF version?

The only trouble I think off, could be if these "multiple mqtt brokers TLS/non TLS" is actually one broker with multiple interfaces/protocols (common on mosqutto's). In that case a client has to connect with a different client id (this is by default generated from the MAC, so would be the same on both instances). This code works for me in this case:

Code: Select all

static void mqtt_app_start(void)
{
    const esp_mqtt_client_config_t mqtt_cfg = {
        .uri = "mqtts://mqtt.eclipse.org:8883",
        .client_id = "first_client",
        .cert_pem = (const char *)mqtt_eclipse_org_pem_start,
    };

    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);

    const esp_mqtt_client_config_t mqtt_cfg2 = {
        .uri = "mqtt://mqtt.eclipse.org",
        .client_id = "another_client",
    };

    client = esp_mqtt_client_init(&mqtt_cfg2);
    esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client);
    esp_mqtt_client_start(client);
}

Who is online

Users browsing this forum: No registered users and 104 guests