MQTT porting source code package download https://github.com/qt/qtmqtt
1. Transplantation on QT
1. Use qt to open the source code package
Compile using the suite, then transplant after compilation
2. Copy all header files under the qtmqtt-5.12.8\src\mqtt file of the source code package to the Mqtt file created in include
3.
4.
Copy these two files to
5.
Add this file to
2. Porting QT for Android
1. Use qt to open the source code package
Use the android kit to compile, and then transplant after compilation (albeit an alarm, but it does not affect it)
2. Copy all header files under the qtmqtt-5.12.8\src\mqtt file of the source code package to the Mqtt file created in include
3.
Copy these three to
4.
Move this file to
3. Use mqtt to write code and conduct experiments
1. Add in .pro
2. Related codes
.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QDateTime> #include <mqtt/qmqttclient.h> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); QMqttClient *m_client = nullptr; void MyMQTTSubscribe(QString); void MyMQTTSendMessage(const QString, const QString); signals: public slots: void brokerConnected(); void updateLogStateChange(); void brokerDisconnected(); void receiveMess(const QByteArray & amp;, const QMqttTopicName & amp;); private slots: void on_connect_btn_clicked(); void on_sub_btn_clicked(); void on_send_btn_clicked(); private: Ui::MainWindow *ui; bool connect_flag=false; }; #endif // MAINWINDOW_H
.c
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QTimer> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } //subscription void MainWindow::MyMQTTSubscribe(QString str) { auto subscription = m_client->subscribe(str, 0); if (!subscription) { qDebug() << "Could not subscribe. Is there a valid connection?"; return; } } //time void MainWindow::updateLogStateChange() { const QString content = QDateTime::currentDateTime().toString() + QLatin1String(": State Change") + QString::number(m_client->state()) + QLatin1Char('\\ '); qDebug() << content; } //connect void MainWindow::brokerConnected() { qDebug() << "Connected!"; QString text=QString{"Connection successful, ip=%1,port=%2"}.arg(ui->ip_lineEdit->text()).arg(ui->port_lineEdit->text()); ui->connect_label->setText(text); connect_flag=true; if(m_client->state() == QMqttClient::Connected){ //Connect to receive messages connect(m_client, SIGNAL(messageReceived(QByteArray,QMqttTopicName)), this, SLOT(receiveMess(QByteArray,QMqttTopicName))); } } //disconnect void MainWindow::brokerDisconnected() { qDebug() << "Disconnected!"; } //take over void MainWindow::receiveMess(const QByteArray & amp;message, const QMqttTopicName & amp;topic) { QString content; content = QDateTime::currentDateTime().toString() + QLatin1Char('\\ '); content + = QLatin1String(" Received Topic: ") + topic.name() + QLatin1Char('\\ '); content + = QLatin1String(" Message: ") + message + QLatin1Char('\\ '); qDebug() << content; ui->recv_textEdit->append(content); } //send void MainWindow::MyMQTTSendMessage(const QString topic, const QString message) { if (m_client->publish(topic, message.toUtf8()) == -1){ qDebug() << "Could not publish message"; } } void MainWindow::on_connect_btn_clicked() { QString ip=ui->ip_lineEdit->text(); QString port=ui->port_lineEdit->text(); if(ip=="" || port=="") { qDebug()<<"ip or port content is empty"; return ; } m_client = new QMqttClient(this); m_client->setHostname(ip); m_client->setPort(port.toInt()); m_client->connectToHost(); // QTimer *time1=new QTimer(this); // time1->setInterval(5000); // time1->start(); // connect(time1, & amp;QTimer::timeout,this,[=](){ // qDebug()<<1; // MyMQTTSendMessage("mcu_test","hello"); // }); connect(m_client, & amp;QMqttClient::connected, this, & amp;MainWindow::brokerConnected); connect(m_client, & amp;QMqttClient::stateChanged, this, & amp;MainWindow::updateLogStateChange); connect(m_client, & amp;QMqttClient::disconnected, this, & amp;MainWindow::brokerDisconnected); connect(m_client, & amp;QMqttClient::pingResponseReceived, this, [=]() { const QString content = QDateTime::currentDateTime().toString() + QLatin1String(" PingResponse") + QLatin1Char('\\ '); qDebug() << content; }); } void MainWindow::on_sub_btn_clicked() { if(connect_flag) { QString sub=ui->sub_lineEdit->text(); if(sub=="") { qDebug()<<"Subscription content"; return ; } m_client->subscribe(sub, 0);//Subscribe //MyMQTTSubscribe("topic2");//Subscribe } else qDebug()<<"Not connected"; } void MainWindow::on_send_btn_clicked() { if(connect_flag) { QString topic=ui->topic_lineEdit->text(); QString text=ui->text_lineEdit->text(); if(topic=="" || text=="") { qDebug()<<"The topic or content is empty"; return ; } else { MyMQTTSendMessage(topic,text); } else qDebug()<<"Not connected"; }
3. The effect is as shown in the figure
window side
android side