Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problems connecting to a broker with QtMqtt
Forum Updated to NodeBB v4.3 + New Features

Problems connecting to a broker with QtMqtt

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 5 Posters 3.5k Views 5 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    glasen77
    wrote on last edited by
    #1

    Hi,

    i'm currently trying to implement a small command line Mqtt-client with QtMqtt. All examples (e.g. "simpleclient") connect absolutely flawlessly to my local mosquitto-server. I'm using Qt 5.10.1 with Ubuntu 18.04.

    I used the code from "simpleclient" as base for my client but own code does not connect to the server. It stays in stage 1 and subscriptions are not possible. Here is a snippet from my code:

    dummyplugin.cpp
    
    DummyPlugin::DummyPlugin() {
        qDebug() << "Initialising DummyPlugin ...";
    
        m_client = new QMqttClient(this);
    
        connect(m_client, &QMqttClient::stateChanged, this, &DummyPlugin::updateLogStateChange);
    
        QString hostname = QString("127.0.0.1");
        int port = 1183;
    
        m_client->setHostname(hostname);
        m_client->setPort(port);
    
        connect(m_client, &QMqttClient::messageReceived, this, [this](const QByteArray &message, const QMqttTopicName &topic) {
                const QString content = QDateTime::currentDateTime().toString()
                            + QLatin1String(" Received Topic: ")
                            + topic.name()
                            + QLatin1String(" Message: ")
                            + message;
                qDebug() << content;
        });
    
        updateLogStateChange();
    }
    
    QString DummyPlugin::getPluginName() {
        return "org.projekt-bienhaus.DummyPlugin";
    }
    
    
    void DummyPlugin::connectToServer() {
        m_client->connectToHost();
        qDebug() << "Connecting to server: " << m_client->hostname();
        /*auto subscription = m_client->subscribe(QString("myTopic"),0);
        if(!subscription) {
            qDebug() << "Not subscribed!";
        }*/
    
    }
    
    void DummyPlugin::updateLogStateChange()
    {
        const QString content = QDateTime::currentDateTime().toString()
                        + QLatin1String(": State Change ")
                        + QString::number(m_client->state());
        qDebug() << content;
    }
    
    main.cpp:
    
    QCoreApplication a(argc, argv);
    ...
    
    PluginLoader loader;
        QList<PluginInterface *> listOfPlugins = loader.loadPlugin();
    
        foreach(auto plugin, listOfPlugins) {
            plugin->connectToServer();
        }
    ...
    

    Does anybody know what i'm doing wrong?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Might be silly question but are you sure your plugin is loaded correctly ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • G Offline
        G Offline
        glasen77
        wrote on last edited by
        #3

        Yes, the plugin loads correctly. Before using QtMtqq i tested the plugin system with a QTimer which outputs a string every 2 seconds.

        1 Reply Last reply
        0
        • Pablo J. RoginaP Offline
          Pablo J. RoginaP Offline
          Pablo J. Rogina
          wrote on last edited by
          #4

          @glasen77 not that I'm an expert with MQTT protocol, but comparing your client with Qt simpleclient example, it seems that you're not subscribing to any topic at all.
          From this tutorial it looks like even if you connect successfully to the MQTT broker (or server) then you need to register your client to "publish" or "subscribe" for a particular topic.

          Upvote the answer(s) that helped you solve the issue
          Use "Topic Tools" button to mark your post as Solved
          Add screenshots via postimage.org
          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          2
          • G Offline
            G Offline
            glasen77
            wrote on last edited by
            #5

            The problem is that i can't subscribe to any topic because the connection is not completed. Is it possible that i do set the hostname in a wrong way? Or can i only use QtMqtt with QApplication and not QCoreApplication?

            The only difference between my code and the simpleclient example is that my code only uses the command line.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              You should also connect the QMqttClient::errorChanged signal to see if you have something going wrong.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              G 1 Reply Last reply
              3
              • SGaistS SGaist

                You should also connect the QMqttClient::errorChanged signal to see if you have something going wrong.

                G Offline
                G Offline
                glasen77
                wrote on last edited by
                #7

                @SGaist : Also tried that. "errorChanged" does nothing. I've completely run out of ideas.

                mrjjM 1 Reply Last reply
                0
                • G glasen77

                  @SGaist : Also tried that. "errorChanged" does nothing. I've completely run out of ideas.

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @glasen77
                  So basically your sample stays in
                  QMqttClient::Connecting state ?

                  do you have multiple clients ?
                  seems you have multiple plugins.
                  you do follow the rule that docs says:
                  "The connection request must contain a unique client identifier. "

                  G 1 Reply Last reply
                  0
                  • mrjjM mrjj

                    @glasen77
                    So basically your sample stays in
                    QMqttClient::Connecting state ?

                    do you have multiple clients ?
                    seems you have multiple plugins.
                    you do follow the rule that docs says:
                    "The connection request must contain a unique client identifier. "

                    G Offline
                    G Offline
                    glasen77
                    wrote on last edited by
                    #9

                    @mrjj :
                    At the moment there is only one client running. And setting an identifier is optional because the class will do that for you:

                    Each client needs to have a unique ID to be able to connect to an MQTT broker. If no client ID is specified by the user, one will be generated automatically when a connection is established.

                    I can run multiple versions of the "simpleclient" example without specifying an identifier.

                    1 Reply Last reply
                    0
                    • Pablo J. RoginaP Offline
                      Pablo J. RoginaP Offline
                      Pablo J. Rogina
                      wrote on last edited by Pablo J. Rogina
                      #10

                      @glasen77 ok, so at this point another difference I see is that you're using your code as a plugin vs the simpleclient being just an app.
                      Is there a real need to use a plugin?
                      Could you try your code as a regular app to see if it works? This way you could discard coding errors regarding network and just focus on the plugin side...

                      Upvote the answer(s) that helped you solve the issue
                      Use "Topic Tools" button to mark your post as Solved
                      Add screenshots via postimage.org
                      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                      G 1 Reply Last reply
                      1
                      • Pablo J. RoginaP Pablo J. Rogina

                        @glasen77 ok, so at this point another difference I see is that you're using your code as a plugin vs the simpleclient being just an app.
                        Is there a real need to use a plugin?
                        Could you try your code as a regular app to see if it works? This way you could discard coding errors regarding network and just focus on the plugin side...

                        G Offline
                        G Offline
                        glasen77
                        wrote on last edited by
                        #11

                        @Pablo-J.-Rogina :

                        Yes, i need to use a plugin. But i solved the problem:

                        After playing around with the Unit-tests of QtMqtt i found out that you explicitly need to inherit from QObject for your Mqtt-Class. Because i'm already inheriting from QObject in the interface for my PluginClass, it was not possible to inherit another time (Don't ask me why, i'm new to C++).

                        I solved my problem by making another class named MqttWrapper which calls all the code to establish the connection to the broker. This class is called from my DummyPlugin-Class. Now the code works perfectly.

                        U 1 Reply Last reply
                        0
                        • G glasen77

                          @Pablo-J.-Rogina :

                          Yes, i need to use a plugin. But i solved the problem:

                          After playing around with the Unit-tests of QtMqtt i found out that you explicitly need to inherit from QObject for your Mqtt-Class. Because i'm already inheriting from QObject in the interface for my PluginClass, it was not possible to inherit another time (Don't ask me why, i'm new to C++).

                          I solved my problem by making another class named MqttWrapper which calls all the code to establish the connection to the broker. This class is called from my DummyPlugin-Class. Now the code works perfectly.

                          U Offline
                          U Offline
                          unamlositp
                          wrote on last edited by
                          #12

                          @glasen77 Hello, I have the same exact problem as yours. Can you please explain me better how did you solve it?

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved