]> git.basschouten.com Git - mqttthermostat.git/commitdiff
Make the broker configurable.
authorBas Schouten <bas@basschouten.com>
Thu, 2 Feb 2023 13:15:27 +0000 (14:15 +0100)
committerBas Schouten <bas@basschouten.com>
Thu, 2 Feb 2023 13:15:27 +0000 (14:15 +0100)
MQTTThermostat/MQTTThermostat.cpp
MQTTThermostat/MQTTThermostat.h
MQTTThermostat/maindaemon.cpp
MQTTThermostat/maintest.cpp
mqttthermostat.conf

index 02f551e8f940d6a2250107e03fdfc9464fc614b7..836ebfd98ebdd78266dd52e475396baa3cff0308 100644 (file)
@@ -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)
index 7d0d7911185215337eb244f66db5371ec8c4c461..60de4481267ca8e763793c108baeb080646cc297 100644 (file)
@@ -40,6 +40,7 @@ public:
   void SetHeatingController(std::unique_ptr<HeatingController>&& 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
index 306dad664da18748742b24638f82c45c30b96cad..695e9d41fe624eece5a3a766b2e3fab58beab32f 100644 (file)
@@ -70,7 +70,7 @@ void read_config_file()
     exit(EXIT_FAILURE);
   }
 
-  string line, topic, clientid;
+  string line, topic, clientid, broker;
   unique_ptr<TemperatureSupplier> tempSupplier;
   unique_ptr<HeatingController> 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()
index af528bcf10eb95d071bee4e9c76ed0d24efc88bd..485ef40bfb9ec9c5c9e425c9e3c7f360a556fcd0 100644 (file)
@@ -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;
index 1a4e0008a00e73ffcd443675200020d24b490a0a..b854da87c98db058ed379f738b40f51c5b5eebbf 100644 (file)
@@ -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