QT uses the MQTT protocol to remotely control LED lights

Source: Internet
Author: User
test Environment: mqtt_client (Publish) QT 5.6.0 win10 mqtt_client (Subscribe) Ubuntu 12.04 mqtt_broker mosquitto ubuntu 12.0.4 the QT program was developed using the QMQTT library download address Effect:

QT Client code: mainwindow.h

#ifndef mainwindow_h
#define MAINWINDOW_H

#include <QMainWindow>
#include "qmqtt.h"

Namespace Ui {
class MainWindow;
}

Class Mainwindow:public Qmainwindow
{
    q_object public

:
    explicit MainWindow (Qwidget *parent = 0);
    ~mainwindow ();

Private:
    Ui::mainwindow *ui;
    Qmqtt::client *_client;
Private slots:
    void OnConnect ();
    void OnDisconnect ();
    void Ledonslot ();
    void Ledoffslot ();
    void Adjustlightness ();
};

#endif//Mainwindow_h
Mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QString> #include <QDebug> MainWindow::
    MainWindow (Qwidget *parent): Qmainwindow (parent), UI (new Ui::mainwindow) {ui->setupui (this);
    Ui->luxhorizontalslider->setminimum (0);
    Ui->luxhorizontalslider->setmaximum (100);
    _client = new Qmqtt::client ();
    Qobject::connect (Ui->connectbtn,signal (clicked (bool)), This,slot (OnConnect ()));
    Qobject::connect (Ui->disconnectbtn,signal (clicked (bool)), This,slot (OnDisconnect ()));
    Qobject::connect (Ui->ledonbtn,signal (clicked (bool)), This,slot (Ledonslot ()));
    Qobject::connect (Ui->ledoffbtn,signal (clicked (bool)), This,slot (Ledoffslot ()));
Qobject::connect (Ui->luxhorizontalslider,signal (valuechanged (int.)), This,slot (Adjustlightness ()));

} mainwindow::~mainwindow () {Delete UI;}
    void Mainwindow::onconnect () {qhostaddress host ("127.0.0.1");
    quint16 port = 1883;
    _client->sethost (host); _client-> Setport (port);
    _client->setclientid ("Client_id_1");
    _client->setusername ("Sagarfan");
    _client->setpassword ("password");
    _client->connecttohost ();
    if (_client->isconnectedtohost ()) {Ui->statustextedit->settext ("connected"); }} void Mainwindow::ondisconnect () {if (_client->isconnectedtohost ()) {_client->disconnectfromhos
        T ();
    Ui->statustextedit->settext ("Disconnected");
    }} void Mainwindow::ledonslot () {QString payload = "{\" cmd\ ": 1}";
    Qdebug () << payload;
    Qmqtt::message msg (0, "cmd", Payload.tolatin1 ());
    if (_client->isconnectedtohost ()) {_client->publish (msg);
    } else {qdebug () << "Not Connected";
    }} void Mainwindow::ledoffslot () {QString payload ("{\" cmd\ ": 0}");
    Qdebug () << payload;
    Qmqtt::message msg (0, "cmd", Payload.tolatin1 ()); if (_client->isconnectedtohost ()) {_client->pubLish (msg);
    } else {qdebug () << "Not Connected";
    }} void Mainwindow::adjustlightness () {QString tmp = "\" lux\ ":";
    QString postfix;
    Postfix = Qstring::number (Ui->luxhorizontalslider->value ());
    Qdebug () << ui->luxhorizontalslider->value ();
    TMP + = Postfix;
    TMP + = "}";
    QString prefix ("{\" cmd\ ": 3,");

    QString payload (PREFIX+TMP);
    Qdebug () << payload;
    Qmqtt::message msg (0, "cmd", Payload.tolatin1 ());
    if (_client->isconnectedtohost ()) {_client->publish (msg);
    } else {qdebug () << "Not Connected";
 }
}
main.cpp
#include <QApplication>
#include "mainwindow.h"
#include "qmqtt.h"

int main (int argc, char *argv[])
{
    qapplication A (argc, argv);
    MainWindow W;
    W.show ();

    return a.exec ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.