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. qmqtt auto reconnect on disconnet

qmqtt auto reconnect on disconnet

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 2 Posters 2.8k Views
  • 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.
  • R Offline
    R Offline
    RahibeMeryem
    wrote on last edited by
    #1

    Hi

    I am connecting to the qmqtt

    m_client = new QMqttClient(this);
    
    
        m_client->setHostname("localhost");
        m_client->setPort(1883);
        m_client->setClientId("RASPBERRY-009");
    
        m_client->setUsername("pullu");
        m_client->setPassword("pullu");
        m_client->setKeepAlive(30);
    
        m_client->setCleanSession(false);
    
        m_client->connectToHost();
        QObject::connect(m_client, &QMqttClient::stateChanged, this, &Publisher::updateStateChange);
        QObject::connect(m_client, &QMqttClient::connected, this, &Publisher::sendMessages);
    

    When its connected looking a folder new files than sending to the emqt broker.

    There sometimes network problem or broker restart etc. Than client lost his connection and state change 1.

    than :

    void Publisher::updateStateChange()
    {
        const QString content = QDateTime::currentDateTime().toString()
                        + QLatin1String(": State Changed : ")
                        + QString::number(m_client->state());
    
        qDebug() << " -- *** **  state change .. : " << content << endl;
        while (m_client->state()  != 2 ){
            qDebug() << " Retrying.... . . . " << endl;
            m_client->setHostname("localhost");
            m_client->setPort(1883);
            m_client->setClientId("RASPBERRY-009");
    
            m_client->setUsername("pullu");
            m_client->setPassword("pullu");
            m_client->setKeepAlive(30);
    
    
            m_client->setCleanSession(false);
    
            m_client->connectToHost();
            const QString content = QDateTime::currentDateTime().toString()
                            + QLatin1String(": State Changed : ")
                            + QString::number(m_client->state());
            qDebug() << " -- *** **  state === .. : " << content << endl;
    
    
            QThread::sleep(2);
    }
    

    I am checking state than try to recover my connection .

    I couldt succed.

    How can I keep my client always connected if bad things happen try to reconnect untill connected :)

    Best

    jsulmJ 1 Reply Last reply
    0
    • R RahibeMeryem

      Hi

      I am connecting to the qmqtt

      m_client = new QMqttClient(this);
      
      
          m_client->setHostname("localhost");
          m_client->setPort(1883);
          m_client->setClientId("RASPBERRY-009");
      
          m_client->setUsername("pullu");
          m_client->setPassword("pullu");
          m_client->setKeepAlive(30);
      
          m_client->setCleanSession(false);
      
          m_client->connectToHost();
          QObject::connect(m_client, &QMqttClient::stateChanged, this, &Publisher::updateStateChange);
          QObject::connect(m_client, &QMqttClient::connected, this, &Publisher::sendMessages);
      

      When its connected looking a folder new files than sending to the emqt broker.

      There sometimes network problem or broker restart etc. Than client lost his connection and state change 1.

      than :

      void Publisher::updateStateChange()
      {
          const QString content = QDateTime::currentDateTime().toString()
                          + QLatin1String(": State Changed : ")
                          + QString::number(m_client->state());
      
          qDebug() << " -- *** **  state change .. : " << content << endl;
          while (m_client->state()  != 2 ){
              qDebug() << " Retrying.... . . . " << endl;
              m_client->setHostname("localhost");
              m_client->setPort(1883);
              m_client->setClientId("RASPBERRY-009");
      
              m_client->setUsername("pullu");
              m_client->setPassword("pullu");
              m_client->setKeepAlive(30);
      
      
              m_client->setCleanSession(false);
      
              m_client->connectToHost();
              const QString content = QDateTime::currentDateTime().toString()
                              + QLatin1String(": State Changed : ")
                              + QString::number(m_client->state());
              qDebug() << " -- *** **  state === .. : " << content << endl;
      
      
              QThread::sleep(2);
      }
      

      I am checking state than try to recover my connection .

      I couldt succed.

      How can I keep my client always connected if bad things happen try to reconnect untill connected :)

      Best

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @RahibeMeryem said in qmqtt auto reconnect on disconnet:

      while (m_client->state() != 2 ){

      NEVER use such loops and QThread::sleep(2); in event driven applications!
      You're blocking Qt event loop!

      Use a QTimer for reconnect.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • R Offline
        R Offline
        RahibeMeryem
        wrote on last edited by
        #3

        I figured it out before read this :)

        Another question for same topic:

        can I connect signals from threads signal from it ? like :

        Messenqt::Messenqt(QObject *parent) : QObject(parent)
        {
        
        
                 m_client = new QMqttClient(this);
        
        
            m_client->setHostname("localhost");
            m_client->setPort(1883);
            m_client->setClientId("ERRY-009");
        
            m_client->setUsername("xxxxx");
            m_client->setPassword("xxxxx");
            m_client->setKeepAlive(300);
            m_client->setCleanSession(false);
        
            m_client->connectToHost();
            //yeni slot mekanizmasi. kendi icinde &Publisher örneği..
            QObject::connect(m_client, &QMqttClient::stateChanged, this, &Messenqt::updateStateChange);
        
        
            QObject::connect(m_client, &QMqttClient::connected, this, &Messenqt::subscribe_results);
            QObject::connect(m_client , &QMqttClient::messageReceived , this , &Messenqt::result_received);
        
        //    connect(messqt->m_client, &QMqttClient::stateChanged, messqt, &Messenqt::updateLogStateChange);
        //    connect(m_client, &QMqttClient::disconnected, this, &Publisher::disconnect);
        //    connect(messqt->m_client, &QMqttClient::connected, messqt, &Messenqt::sendingMessage);
        
            msg_timer = new QTimer(this);
            connection_check = new QTimer(this);
        
                    //    m_client->subscribe(result_topic ,2);
        
        
            //TODO big chip save timer.
            connect(msg_timer, SIGNAL(timeout()) , this , SLOT(sendMessages()));
            msg_timer->start(4000);
        
            connect(connection_check , SIGNAL(timeout()) , this , SLOT(reconnect()));
            connection_check->start(5000);
        
        
                 }
        

        This thread started from the main GUI thread.

        When started the app it says :

        QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread
        
        jsulmJ 1 Reply Last reply
        0
        • R RahibeMeryem

          I figured it out before read this :)

          Another question for same topic:

          can I connect signals from threads signal from it ? like :

          Messenqt::Messenqt(QObject *parent) : QObject(parent)
          {
          
          
                   m_client = new QMqttClient(this);
          
          
              m_client->setHostname("localhost");
              m_client->setPort(1883);
              m_client->setClientId("ERRY-009");
          
              m_client->setUsername("xxxxx");
              m_client->setPassword("xxxxx");
              m_client->setKeepAlive(300);
              m_client->setCleanSession(false);
          
              m_client->connectToHost();
              //yeni slot mekanizmasi. kendi icinde &Publisher örneği..
              QObject::connect(m_client, &QMqttClient::stateChanged, this, &Messenqt::updateStateChange);
          
          
              QObject::connect(m_client, &QMqttClient::connected, this, &Messenqt::subscribe_results);
              QObject::connect(m_client , &QMqttClient::messageReceived , this , &Messenqt::result_received);
          
          //    connect(messqt->m_client, &QMqttClient::stateChanged, messqt, &Messenqt::updateLogStateChange);
          //    connect(m_client, &QMqttClient::disconnected, this, &Publisher::disconnect);
          //    connect(messqt->m_client, &QMqttClient::connected, messqt, &Messenqt::sendingMessage);
          
              msg_timer = new QTimer(this);
              connection_check = new QTimer(this);
          
                      //    m_client->subscribe(result_topic ,2);
          
          
              //TODO big chip save timer.
              connect(msg_timer, SIGNAL(timeout()) , this , SLOT(sendMessages()));
              msg_timer->start(4000);
          
              connect(connection_check , SIGNAL(timeout()) , this , SLOT(reconnect()));
              connection_check->start(5000);
          
          
                   }
          

          This thread started from the main GUI thread.

          When started the app it says :

          QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @RahibeMeryem said in qmqtt auto reconnect on disconnet:

          can I connect signals from threads signal from it ?

          What do you mean? Do you mean to connect a signal to a slot living in different threads? If so then yes you can (use Qt::QueuedConnection connection type). But that doesn't mean that what you're doing in other thread with your socket is allowed.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • R Offline
            R Offline
            RahibeMeryem
            wrote on last edited by
            #5

            for example :

            Main thread is Widget.cpp which is updating UI.

            Messenqt.cpp is also a thread created by Widget.cpp with moveThread .

            so how can I use

            connect(m_client, &QMqttClient::connected, this, &Messenqt::subscribe_results);
            

            in Messenqt ?

            jsulmJ 1 Reply Last reply
            0
            • R RahibeMeryem

              for example :

              Main thread is Widget.cpp which is updating UI.

              Messenqt.cpp is also a thread created by Widget.cpp with moveThread .

              so how can I use

              connect(m_client, &QMqttClient::connected, this, &Messenqt::subscribe_results);
              

              in Messenqt ?

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @RahibeMeryem You should do the connect in Widget.cpp and add Qt::QueuedConnection to the connect call

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • R Offline
                R Offline
                RahibeMeryem
                wrote on last edited by
                #7

                Whey there is not an autoconnect on diconnect is Qt qmqtt :) . There is in the pyhthon... is there any ?

                jsulmJ 1 Reply Last reply
                0
                • R RahibeMeryem

                  Whey there is not an autoconnect on diconnect is Qt qmqtt :) . There is in the pyhthon... is there any ?

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @RahibeMeryem You should ask qmqtt project I guess

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    RahibeMeryem
                    wrote on last edited by
                    #9

                    Looking into it, but I am confused qmqtt in thread and connecting signal slot inside that thread also to check connection and re-connect.

                    is there any minimalistic example for qmqtt running in a thread like:
                    mainwindow.cpp

                        messenqt_thread = new QThread();
                        messqt = new Messenqt();
                        messqt->moveToThread(messenqt_thread);
                        messenqt_thread->start();
                    

                    Messenqt.cpp

                    Messenqt::Messenqt(QObject *parent) : QObject(parent)
                    {
                    
                    
                    //        qRegisterMetaType<QMqttClient::ClientState>("QMqttClient::ClientState");
                            
                            m_client = new QMqttClient(this);
                            m_client->setHostname("xx.15.43.84");
                            m_client->setPort(1883);
                            m_client->setClientId("001");
                    
                    
                    
                            qDebug() << "?? is here"  << m_client->state() << endl;
                            if (m_client->state() == QMqttClient::Disconnected){
                                qDebug() << "state Disconnected 01" << endl;}
                            else{qDebug() << "state Connected 01" << endl;};
                    
                    
                                connect(m_client, &QMqttClient::stateChanged, this, &Messenqt::updateLogStateChange);
                                connect(m_client, &QMqttClient::disconnected, this, &Messenqt::brokerDisconnected);
                                connect(m_client, &QMqttClient::connected, this, &Messenqt::succesfully_connected);
                                connect(m_client, &QMqttClient::pingResponseReceived, this , &Messenqt::pingReceived);
                    
                    }
                    
                    void Messenqt::brokerDisconnected()
                    {
                        qDebug() << "Qmqt -- :: Disconnected ????****????*****???? " << endl;
                    
                        if (m_client->state() != 2){
                            qDebug() << " Re-connecting......" << endl ;
                            qDebug ()<< " state :  " << m_client->state() << endl;
                            m_client->setHostname("xx.15.43.84");
                            m_client->setPort(1883);
                            m_client->setClientId("RASPBERRY-009");
                    
                            m_client->setUsername("xx");
                            m_client->setPassword("xx");
                            m_client->setKeepAlive(60);
                            m_client->setCleanSession(false);
                    
                            m_client->connectToHost();
                    
                    
                    }
                    

                    this is not working... I see debug message at the emqt server :

                    14:49:37.455 [debug] Client(78.187.191.133:50395): Terminated for {shutdown,closed}
                    
                    
                    jsulmJ 1 Reply Last reply
                    0
                    • R RahibeMeryem

                      Looking into it, but I am confused qmqtt in thread and connecting signal slot inside that thread also to check connection and re-connect.

                      is there any minimalistic example for qmqtt running in a thread like:
                      mainwindow.cpp

                          messenqt_thread = new QThread();
                          messqt = new Messenqt();
                          messqt->moveToThread(messenqt_thread);
                          messenqt_thread->start();
                      

                      Messenqt.cpp

                      Messenqt::Messenqt(QObject *parent) : QObject(parent)
                      {
                      
                      
                      //        qRegisterMetaType<QMqttClient::ClientState>("QMqttClient::ClientState");
                              
                              m_client = new QMqttClient(this);
                              m_client->setHostname("xx.15.43.84");
                              m_client->setPort(1883);
                              m_client->setClientId("001");
                      
                      
                      
                              qDebug() << "?? is here"  << m_client->state() << endl;
                              if (m_client->state() == QMqttClient::Disconnected){
                                  qDebug() << "state Disconnected 01" << endl;}
                              else{qDebug() << "state Connected 01" << endl;};
                      
                      
                                  connect(m_client, &QMqttClient::stateChanged, this, &Messenqt::updateLogStateChange);
                                  connect(m_client, &QMqttClient::disconnected, this, &Messenqt::brokerDisconnected);
                                  connect(m_client, &QMqttClient::connected, this, &Messenqt::succesfully_connected);
                                  connect(m_client, &QMqttClient::pingResponseReceived, this , &Messenqt::pingReceived);
                      
                      }
                      
                      void Messenqt::brokerDisconnected()
                      {
                          qDebug() << "Qmqt -- :: Disconnected ????****????*****???? " << endl;
                      
                          if (m_client->state() != 2){
                              qDebug() << " Re-connecting......" << endl ;
                              qDebug ()<< " state :  " << m_client->state() << endl;
                              m_client->setHostname("xx.15.43.84");
                              m_client->setPort(1883);
                              m_client->setClientId("RASPBERRY-009");
                      
                              m_client->setUsername("xx");
                              m_client->setPassword("xx");
                              m_client->setKeepAlive(60);
                              m_client->setCleanSession(false);
                      
                              m_client->connectToHost();
                      
                      
                      }
                      

                      this is not working... I see debug message at the emqt server :

                      14:49:37.455 [debug] Client(78.187.191.133:50395): Terminated for {shutdown,closed}
                      
                      
                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @RahibeMeryem said in qmqtt auto reconnect on disconnet:

                      I am confused qmqtt in thread and connecting signal slot inside that thread also to check connection and re-connect

                      As I said you can connect signals slots across threads

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      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