简介
接接上面写!!!!!!!!!!!!!!!
阿里云配置部分
阿里云的连接分为三部分
1)网关的WiFi模块连接到阿里云;
2)QT上位机连接到阿里云;
3)阿里云的平台配置。
其中第二和第三部分不讲解了不会的可以参考QT作为设备接入阿里云平台这也是我前面写的文章,那么这部分的重点放到WiFi如何连接到阿里云
WiFi模块连接到阿里云
关于WiFi的选型,我选择的是简单而又方便的HC-25。它内部集成AT指令的MQTT协议,小白专属。
下位机连接阿里云通过HC-25WiFi模块自带的MQTT协议连接到阿里云物联网平台,用AT指令配置HC-25WiFi模块订阅和发布对应阿里云的设备。
首先查询设备证书找到ProductKey、DeviceName、DeviceSecret,复制参数到密码生成器中,自定义clientld的值。点击Generate生成password,并保存clientld和password的值。
(1)设置MQTT连接域名:
AT+SOCK=MQTT, a1W0uRZgqlY.iot-as-mqtt.cn-shanghai.aliyuncs.com,1883
(2)设置保活时间:
AT+MQTTK=60
(3)设置MQTT的Connect报文参数:
①AT+MQTTCID= 456789| securemode=2, signmethod = hmacsha1|
②AT+MQTTUSERNAME= HC_25train&a1W0uRZgqlY
③AT+MQTTPASSWORD= 57************************************
(4)设置MQTT订阅消息主题
AT+MQTTSUBTOPIC=/a1W0uRZgqlY/Appx/user/update
(5)设置MQTT推送消息主题
AT+MQTTPUBTOPIC=/a1W0uRZgqlY/Appx/user/get
应用程序设计与实现
系统的主界面可查询两个大棚内的终端节点所采集的所有数据并可以调控环境预设值,可手动开启或关闭当前大棚的调控设备。阈值管理功能,能够为不同的大棚的不同环境参数设置阈值,当环境数据超出阈值范围将发出警报并发出指令开启相应的环境设备并调控环境参数。数据管理功能,能够查看所有大棚的数据,便于用户进行调用和处理。
上位机通过自带的MQTT库函数接入阿里云服务器,以JASON格式发送数据到阿里云物联网平台。按照JASON格式解析云端发送的数据并显示数据到对应的界面上。
应用程序界面展示
主界面是①,然后点击设置参数后会跳转到界面②,点击手动控制会跳转到界面③。界面②点击大棚一会跳转到界面④,点击大棚二会跳转到界面⑤。
QT代码
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "choose.h"
#include "control.h"
#include "pre2.h"
#include "setpre.h"
#include "qmqtt.h"
#include "QHostAddress"
#include "QMessageBox"
#include "QTimer"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
time.start(1);
connect(&time,&QTimer::timeout,[=](){refreshscreen();});
PushButton();
//连接到云
client = new QMQTT::Client(QHostAddress::LocalHost,1883);
client->setHostName("iot-060a06sa.mqtt.iothub.aliyuncs.com");
client->setPort(1883);
client->setUsername("Appx&a1W0uRZgqlY");
client->setPassword("195092B40DC99D223132DAD822DF49840EFE2621");
client->setClientId("456321|securemode=2,signmethod=hmacsha1|");
client->connectToHost();
//订阅
// /a1W0uRZgqlY/Appx/user/get
client->subscribe("/a1W0uRZgqlY/Appx/user/get");
connect(client,&QMQTT::Client::received,this,&MainWindow::mqttSetData);
//推送
// /a1W0uRZgqlY/Appx/user/update
//显示判断
setWindowFlags(Qt::FramelessWindowHint);
this->setAutoFillBackground(true);
QPalette palette;
//palette.setColor(QPalette::Background, QColor(0,0,0));
QPixmap pix_img;
pix_img.load(":/start.png");
pix_img = pix_img.scaled(1080, 2430,Qt::IgnoreAspectRatio);
palette.setBrush(QPalette::Background, QBrush(pix_img));
this->setPalette(palette);
}
void MainWindow::mqttSetData(QMQTT::Message mess)
{
qDebug()<<mess.topic()<<"::"<<mess.payload();
QJsonParseError err;//错误解析
QJsonDocument document = QJsonDocument::fromJson(mess.payload(),&err);
QJsonObject jsonObj = document.object();
if(err.error != QJsonParseError::NoError)
{
qDebug()<<"JSON格式错误";
}
//大棚一号
GH.CO2[0]=jsonObj["CO2_1"].toInt();
GH.CO2pre[0]=jsonObj["CO2pre_1"].toInt();
GH.PH[0]=jsonObj["PH_1"].toInt();
GH.PHpre[0]=jsonObj["PHpre_1"].toInt();
GH.SoilHum[0]=jsonObj["SoilHum_1"].toInt();
GH.SoilHumpre[0]=jsonObj["SoilHumpre_1"].toInt();
// GH.beef[0]=jsonObj["beef_1"].toInt();
// GH.fan[0]=jsonObj["fan_1"].toInt();
// GH.water[0]=jsonObj["water_1"].toInt();
//大棚二号
GH.CO2[1]=jsonObj["CO2_2"].toInt();
GH.CO2pre[1]=jsonObj["CO2pre_2"].toInt();
GH.PH[1]=jsonObj["PH_2"].toInt();
GH.PHpre[1]=jsonObj["PHpre_2"].toInt();
GH.SoilHum[1]=jsonObj["SoilHum_2"].toInt();
GH.SoilHumpre[1]=jsonObj["SoilHumpre_2"].toInt();
// GH.beef[1]=jsonObj["beef_2"].toInt();
// GH.fan[1]=jsonObj["fan_2"].toInt();
// GH.water[1]=jsonObj["water_2"].toInt();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::refreshscreen(){
//大棚一
//二氧化碳
if(GH.CO2[0]>GH.CO2pre[0]){
ui->CO2_1->setText("CO2浓度过高");
ui->CO2_1->setStyleSheet("background-color: rgb(255, 0, 0);");
}else{
ui->CO2_1->setText("CO2浓度正常");
ui->CO2_1->setStyleSheet("background-color: rgb(255, 255, 255);");
}
//PH值
if(GH.PH[0]<GH.PHpre[0]-1100){
ui->PH_1->setText("PH值过低");
ui->PH_1->setStyleSheet("background-color: rgb(255, 0, 0);");
}
else if(GH.PH[0]>GH.PHpre[0]+1100){
ui->PH_1->setText("PH值过高");
ui->PH_1->setStyleSheet("background-color: rgb(255, 0, 0);");
}else{
ui->PH_1->setText("PH正常");
ui->PH_1->setStyleSheet("background-color: rgb(255, 255, 255);");
}
//SoilHum
if(GH.SoilHum[0]>GH.SoilHumpre[0]){
ui->Soil_1->setText("土壤湿度过高");
ui->Soil_1->setStyleSheet("background-color: rgb(255, 0, 0);");
}else{
ui->Soil_1->setText("土壤湿度正常");
ui->Soil_1->setStyleSheet("background-color: rgb(255, 255, 255);");
}
//大棚二号
//二氧化碳
if(GH.CO2[1]>GH.CO2pre[1]){
ui->CO2_2->setText("CO2浓度过高");
ui->CO2_2->setStyleSheet("background-color: rgb(255, 0, 0);");
}else{
ui->CO2_2->setText("CO2浓度正常");
ui->CO2_2->setStyleSheet("background-color: rgb(255, 255, 255);");
}
//PH值
if(GH.PH[1]<GH.PHpre[1]-1100){
ui->PH_2->setText("PH值过低");
ui->PH_2->setStyleSheet("background-color: rgb(255, 0, 0);");
}
else if(GH.PH[1]>GH.PHpre[1]+1100){
ui->PH_2->setText("PH值过高");
ui->PH_2->setStyleSheet("background-color: rgb(255, 0, 0);");
}else{
ui->PH_2->setText("PH正常");
ui->PH_2->setStyleSheet("background-color: rgb(255, 255, 255);");
}
//SoilHum
if(GH.SoilHum[1]>GH.SoilHumpre[1]){
ui->Soil_2->setText("土壤湿度过高");
ui->Soil_2->setStyleSheet("background-color: rgb(255, 0, 0);");
}else{
ui->Soil_2->setText("土壤湿度正常");
ui->Soil_2->setStyleSheet("background-color: rgb(255, 255, 255);");
}
}
void MainWindow::PushButton(){
connect(ui->setpre,&QPushButton::clicked,this,&MainWindow::Choose_ow);
connect(ui->controlhand,&QPushButton::clicked,this,&MainWindow::Control);
}
void MainWindow::Choose_ow(){
cho =new choose(&GH,client);
cho->show();
}
void MainWindow::Control(){
con=new control(client);
con->show();
}
void MainWindow::sendTopic(QString data){
client->publish(QMQTT::Message(0,"/a1W0uRZgqlY/Appx/user/update",data.toLocal8Bit()));
}
有问题评论区或私信-_-