Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions src/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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()
4 changes: 3 additions & 1 deletion src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand All @@ -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,
Expand Down