Browse Source

add mqtt subscribe capability

master
Richard Genoud 5 years ago
parent
commit
01130f0036
5 changed files with 181 additions and 2 deletions
  1. +42
    -0
      soft/thermostat/inc/backgroundloop.h
  2. +6
    -1
      soft/thermostat/inc/mainwindow.h
  3. +126
    -0
      soft/thermostat/src/backgroundloop.cpp
  4. +1
    -0
      soft/thermostat/src/mainwindow.cpp
  5. +6
    -1
      soft/thermostat/thermostat.pro

+ 42
- 0
soft/thermostat/inc/backgroundloop.h View File

@ -0,0 +1,42 @@
// SPDX-License-Identifier: GPL-3.0-or-later
/*
* Qt mutizone MQTT thermostat
*
* Copyright (C) 2019 Richard Genoud
*
*/
#ifndef BACKGROUNDLOOP_H
#define BACKGROUNDLOOP_H
#include <QThread>
#include "qmqtt.h"
class BackgroundLoop : public QThread
{
Q_OBJECT
public:
BackgroundLoop();
~BackgroundLoop();
bool connectMQTT();
private:
void run() override;
QMQTT::Client *m_mqtt_client;
private slots:
void mqtt_connected(void);
void mqtt_disconnected(void);
void mqtt_error(const QMQTT::ClientError error);
void mqtt_subscribed(const QString& topic, const quint8 qos);
void mqtt_received(const QMQTT::Message& message);
};
#endif // BACKGROUNDLOOP_H
/* vim: set tabstop=8 shiftwidth=8 softtabstop=0 noexpandtab: */

+ 6
- 1
soft/thermostat/inc/mainwindow.h View File

@ -13,13 +13,18 @@
#include <QPushButton>
#include <QtWidgets>
#include "backgroundloop.h"
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
explicit MainWindow(QWidget *parent = NULL);
~MainWindow();
private:
BackgroundLoop m_backLoop;
};
#endif // MAINWINDOW_H


+ 126
- 0
soft/thermostat/src/backgroundloop.cpp View File

@ -0,0 +1,126 @@
// SPDX-License-Identifier: GPL-3.0-or-later
/*
* Qt mutizone MQTT thermostat
*
* Copyright (C) 2019 Richard Genoud
*
*/
#include <QEventLoop>
#include <QDateTime>
#include <QElapsedTimer>
#include <QHostInfo>
#include <QHostAddress>
#include <QLoggingCategory>
#include "qmqtt.h"
#include "backgroundloop.h"
#define MQTT_SERVER "nas"
#define MQTT_PORT 1883
#define MQTT_USERNAME ""
#define MQTT_PASSWORD ""
#define MQTT_CLIENT_ID ""
#define LOOP_SLEEP_TIME_S 5
BackgroundLoop::BackgroundLoop() {
m_mqtt_client = NULL;
qsrand((uint)QDateTime::currentMSecsSinceEpoch());
}
BackgroundLoop::~BackgroundLoop() {
}
bool BackgroundLoop::connectMQTT()
{
QHostInfo info;
QHostAddress address;
bool result = false, ok;
if (m_mqtt_client) {
delete m_mqtt_client;
m_mqtt_client = NULL;
}
ok = address.setAddress(MQTT_SERVER);
if (!ok) {
/* This must be a domain name, not an IP */
info = QHostInfo::fromName(MQTT_SERVER);
if (info.error() != QHostInfo::NoError) {
qWarning() << QString("Error looking for address %1").arg(MQTT_SERVER)
<< info.errorString();
goto out;
}
if (info.addresses().isEmpty()) {
qWarning() << QString("No address returned for %1").arg(MQTT_SERVER);
goto out;
}
address = info.addresses().first();
}
m_mqtt_client = new QMQTT::Client(address, MQTT_PORT);
if (!m_mqtt_client) {
qWarning() << QString("Error allocating memory for MQTT client");
goto out;
}
if (QString(MQTT_CLIENT_ID).isEmpty()) {
m_mqtt_client->setClientId(QString("thermostat%1").arg(qrand()));
} else {
m_mqtt_client->setClientId(MQTT_CLIENT_ID);
}
connect(m_mqtt_client, SIGNAL(connected()), this, SLOT(mqtt_connected()));
connect(m_mqtt_client, SIGNAL(disconnected()), this, SLOT(mqtt_disconnected()));
connect(m_mqtt_client, SIGNAL(error(const QMQTT::ClientError)),
this, SLOT(mqtt_error(const QMQTT::ClientError)));
connect(m_mqtt_client, SIGNAL(subscribed(const QString&, const quint8)),
this, SLOT(mqtt_subscribed(const QString&, const quint8)));
connect(m_mqtt_client, SIGNAL(received(const QMQTT::Message&)),
this, SLOT(mqtt_received(const QMQTT::Message&)));
m_mqtt_client->setUsername(MQTT_USERNAME);
m_mqtt_client->setPassword(MQTT_PASSWORD);
m_mqtt_client->connectToHost();
result = true;
out:
return result;
}
void BackgroundLoop::mqtt_connected(void)
{
qDebug() << "MQTT connected !";
}
void BackgroundLoop::mqtt_disconnected(void)
{
qDebug() << "MQTT disconnected !";
}
void BackgroundLoop::mqtt_error(const QMQTT::ClientError error)
{
qDebug() << "MQTT error ! code: " << error;
}
void BackgroundLoop::mqtt_subscribed(const QString& topic, const quint8 qos)
{
qDebug() << "MQTT subscribed! topic: " << topic << " QoS: " << qos;
}
void BackgroundLoop::mqtt_received(const QMQTT::Message& message)
{
qDebug() << "MQTT recieved: " << QString::fromUtf8(message.payload());
}
void BackgroundLoop::run()
{
connectMQTT();
while(!isInterruptionRequested()) {
this->sleep(LOOP_SLEEP_TIME_S);
}
}
/* vim: set tabstop=8 shiftwidth=8 softtabstop=0 noexpandtab: */

+ 1
- 0
soft/thermostat/src/mainwindow.cpp View File

@ -39,6 +39,7 @@ MainWindow::MainWindow(QWidget *parent) :
setCentralWidget(mainWidget);
setWindowTitle(tr("Sorico's thermostat"));
m_backLoop.start();
}
MainWindow::~MainWindow()


+ 6
- 1
soft/thermostat/thermostat.pro View File

@ -1,4 +1,6 @@
QT += core gui
QT += core gui network
QT += qmqtt
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
@ -21,10 +23,12 @@ DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs depr
SOURCES += src/main.cpp \
src/mainwindow.cpp \
src/zoneitem.cpp \
src/backgroundloop.cpp \
HEADERS += inc/mainwindow.h \
inc/zoneitem.h \
inc/backgroundloop.h \
RESOURCES += thermostat.qrc
@ -40,6 +44,7 @@ INSTALLS += target
VERSION = $$system(./scripts/get_tag_version.sh)
# to see debug messages in console, run with environment variable QT_LOGGING_RULES="debug=true;"
QMAKE_CXXFLAGS += -Wall -Werror
QMAKE_CXXFLAGS += -std=c++11
QMAKE_CXXFLAGS += -DVERSION=\'\"$$VERSION\"\'


Loading…
Cancel
Save