这短时间,我做基于STM32f103 的GPRS的连接,每次都要打开电脑上的mqttfx来进行调试,感觉很麻烦。所以就用QT forAndroid做了一个Android上的MQTT客户端,在此分享给各位老铁。
一、你需要下载官方给的基于QT的MQTT的源码https://github.com/emqtt/qmqtt
二、源码移植到QT for Android的工程里,下载完后解压,把src里所有的.h和.c文件都放到一个文件夹下面去
就像这样
三、现在你就可以写你的代码了,我的是这样的
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include "mqtt/qmqtt.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void onConnected();
quint16 onSubscribed(const QString& topic, const quint8 qos);
void onReceived(const QMQTT::Message& message);
void ondisconnected();
private slots:
void on_pushButton_connection_clicked();
void on_pushButton_subscribe_clicked();
void on_pushButton_publish_clicked();
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
QMQTT::Client * client;
};
#endif // MAINWINDOW_H
mainwindow.c
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mqtt/qmqtt.h"
#include
#include
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//============设置按钮样式================
setStyleSheet("QPushButton{background-color:black;\
color: white; border-radius: 10px; border: 2px groove gray;\
border-style: outset;}"
"QPushButton:hover{background-color:white; color: black;}"
"QPushButton:pressed{background-color:rgb(85, 170, 255);\
border-style: inset; }"
);
//============================
}
MainWindow::~MainWindow()
{
delete ui;
}
/*连接MQTT的槽*/
void MainWindow::onConnected()
{
QMessageBox::information(this,"提示","连接服务器成功");
ui->pushButton_connection->setText("断开连接");
}
/*订阅MQTT的主题*/
quint16 MainWindow::onSubscribed(const QString &topic, const quint8 qos)
{
QMessageBox::information(this,"订阅",topic);
}
/*接收主题信息*/
void MainWindow::onReceived(const QMQTT::Message &message)
{
//QMessageBox::information(this,"收到",QString::fromUtf8(message.payload()));
ui->textBrowser_rev->insertPlainText(message.topic()+":"+'\n'+" "+QString::fromUtf8(message.payload())+'\n');
}
/*断开连接的槽*/
void MainWindow::ondisconnected()
{
ui->pushButton_connection->setText("连接MQTT服务器");
}
/*连接按钮*/
void MainWindow::on_pushButton_connection_clicked()
{
QHostAddress host(QString(ui->lineEdit_address->text().trimmed()));//得到MQTT主机地址
quint16 port = ui->lineEdit_prot->text().toInt();//得到端口号
client = new QMQTT::Client(host,port);
connect(client, &QMQTT::Client::connected, this, &MainWindow::onConnected);
connect(client, &QMQTT::Client::subscribed, this, &MainWindow::onSubscribed );
connect(client, &QMQTT::Client::received, this, &MainWindow::onReceived );
connect(client, &QMQTT::Client::disconnected, this, &MainWindow::ondisconnected );
client->setClientId(ui->lineEdit_id->text().trimmed());//设置用户id
client->setUsername(ui->lineEdit_user->text().trimmed());//设置用户名
client->setPassword(ui->lineEdit_pwd->text().trimmed().toLocal8Bit());//设置密码
client->connectToHost();//连接
}
//订阅主题按钮
void MainWindow::on_pushButton_subscribe_clicked()
{
QString sub = ui->comboBox_sub->currentText().trimmed();
client->subscribe(sub,0);
ui->label_topic->setText("本机订阅主题:"+sub);
}
//发布主题按钮
void MainWindow::on_pushButton_publish_clicked()
{
client->publish(QMQTT::Message(1,ui->comboBox_pub->currentText().trimmed(),ui->lineEdit_topic_text->text().toLocal8Bit()));
}
//清空接收区域文本按钮
void MainWindow::on_pushButton_clicked()
{
ui->textBrowser_rev->clear();
}
运行之后的界面是这样的
四、注意我用的是百度云无接入免费的MQTT服务器,我在上面设定的三个主题分别是stm32_pub,stm32_sub,stm32_w
这个很简单,自已先研究,如果需要源码可以问我。