From b2472b4c49aad1362a83a2e6789c891358b854d1 Mon Sep 17 00:00:00 2001 From: Sean Danischevsky Date: Sat, 16 May 2020 18:46:01 +0100 Subject: [PATCH 1/4] added -r/--retain flag --- src/logger.py | 9 +++++---- src/main.py | 4 +++- 2 files changed, 8 insertions(+), 5 deletions(-) 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, From 1d743e0db66b59a5b077bdf00a836b876317401d Mon Sep 17 00:00:00 2001 From: Sean Danischevsky <32126021+seaniedan@users.noreply.github.com> Date: Sat, 16 May 2020 20:08:22 +0100 Subject: [PATCH 2/4] Update README.md added ---retain option to docs --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index f2aee54..25b63dd 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,8 @@ 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 From 9af070b34b8b9bb794cb054d41d7400c3930e80e Mon Sep 17 00:00:00 2001 From: Sean Danischevsky <32126021+seaniedan@users.noreply.github.com> Date: Sat, 16 May 2020 20:11:25 +0100 Subject: [PATCH 3/4] Update README.md added retain documentation --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index f2aee54..7c15537 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,7 @@ 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 From f4e877b4301fc026c73f96cc85993f62d9143bba Mon Sep 17 00:00:00 2001 From: Sean Danischevsky <32126021+seaniedan@users.noreply.github.com> Date: Mon, 8 Jun 2020 06:58:04 +0100 Subject: [PATCH 4/4] Update README.md added paho-mqtt dependency --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c244ec8..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