Qt World Summit: Submit your Presentation

Trouble connecting to aws iot using QMqtt and QSsl stack.

  • Brief Description: I am using QMqtt and QSsl stack to connect to perform a secure Mqtt connection and then publish some data on the aws server. But the Client state always stays on connecting never changes to connected

    Expected behavior: First I tried connecting to test.mosquitto.org, I was able to connect and publish the data. So the expected behavior was to see some similar results.

    Send_MqTT::Send_MqTT(QObject *parent) : QObject(parent)
        m_socket=new QSslSocket();
        m_client=new QMqttClient();
        m_topicname = new QMqttTopicName();
        connect(m_client,&QMqttClient::stateChanged,[](QMqttClient::ClientState state){
            if(state == QMqttClient::Disconnected)
                qDebug()<<"State is Disconnected: "<<state;
            else if(state == QMqttClient::Connecting)
                qDebug()<<"State is Connecting: "<<state;
            else if(state == QMqttClient::Connected)
                qDebug()<<"State is Connected: "<<state;
        connect(m_client,&QMqttClient::errorChanged,[](QMqttClient::ClientError error_state){
            qDebug()<<"Error State is: "<<error_state;
    void Send_MqTT::connect_toserver()
            qDebug()<<"SSL SUPORTED";
            m_socket->setPrivateKey("<private Key Location>",QSsl::Rsa,QSsl::Pem);

    Am I missing something. Do I have to set Mqtt connection properties before I begin the connection?

  • Lifetime Qt Champion


    Can you explain what result you are getting ?
    What errors are you getting ?
    What version of Qt are you using ?

  • @sgaist The clientState stays on connecting and never goes to connected. Because of which I am unable to publish any data. The ClientState has to transition to connected for me to publish or subscribe

    enum QMqttClient::ClientState
    Constant	                             Value	
    QMqttClient::Disconnected	              0	
    QMqttClient::Connecting	                      1	      
    QMqttClient::Connected	                      2

    So the state always stays on connecting. I do not get any errors.
    Qt version is 5.12.3
    and system is an Ubuntu 18.04

  • Lifetime Qt Champion

    From the looks of it, you didn't connect any of the QOpenSSLSocket error signals. You should add them.

  • @SGaist okay I will add that. But Don't you think if there was any error. Mqtt should also throw an error. Because if I remove a certificate or a private key from the file or add any wrong information, I do get an error stating that the transport properties are not set properly and the ClientState goes to disconnected it does not stays on connecting for a long period of time.

Log in to reply