diff --git a/README.md b/README.md index f2aee54..c01a349 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,11 @@ sudo git clone https://github.com/hotplot/enviroplus-mqtt /usr/src/enviroplus-mqtt -5) Add a new file at `/etc/systemd/system/envlogger.service` with the following content: +5) Install [Paho MQTT tools](https://www.eclipse.org/paho/): + + pip3 install paho-mqtt + +6) Add a new file at `/etc/systemd/system/envlogger.service` with the following content: [Unit] Description=Enviro+ MQTT Logger @@ -62,6 +66,10 @@ stabilise before starting to publish readings --use-pms5003 if set, PM readings will be taken from the PMS5003 sensor + + -r, --retain tell MQTT broker to retain the last message + + --help print this help message and exit ## Published Topics diff --git a/src/logger.py b/src/logger.py index adf5ba5..42183bf 100644 --- a/src/logger.py +++ b/src/logger.py @@ -15,7 +15,7 @@ class EnvLogger: - def __init__(self, client_id, host, port, username, password, prefix, use_pms5003, num_samples): + def __init__(self, client_id, host, port, username, password, prefix, use_pms5003, num_samples, retain): self.bme280 = BME280() self.prefix = prefix @@ -33,6 +33,7 @@ def __init__(self, client_id, host, port, username, password, prefix, use_pms500 self.pm_thread = threading.Thread(target=self.__read_pms_continuously) self.pm_thread.daemon = True self.pm_thread.start() + self.retain = retain def __on_connect(self, client, userdata, flags, rc): @@ -89,9 +90,9 @@ def take_readings(self): return readings - def publish(self, topic, value): + def publish(self, topic, value, retain): topic = self.prefix.strip("/") + "/" + topic - self.client.publish(topic, str(value)) + self.client.publish(topic, str(value), retain=retain) def update(self, publish_readings=True): @@ -101,6 +102,6 @@ def update(self, publish_readings=True): for topic in self.samples[0].keys(): value_sum = sum([d[topic] for d in self.samples]) value_avg = value_sum / len(self.samples) - self.publish(topic, value_avg) + self.publish(topic, value_avg, retain=self.retain) self.client.loop() diff --git a/src/main.py b/src/main.py index 0f92baf..4a25f60 100644 --- a/src/main.py +++ b/src/main.py @@ -14,6 +14,7 @@ def parse_args(): ap.add_argument("--interval", type=int, default=5, help="the duration in seconds between updates") ap.add_argument("--delay", type=int, default=15, help="the duration in seconds to allow the sensors to stabilise before starting to publish readings") ap.add_argument("--use-pms5003", action="store_true", help="if set, PM readings will be taken from the PMS5003 sensor") + ap.add_argument("-r", "--retain", action='store_true', help="tell MQTT broker to retain the last message") ap.add_argument("--help", action="help", help="print this help message and exit") return vars(ap.parse_args()) @@ -30,7 +31,8 @@ def main(): password=args["password"], prefix=args["prefix"], use_pms5003=args["use_pms5003"], - num_samples=args["interval"] + num_samples=args["interval"], + retain=args["retain"], ) # Take readings without publishing them for the specified delay period,