From 9d7f776bfe0713e708b758ce9bc3aa60c5a0c60d Mon Sep 17 00:00:00 2001 From: Bas Schouten Date: Thu, 2 Feb 2023 14:15:27 +0100 Subject: [PATCH] Make the broker configurable. --- MQTTThermostat/MQTTThermostat.cpp | 4 +--- MQTTThermostat/MQTTThermostat.h | 2 ++ MQTTThermostat/maindaemon.cpp | 6 +++++- MQTTThermostat/maintest.cpp | 1 + mqttthermostat.conf | 3 ++- 5 files changed, 11 insertions(+), 5 deletions(-) diff --git a/MQTTThermostat/MQTTThermostat.cpp b/MQTTThermostat/MQTTThermostat.cpp index 02f551e..836ebfd 100644 --- a/MQTTThermostat/MQTTThermostat.cpp +++ b/MQTTThermostat/MQTTThermostat.cpp @@ -11,8 +11,6 @@ using namespace std; -#define ADDRESS "tcp://10.0.1.213:1883" - #define QOS 1 #define TIMEOUT 10000L @@ -55,7 +53,7 @@ void MQTTThermostat::Start() MQTTClient_message pubmsg = MQTTClient_message_initializer; MQTTClient_deliveryToken token; int rc; - MQTTClient_create(&mClient, ADDRESS, mClientID.c_str(), + MQTTClient_create(&mClient, mBroker.c_str(), mClientID.c_str(), MQTTCLIENT_PERSISTENCE_NONE, NULL); if ((rc = MQTTClient_setCallbacks(mClient, this, connlost, msgarrvd, delivered)) != MQTTCLIENT_SUCCESS) diff --git a/MQTTThermostat/MQTTThermostat.h b/MQTTThermostat/MQTTThermostat.h index 7d0d791..60de448 100644 --- a/MQTTThermostat/MQTTThermostat.h +++ b/MQTTThermostat/MQTTThermostat.h @@ -40,6 +40,7 @@ public: void SetHeatingController(std::unique_ptr&& aController) { mHeatingController = std::move(aController); } void SetMeasurementInterval(std::chrono::milliseconds aMS) { mMeasurementInterval = aMS; } void SetTopic(const std::string& aTopic) { mTopic = aTopic; } + void SetBroker(const std::string& aBroker) { mBroker = aBroker; } void SetSetpoint(float aTemperature); @@ -73,4 +74,5 @@ private: std::condition_variable mSetpointUpdateCV; std::chrono::milliseconds mMeasurementInterval; std::string mTopic; + std::string mBroker; }; \ No newline at end of file diff --git a/MQTTThermostat/maindaemon.cpp b/MQTTThermostat/maindaemon.cpp index 306dad6..695e9d4 100644 --- a/MQTTThermostat/maindaemon.cpp +++ b/MQTTThermostat/maindaemon.cpp @@ -70,7 +70,7 @@ void read_config_file() exit(EXIT_FAILURE); } - string line, topic, clientid; + string line, topic, clientid, broker; unique_ptr tempSupplier; unique_ptr heatingController; uint32_t measurementInterval; @@ -121,6 +121,9 @@ void read_config_file() else if (key == "clientid") { clientid = value; } + else if (key == "broker") { + broker = value; + } else if (key == "gpio heating pin") { gpioHeatingPin = stoi(value); } @@ -142,6 +145,7 @@ void read_config_file() MQTTThermostat::instance().SetMeasurementInterval(chrono::milliseconds{ measurementInterval }); MQTTThermostat::instance().SetTopic(topic); MQTTThermostat::instance().SetClientID(clientid); + MQTTThermostat::instance().SetBroker(broker); } void daemonize() diff --git a/MQTTThermostat/maintest.cpp b/MQTTThermostat/maintest.cpp index af528bc..485ef40 100644 --- a/MQTTThermostat/maintest.cpp +++ b/MQTTThermostat/maintest.cpp @@ -17,6 +17,7 @@ int main() MQTTThermostat::instance().SetMeasurementInterval(2000ms); MQTTThermostat::instance().SetTopic("bedroom-staging/thermostat/"); MQTTThermostat::instance().SetClientID("bedroom-staging"); + MQTTThermostat::instance().SetBroker("10.0.1.225"); MQTTThermostat::instance().Start(); return 0; diff --git a/mqttthermostat.conf b/mqttthermostat.conf index 1a4e000..b854da8 100644 --- a/mqttthermostat.conf +++ b/mqttthermostat.conf @@ -2,4 +2,5 @@ temperature supplier=dummy heating controller=dummy measurement interval=2000 topic=staging/thermostat/ -clientid=staging \ No newline at end of file +clientid=staging +broker=127.0.0.1 \ No newline at end of file -- 2.47.3