这篇文章主要是分享QT智能家居项目的代码
在我看来,QT MQTT的使用主要难点就是对于环境的搭建,在我们把环境搭建完之后,接下来就是传统手艺了,我们只要知道相应的库函数,调用它,就能实现我们所需要的功能了。
所以,在这里我也只是简单的分享一下我写的第一版代码,希望能够对各位提供到帮助。
若是有什么疑问,或者有什么更好的想法,也可以联系我,我尽量帮你们解决。
我们主要是使用MQTT协议以及ONENET云来实现数据从硬件端到PC端的传输以及控制指令的下发。
mainwindows.h代码
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "QMqtt/qmqtt.h"
#include <QTime>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_btn_connect_clicked();
void on_btn_send_clicked();
void on_btn_close_clicked();
void connect_success_slot();
void connect_error_slot(const QMQTT::ClientError error);
void disconnect_slot();
void received_slot(const QMQTT::Message& msg);
void on_clear_clicked();
void on_pushButton_clicked();
void on_pushButton_2_clicked();
void on_pushButton_3_clicked();
void on_pushButton_4_clicked();
void ClickButton0();
void ClickButton1();
void ClickButton2();
void ClickButton3();
// void onTimeOut();
private:
Ui::MainWindow *ui;
QMQTT::Client *client;
int flag0;
int flag1;
int flag2;
int flag3;
QTimer *tim;
};
#endif // MAINWINDOW_H
mainwindows.cpp代码
这几句代码是用来设置你连接的ONENET设备的,因为我不需要更改onenet平台设备,所以直接将其内置在代码中了
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QMqtt/qmqtt.h"
#include <QtNetwork>
#include <QHoverEvent>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//连接onenet云平台
client = new QMQTT::Client(QHostAddress("183.230.40.39"),6002);
client->setClientId("943137931");//设备ID
client->setUsername("506581"); //产品ID
client->setPassword("123456"); //鉴权信息ID
client->setVersion(QMQTT::MQTTVersion::V3_1_1);
connect(client,SIGNAL(connected()),this,SLOT(connect_success_slot()));
connect(client,SIGNAL(received(const QMQTT::Message&)),this,SLOT(received_slot(const QMQTT::Message&)));
connect(client,SIGNAL(error(const QMQTT::ClientError)),this,SLOT(connect_error_slot(const QMQTT::ClientError)));
connect(client,SIGNAL(disconnected()),this,SLOT(disconnect_slot()));
//
connect(ui->btn_send, SIGNAL(clicked()), this, SLOT(ClickButton0()));
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(ClickButton1()));
connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(ClickButton2()));
connect(ui->pushButton_3, SIGNAL(clicked()), this, SLOT(ClickButton3()));
flag0 = 0;
flag1 = 0;
flag2 = 0;
flag3 = 0;
tim = new QTimer();
tim->setInterval(5000);
connect(tim,SIGNAL(timeout()),this,SLOT(onTimeOut()));
tim->start();
}
MainWindow::~MainWindow()
{
delete ui;
}
//连接onenet云
void MainWindow::on_btn_connect_clicked()
{
client->connectToHost();
}
// 连接成功后输出“connect success”并订阅主题
void MainWindow::connect_success_slot(){
qDebug() << "connect success";
client->subscribe("Celsius",0);
client->subscribe("Humidity",0);
client->subscribe("Rain",0);
client->subscribe("Smoke",0);
client->subscribe("Light",0);
client->subscribe("Far",0);
client->subscribe("test",0);
ui->output->setText("连接成功");
}
//连接出现错误时在控制台输出错误
void MainWindow::connect_error_slot(const QMQTT::ClientError error){
qDebug() << "错误" << error;
}
// 断开连接输出disconnect___
void MainWindow::disconnect_slot(){
qDebug() << "disconnect___";
ui->output->setText("断开连接");
}
//断开onenet云
void MainWindow::on_btn_close_clicked()
{
client->disconnectFromHost();
}
// 将对应主题的数据输出到对用的文本框中
void MainWindow::received_slot(const QMQTT::Message& msg){
QString strPayload(msg.payload());
if(msg.topic() == "Celsius")
ui->le_receive->setText(strPayload);
if(msg.topic() == "Humidity")
ui->le_send->setText(strPayload);
if(msg.topic() == "Rain")
ui->rain->setText(strPayload);
if(msg.topic() == "Smoke")
ui->smoke->setText(strPayload);
if(msg.topic() == "Light")
ui->light->setText(strPayload);
if(msg.topic() == "Far")
ui->far->setText(strPayload);
if(msg.topic() == "test")
ui->lineEdit->setText(strPayload);
}
//清空文本框
void MainWindow::on_clear_clicked()
{
ui->le_receive->setText("");
ui->le_send->setText("");
ui->far->setText("");
ui->rain->setText("");
ui->light->setText("");
ui->smoke->setText("");
ui->lineEdit->setText("");
}
//下发指令
void MainWindow::on_btn_send_clicked()
{
if(ui->btn_send->text() == "打开水泵"){
QString controal = "t";
QMQTT::Message msg(110,"Controal",controal.toUtf8());
client->publish(msg);
}
if(ui->btn_send->text() == "关闭水泵"){
QString controal = "y";
QMQTT::Message msg(110,"Controal",controal.toUtf8());
client->publish(msg);
}
}
void MainWindow::on_pushButton_clicked()
{
if(ui->pushButton->text() == "开灯"){
QString controal = "q";
QMQTT::Message msg(110,"Controal",controal.toUtf8());
client->publish(msg);
}
if(ui->pushButton->text() == "关灯"){
QString controal = "w";
QMQTT::Message msg(110,"Controal",controal.toUtf8());
client->publish(msg);
}
}
void MainWindow::on_pushButton_2_clicked()
{
if(ui->pushButton_2->text() == "开警报"){
QString controal = "e";
QMQTT::Message msg(110,"Controal",controal.toUtf8());
client->publish(msg);
}
if(ui->pushButton_2->text() == "关警报"){
QString controal = "r";
QMQTT::Message msg(110,"Controal",controal.toUtf8());
client->publish(msg);
}
}
void MainWindow::on_pushButton_3_clicked()
{
if(ui->pushButton_3->text() == "打开风扇"){
QString controal = "d";
QMQTT::Message msg(110,"Controal",controal.toUtf8());
client->publish(msg);
}
if(ui->pushButton_3->text() == "关闭风扇"){
QString controal = "f";
QMQTT::Message msg(110,"Controal",controal.toUtf8());
client->publish(msg);
}
}
void MainWindow::on_pushButton_4_clicked()
{
QString controal = "z";
QMQTT::Message msg(110,"Controal",controal.toUtf8());
client->publish(msg);
}
void MainWindow::ClickButton0()
{
if(!flag0){
ui->btn_send->setText("关闭水泵");
flag0 = 1;
}
else{
ui->btn_send->setText("打开水泵");
flag0 = 0;
}
}
void MainWindow::ClickButton1()
{
if(!flag1){
ui->pushButton->setText("关灯");
flag1 = 1;
}
else{
ui->pushButton->setText("开灯");
flag1 = 0;
}
}
void MainWindow::ClickButton2()
{
if(!flag2){
ui->pushButton_2->setText("关警报");
flag2 = 1;
}
else{
ui->pushButton_2->setText("开警报");
flag2 = 0;
}
}
void MainWindow::ClickButton3()
{
if(!flag3){
ui->pushButton_3->setText("关闭风扇");
flag3 = 1;
}
else{
ui->pushButton_3->setText("打开风扇");
flag3 = 0;
}
}
//void MainWindow::onTimeOut()
//{
// QString controal = "i";
// QMQTT::Message msg(110,"Controal",controal.toUtf8());
// client->publish(msg);
//}
运行结果
连接成功状态
断开连接状态
这次代码只是第一版,简单的实现了数据接收与指令下发的功能,若是以后有改进,会在之后分享给大家。
代码链接:
百度网盘:
链接:https://pan.baidu.com/s/1Qh5EV0x_xVEhUhfWkLMQpQ?pwd=flas
提取码:flas文章来源:https://www.toymoban.com/news/detail-602564.html
希望对诸君有所帮助文章来源地址https://www.toymoban.com/news/detail-602564.html
到了这里,关于QT使用MQTT协议连接ONENET云平台:智能家居PC客户端开发笔记-2 项目开发的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!