前两篇文章主要说了如何使用阿里云和MQTT.fx。已经实现了设备与设备之间的通信,如果还不会使用阿里云实现设备之间通信的,先移步去看我的前两篇文章,按着步骤来小白也行。这篇文章主要展示实际应用,以及QT客户端核心代码。
主控芯片:stm32f103 c8t6
wifi模块:esp8266 01s
温湿度模块:dht11传感器
usb转ttl进行串口调试,stlink _v2进行程序下载。还有面包板,连接线若干。
连接好之后就是这个样子了:
QT提供了MQTT官方库,下载库之后直接使用就可以了,使用起来是非常的方便好用。我的QT界面1.0版本就是下面这样了。可以收到硬件发送过来的信息:这是收到的温度信息。现在显示的是Json字符串数据,可以用解析后拿到具体的数据,你们可以再做一个可视化的label什么的显示出来。
连接成功后串口调试助手就会显示如下数据:
顺便可以去阿里云去看看硬件、软件有没有连接上:
我这里QT客户端连接的qt_client这个设备,stm32硬件连接的iot_mq2这个设备,都是已经在线了。
QT代码:
mainwindow.c
#include "mainwindow.h"
#include "ui_mainwindow.h"
static QString HostName=" ";
static quint16 Port = 1883;
static QString username = "" ;
static QString password = "";
static QString ClientId = "";
static QString m_topic_publish= "/i8sh22T2TuH/qt_client/user/update";
static QString m_topic_get= "/i8sh22T2TuH/qt_client/user/get";
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
mqtt_client=new QMqttClient;
ConnectOrDisConnect();
Publish(m_topic_publish);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::ConnectOrDisConnect()
{
connect(ui->pushButton_connect,&QPushButton::clicked,this,[=](){
if(mqtt_client->state()==QMqttClient::Disconnected)
{
mqtt_client->setHostname(HostName);
mqtt_client->setPort(Port);
mqtt_client->setUsername(username);
mqtt_client->setPassword(password);
mqtt_client->setClientId(ClientId);
mqtt_client->connectToHost();
if(mqtt_client->state()==QMqttClient::Disconnected)
{
qDebug()<<mqtt_client->error();
}
else if(mqtt_client->state()==QMqttClient::Connecting)
{
ui->pushButton_connect->setText("断开");
ui->label_state->setText("Connect");
Subcribe();
}
}
else
{
mqtt_client->disconnectFromHost();
ui->pushButton_connect->setText("连接");
ui->label_state->setText("DisConnect");
}
});
}
void MainWindow::Subcribe()
{
if(mqtt_client->state()==QMqttClient::Connecting)
{
qDebug()<<"connect success";
mqtt_client->subscribe(m_topic_get);
connect(mqtt_client, SIGNAL(messageReceived(QByteArray,QMqttTopicName)), this, SLOT(receiveMess(QByteArray,QMqttTopicName)));
}
}
void MainWindow::Publish(QString topic)
{
connect(ui->pushButton_publish,&QPushButton::clicked,this,[=](){
if(ui->lineEdit_publish->text()!="")
{
if (mqtt_client->publish(topic, ui->lineEdit_publish->text().toUtf8()) == -1)
QMessageBox::critical(this, QLatin1String("Error"), QLatin1String("Could not publish message"));
}
});
}
void MainWindow::receiveMess(QByteArray message, QMqttTopicName name)
{
Q_UNUSED(name);
QString msg=message;
qDebug()<<msg;
ui->textBrowser_recv->append(msg);
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "QDebug"
#include "QLabel"
#include "QFile"
#include "QtMqtt/qmqttclient.h"
#include "QJsonObject"
#include "QJsonDocument"
#include "QJsonArray"
#include "QMessageBox"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
void init_mqtt();
void sendTopic(QString data);
void ConnectOrDisConnect();
void Subcribe();
void Publish(QString topic);
public slots:
void receiveMess(QByteArray message,QMqttTopicName name);
private:
QMqttClient *mqtt_client;
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.ui
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
我正在编写一个包含C扩展的gem。通常当我写一个gem时,我会遵循TDD的过程,我会写一个失败的规范,然后处理代码直到它通过,等等......在“ext/mygem/mygem.c”中我的C扩展和在gemspec的“扩展”中配置的有效extconf.rb,如何运行我的规范并仍然加载我的C扩展?当我更改C代码时,我需要采取哪些步骤来重新编译代码?这可能是个愚蠢的问题,但是从我的gem的开发源代码树中输入“bundleinstall”不会构建任何native扩展。当我手动运行rubyext/mygem/extconf.rb时,我确实得到了一个Makefile(在整个项目的根目录中),然后当
所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择
这是一道面试题,我没有答对,但还是很好奇怎么解。你有N个人的大家庭,分别是1,2,3,...,N岁。你想给你的大家庭拍张照片。所有的家庭成员都排成一排。“我是家里的friend,建议家庭成员安排如下:”1岁的家庭成员坐在这一排的最左边。每两个坐在一起的家庭成员的年龄相差不得超过2岁。输入:整数N,1≤N≤55。输出:摄影师可以拍摄的照片数量。示例->输入:4,输出:4符合条件的数组:[1,2,3,4][1,2,4,3][1,3,2,4][1,3,4,2]另一个例子:输入:5输出:6符合条件的数组:[1,2,3,4,5][1,2,3,5,4][1,2,4,3,5][1,2,4,5,3][
我试图在索引页中创建一个超链接,但它没有显示,也没有给出任何错误。这是我的index.html.erb代码。ListingarticlesTitleTextssss我检查了我的路线,我认为它们也没有问题。PrefixVerbURIPatternController#Actionwelcome_indexGET/welcome/index(.:format)welcome#indexarticlesGET/articles(.:format)articles#indexPOST/articles(.:format)articles#createnew_articleGET/article
我已经构建了一些serverspec代码来在多个主机上运行一组测试。问题是当任何测试失败时,测试会在当前主机停止。即使测试失败,我也希望它继续在所有主机上运行。Rakefile:namespace:specdotask:all=>hosts.map{|h|'spec:'+h.split('.')[0]}hosts.eachdo|host|begindesc"Runserverspecto#{host}"RSpec::Core::RakeTask.new(host)do|t|ENV['TARGET_HOST']=hostt.pattern="spec/cfengine3/*_spec.r
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c