Problems with Open-Source Downloads read https://www.qt.io/blog/problem-with-open-source-downloads and https://forum.qt.io/post/638946

is offical mqt has not Autoreconnect future as in the emqtd ?



  • Hi,

    I saw there is

    void setAutoReconnect(const bool value);
    void setAutoReconnectInterval(const int autoReconnectInterval);
    

    in the : https://github.com/emqtt/qmqtt
    link text

    do we have same in the official one ? I couldnt find it ?


  • Lifetime Qt Champion

    @RahibeMeryem I would say it depends on the version you're using. What version of qmqtt do you use?
    And do you mean the Client class?



  • yes offical qmtt Client class.

    I could manage to use qmqtt in Qt thread successfully. I couldn't find a full example such as :

    qmqtt Client lives a thread created by movetothread.

    connet
    subscribe
    sendmessage
    reconnect-at-disconnet
    imeediately work on the received message.
    QTimer usage in that thread.


  • Lifetime Qt Champion

    @RahibeMeryem See https://github.com/emqtt/qmqtt/blob/master/src/mqtt/qmqtt_client.h, there are all these setAutoReconnect*() methods
    Why do you need a thread?
    Implement first without threads.



  • its perfectly working without thread.

    I need to integrate it to our main framework as a thread which is responsible to send/reveve data

    looking now for QUdpSocket for messaging between pyqt5 and qt


  • Lifetime Qt Champion

    @RahibeMeryem said in is offical mqt has not Autoreconnect future as in the emqtd ?:

    messaging between pyqt5 and qt

    I don't get this: do you mean in same application or are those two different applications?



  • I tested qmqtt without thread in Qt c++ and perfectly working .

    (I am using mqt to communicate between Qt c++ and pyqqt)

    Than I want to use in the my c++ app as thread like:

    in mainWidget:

    
        c_qmqtt_thread = new QThread;
        c_queue = new Central_que;
        c_queue->moveToThread(c_qmqtt_thread);
        c_qmqtt_thread->start();
    

    in Central_que.h:

    #ifndef CENTRAL_QUE_H
    #define CENTRAL_QUE_H
    
    #include <QObject>
    #include <QtMqtt>
    //#include "qmqtt.h"
    
    class Central_que : public QObject
    {
        Q_OBJECT
    public:
        explicit Central_que(QObject *parent = 0);
    
        QMqttClient *c_client;
        const QString topic = "qtmqtt/saf-0";
        const QString result_topic = "result/light";
        const QString hostname = "localhost";
        QTimer *msg_timer;
    private:
    
    signals:
    
    public slots:
        void cent_starting_connection();
        void cent_send_message();
        void succesfully_connected();
        void status_changed();
        void sendMessages();
    };
    
    #endif // CENTRAL_QUE_H
    
    

    Cental_que.cpp:

    #include "central_que.h"
    
    Central_que::Central_que(QObject *parent) : QObject(parent)
    {
    //    qRegisterMetaType<QMqttClient::ClientState>("QMqttClient::ClientState");
    
        qDebug() << " CENTRAL QUEUE THREAD started" << endl;
    
    //    cent_starting_connection();
    }
    
    void Central_que::cent_starting_connection()
    {
    
        c_client = new QMqttClient(this);
        qDebug() << "Central :: Connecting....  " << endl;
        qDebug() << "Central :: Connecting....  " << endl;
        qDebug() << "Central :: Connecting....  " << endl;
        qDebug() << "Central :: Connecting....  " << endl;
        qDebug() << "Central :: Connecting....  " << endl;
    
        connect(c_client , &QMqttClient::stateChanged , this , &Central_que::status_changed);
        connect(c_client , &QMqttClient::connected , this , &Central_que::succesfully_connected);
    
        c_client->setHostname(hostname);
        c_client->setPort(1883);
        c_client->setClientId("B-01");
        c_client->setKeepAlive(200);
    
        c_client->connectToHost();
    
    
    }
    
    void Central_que::cent_send_message()
    {
    
    }
    
    void Central_que::succesfully_connected()
    {
        qDebug() << "Central : SUCCESFULLY CONNECTED . .  : " << c_client->state() << endl;
        qDebug() << "Central : SUCCESFULLY CONNECTED . .  : " << c_client->state() << endl;
        qDebug() << "Central : SUCCESFULLY CONNECTED . .  : " << c_client->state() << endl;
    
        msg_timer = new QTimer;
        connect(msg_timer, SIGNAL(timeout()) , this , SLOT(sendMessages()));
        msg_timer->start(4000);
    
         auto subscription = c_client->subscribe(result_topic,2);
        if (!subscription) {
            qDebug() << "Error Could not subscribe. Is there a valid connection?" << endl;
            return;
        }
        else
            qDebug() << " SUBSCRIBED Succefully " << endl;
    }
    
    
    
    
    
    void Central_que::status_changed()
    {
        qDebug() << "Central : State Changed : " << c_client->state() << endl;
        qDebug() << "Central : State Changed : " << c_client->state() << endl;
        qDebug() << "Central : State Changed : " << c_client->state() << endl;
    
    }
    
    void Central_que::sendMessages()
    {
        auto id = c_client->publish(topic, "bytarray" , 2 ,true);
        qDebug() << "serverRespond : " << id << endl;
    }
    
    

    not copleted but this is second version. to give an idea.

    I want to use from my c++ app as a realible thread , re-connect if disconnected .

    I will feed the data from main thread through direct call or signal/slot.

    But when it comes to thread I couldnt utilize qmqtt as I expected.

    need a simple working basic example , could be useful for all of us.

    Best


  • Lifetime Qt Champion

    Hi,

    For that example, you should talk with that module's author, it's not an official Qt module.

    Qt's mqtt module can be found here.



  • I mean official qmqtt , is there any auto reconnect ?

    or how can I properly re-connect if there is disconnection etc ?


Log in to reply