MQTT connection disconnected after connected successfully
-
Hi All
I am using Qt 5.9 with QMQTT-Emqx MQTT library for MQTT communication.
Currently, we have locally installed a mosquitto broker and configured it to use an SSL certificate with the client certificate required on Port 8883.
In our app, we connected to the broker with CA & Client certificate with the following code
QSslConfiguration sslConfig; sslConfig.defaultConfiguration(); sslConfig.setProtocol(QSsl::TlsV1_2); sslConfig.setPeerVerifyMode(QSslSocket::AutoVerifyPeer); sslConfig.setCaCertificates(caCertsList); sslConfig.setLocalCertificate(certs.first()); sslConfig.setPrivateKey(QSslKey(clientKeyFile.readAll(), QSsl::Rsa)); m_client = new QMQTT::Client("192.168.1.13", 8883, sslConfig, true); m_client->setClientId(MQTTConstant::clientId); connect(m_client, &QMQTT::Client::connected, this, &MqttController::onConnected); connect(m_client, &QMQTT::Client::disconnected, this, &MqttController::onDisconnected); connect(m_client, &QMQTT::Client::error, this, &MqttController::onError); m_client->setKeepAlive(10); m_client->setAutoReconnectInterval(1000); m_client->setCleanSession(true); m_client->setAutoReconnect(true); m_client->connectToHost();
Also I checked SSL library details as follows:
- SSL Library Build Version: "OpenSSL 1.0.2j 26 Sep 2016"
- SSL Support: true
- SSL Library Version: "OpenSSL 1.0.2e 3 Dec 2015"
In onConnected() function I also able to publish messages successfully
But after that following logs are appearing2022-06-30T16:24:00.639+05:30 ~ INF ~ controller.mqtt ~ Disconnected from MQTT Broker !!! isConnectedToHost false 2022-06-30T16:24:00.643+05:30 ~ INF ~ controller.mqtt ~ MqttController: Connected to MQTT Broker !!! 2022-06-30T16:24:00.646+05:30 ~ INF ~ controller.mqtt ~ Subscribing to sensor/rtdata topic: true 2022-06-30T16:24:00.652+05:30 ~ WRN ~ controller.mqtt ~ Device is listening messages for Topic: "sensor/rtdata" 2022-06-30T16:24:00.654+05:30 ~ WRN ~ controller.mqtt ~ New Message Arrived... Topic: "sensor/rtdata" Payload: "This is test msg from windows-laptop" 2022-06-30T16:24:00.657+05:30 ~ DBG ~ controller.mqtt ~ MQTT SSL Error: QMQTT::ClientError(SocketRemoteHostClosedError) isConnectedToHost true 2022-06-30T16:24:00.661+05:30 ~ DBG ~ controller.mqtt ~ MQTT SSL Error: QMQTT::ClientError(SocketRemoteHostClosedError) isConnectedToHost true 2022-06-30T16:24:00.664+05:30 ~ INF ~ controller.mqtt ~ Disconnected from MQTT Broker !!! isConnectedToHost false 2022-06-30T16:24:00.667+05:30 ~ INF ~ controller.mqtt ~ MqttController: Connected to MQTT Broker !!! 2022-06-30T16:24:00.669+05:30 ~ WRN ~ controller.mqtt ~ Device is not connected to MQTT Broker !!! 2022-06-30T16:24:00.671+05:30 ~ INF ~ controller.mqtt ~ Subscribing to sensor/rtdata topic: false 2022-06-30T16:24:00.744+05:30 ~ DBG ~ controller.mqtt ~ MQTT SSL Error: QMQTT::ClientError(SocketRemoteHostClosedError) isConnectedToHost false 2022-06-30T16:24:00.747+05:30 ~ DBG ~ controller.mqtt ~ MQTT SSL Error: QMQTT::ClientError(SocketRemoteHostClosedError) isConnectedToHost false 2022-06-30T16:24:00.750+05:30 ~ INF ~ controller.mqtt ~ Disconnected from MQTT Broker !!! isConnectedToHost false 2022-06-30T16:24:00.752+05:30 ~ INF ~ controller.mqtt ~ MqttController: Connected to MQTT Broker !!! 2022-06-30T16:24:00.754+05:30 ~ WRN ~ controller.mqtt ~ Device is not connected to MQTT Broker !!! 2022-06-30T16:24:00.756+05:30 ~ INF ~ controller.mqtt ~ Subscribing to sensor/rtdata topic: false 2022-06-30T16:24:00.788+05:30 ~ DBG ~ controller.mqtt ~ MQTT SSL Error: QMQTT::ClientError(SocketRemoteHostClosedError) isConnectedToHost false 2022-06-30T16:24:00.791+05:30 ~ DBG ~ controller.mqtt ~ MQTT SSL Error: QMQTT::ClientError(SocketRemoteHostClosedError) isConnectedToHost false 2022-06-30T16:24:00.794+05:30 ~ INF ~ controller.mqtt ~ Disconnected from MQTT Broker !!! isConnectedToHost false 2022-06-30T16:24:00.798+05:30 ~ INF ~ controller.mqtt ~ MqttController: Connected to MQTT Broker !!! 2022-06-30T16:24:00.800+05:30 ~ WRN ~ controller.mqtt ~ Device is not connected to MQTT Broker !!! 2022-06-30T16:24:00.802+05:30 ~ INF ~ controller.mqtt ~ Subscribing to sensor/rtdata topic: false 2022-06-30T16:24:00.907+05:30 ~ DBG ~ controller.mqtt ~ MQTT SSL Error: QMQTT::ClientError(SocketRemoteHostClosedError) isConnectedToHost false 2022-06-30T16:24:00.910+05:30 ~ DBG ~ controller.mqtt ~ MQTT SSL Error: QMQTT::ClientError(SocketRemoteHostClosedError) isConnectedToHost false 2022-06-30T16:24:00.913+05:30 ~ INF ~ controller.mqtt ~ Disconnected from MQTT Broker !!! isConnectedToHost false
mosquitto configs:
# secure port with client certificate required listener 8883 allow_anonymous false cafile C:\Program Files\mosquitto\certs\ca.crt certfile C:\Program Files\mosquitto\certs\broker.crt keyfile C:\Program Files\mosquitto\certs\broker.key require_certificate true use_identity_as_username true tls_version tlsv1.2
And above works as expected with mosquitto_sub/mosquitto_pub and another 3rd party MQTT X client tool without any issue
$ mosquitto_pub -h 192.168.1.13 -p 8883 --cafile ca.crt --cert client.crt --key client.key -t test -m "This is message sent over secure port 8883 with client cert with CN as username"
-
Hi,
You should check that with the library author.
Did you consider using the official QtMqtt module ?
-
Hi,
You should check that with the library author.
Did you consider using the official QtMqtt module ?
@SGaist Is the official QtMqtt module available for Qt 5.9? when I tried to import it for Qt 5.9, I was getting an unknown module error as follows:
pro file:QT += mqtt QT += qmqtt
While building getting following error:
:-1: error: Unknown module(s) in QT: mqtt :-1: error: Project ERROR: Unknown module(s) in QT: qmqtt
According to the qtmqtt repo, the minimum supported version is 5.10. That's why I am using the Emqx's qmqtt library.
in which one open issue is there which look related to this:
https://github.com/emqx/qmqtt/issues/187In which they are mentioning some bug related to QTcpSocket, When the remote peer uses shutdown any operation on its end, the local QTcpSocket closes the connection (RemoteHostClosedError).
https://bugreports.qt.io/browse/QTBUG-39518@SGaist Can you please suggest to me which library or module I can use for MQTT with Qt 5.9?
-
@Vijay-R-Gawade Did you actually try it, or just assume that because there was no branch 5.9 that it was not possible?
Others have had success: https://forum.qt.io/topic/101940/what-s-the-minimum-version-for-qtmqtt
Is there a reason for developing a new project on an old version of Qt 5?
-
@Vijay-R-Gawade Did you actually try it, or just assume that because there was no branch 5.9 that it was not possible?
Others have had success: https://forum.qt.io/topic/101940/what-s-the-minimum-version-for-qtmqtt
Is there a reason for developing a new project on an old version of Qt 5?
@ChrisW67 yes, I have not tried to build it for Qt 5.8
I will try to build & use this official module.And regarding the older qt version, yes we have device dependency, BSP not yet supported[in development]