Page 1 of 1

How to reconnect MQTT with new config

Posted: Fri Feb 15, 2019 11:58 am
by adam_ohara
Hi

I am trying to change the config of my MQTT client and reconnect to the broker, as the username and password can be changed. This is over a TLS connection. Currently, I am trying:

Code: Select all

        mqtt_config.username = mqtt_username;
        mqtt_config.password = mqtt_password;
        ESP_LOGI(TAG, "Password set to %s", mqtt_password);

        //Swap config and restart client
        if(started){
            esp_mqtt_client_stop(mqtt_client);
            esp_mqtt_set_config(mqtt_client, &mqtt_config);
            esp_mqtt_client_start(mqtt_client);
        }
However, I get the following:

E (1666107) MQTT_CLIENT: Client has not connected
E (1668167) esp-tls: mbedtls_ssl_setup returned -0x7f00


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

Does anyone have any suggestions?
The MQTT client that was originally set wasn't connected to the broker in the first place, but it looks like it keeps trying until I do the above. Do I need to create a completely new config for doing this, or is config re-use OK?

Any help will be greatly appreciated.

Re: How to reconnect MQTT with new config

Posted: Sat Feb 16, 2019 9:24 am
by ESP_cermak
Hi,

esp-mqtt library has recently been updated to support runtime update of configurations (specifically commit https://github.com/espressif/esp-mqtt/c ... b3eaa5ba15)

Updating the broker uri could be done:

Code: Select all

        esp_mqtt_client_stop(mqtt_client);
        esp_mqtt_client_set_uri(mqtt_client, CONFIG_NEW_BROKER_URI);
        esp_mqtt_client_start(mqtt_client);
Current master of esp-idf does not yet reference this updated mqtt, but that should be fixed soon (+ example added)