From 79192595a040326178d6a20b2c9c4a866fda563f Mon Sep 17 00:00:00 2001 From: James Sutton <1068763+jpwsutton@users.noreply.github.com> Date: Mon, 4 Jan 2021 17:43:46 +0000 Subject: [PATCH 1/7] Adding MQTT functionality - first pass Signed-off-by: James Sutton <1068763+jpwsutton@users.noreply.github.com> --- examples/monitor.py | 146 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) diff --git a/examples/monitor.py b/examples/monitor.py index 03f9f5c..1a65ae5 100644 --- a/examples/monitor.py +++ b/examples/monitor.py @@ -18,6 +18,8 @@ from grow.moisture import Moisture from grow.pump import Pump +import paho.mqtt.client as mqtt + FPS = 10 @@ -887,6 +889,11 @@ def __init__(self): "alarm_interval", ] + self.mqtt_settings = [ + "server", + "topics" + ] + def load(self, settings_file="settings.yml"): if len(sys.argv) > 1: settings_file = sys.argv[1] @@ -921,6 +928,9 @@ def save(self, settings_file="settings.yml"): def get_channel(self, channel_id): return self.config.get("channel{}".format(channel_id), {}) + def get_mqtt(self): + return self.config.get("mqtt") + def set(self, section, settings): if isinstance(settings, dict): self.config[section].update(settings) @@ -940,6 +950,138 @@ def set_general(self, settings): self.set("general", settings) +class MqttController: + def __init__( + self, + channels, + enabled=False, + mqtt_host="", + mqtt_port=1883, + mqtt_tls=False, + mqtt_username=None, + mqtt_password=None, + mqtt_debug=False, + mqtt_keepalive=60, + mqtt_topic_root="plants/", + mqtt_qos=2, + interval_s=60 + ): + self.channels = channels + self.enabled = enabled + self.mqtt_host = mqtt_host + self.mqtt_port = mqtt_port + self.mqtt_tls = mqtt_tls + self.mqtt_username = mqtt_username + self.mqtt_password = mqtt_password + self.mqtt_debug = mqtt_debug + self.mqtt_keepalive = mqtt_keepalive + self.mqtt_topic_root = mqtt_topic_root + self.interval_s = interval_s + self.mqtt_qos = mqtt_qos + self._connected = False + self._time_last_pub = time.time() + + + + def update_from_yml(self, config): + if config is not None: + print(config) + self.enabled = True + self.mqtt_host = config.get("mqtt_host", self.mqtt_host) + self.mqtt_port = config.get("mqtt_port", self.mqtt_port) + self.mqtt_tls = config.get("mqtt_tls", self.mqtt_tls) + self.mqtt_username = config.get("mqtt_username", self.mqtt_username) + self.mqtt_password = config.get("mqtt_password", self.mqtt_password) + self.mqtt_debug = config.get("mqtt_debug", self.mqtt_debug) + self.mqtt_keepalive = config.get("mqtt_keepalive", self.mqtt_keepalive) + self.mqtt_topic_root = config.get("mqtt_topic_root", self.mqtt_topic_root) + self.mqtt_qos = config.get("mqtt_qos", self.mqtt_qos) + self.interval_s = config.get("interval_s", self.interval_s) + else: + self.enabled = False + + def on_connect(self, mqttc, obj, flags, rc): + logging.info(f'mqtt_connect: RC: {rc}') + + def on_message(self, mqttc, obj, msg): + logging.info(f'mqtt_message: Topic: {msg.topic}, QOS: {msg.qos}, Payload: {msg.payload}') + + def on_publish(self, mqttc, obj, mid): + logging.info(f'mqtt_publish: MID: {mid}') + + def on_subscribe(self, mqttc, obj, mid, granted_qos): + logging.info(f'mqtt_subscribe: MID: {mid}, Granted QoS: {granted_qos}') + + def on_log(self,mqttc, obj, level, string): + logging.info(f'mqtt_log: {string}') + + def connect(self): + self.mqttc = mqtt.Client() + #-- TODO - Add MQTT TLS Configuration + #if self.mqtt_tls: + #if usetls: + #if args.tls_version == "tlsv1.2": + #tlsVersion = ssl.PROTOCOL_TLSv1_2 + #elif args.tls_version == "tlsv1.1": + #tlsVersion = ssl.PROTOCOL_TLSv1_1 + #elif args.tls_version == "tlsv1": + #tlsVersion = ssl.PROTOCOL_TLSv1 + #elif args.tls_version is None: + #tlsVersion = None + #else: + #print ("Unknown TLS version - ignoring") + #tlsVersion = None + # + #if not args.insecure: + # cert_required = ssl.CERT_REQUIRED + #else: + # cert_required = ssl.CERT_NONE + # + #mqttc.tls_set(ca_certs=args.cacerts, certfile=None, keyfile=None, cert_reqs=cert_required, tls_version=tlsVersion) + # + #if args.insecure: + # mqttc.tls_insecure_set(True) + + if self.mqtt_username: or self.mqtt_password: + self.mqttc.username_pw_set(self.mqtt_username, self.mqtt_password) + + self.mqttc.on_message = self.on_message + self.mqttc.on_connect = self.on_connect + self.mqttc.on_publish = self.on_publish + self.mqttc.on_subscribe = self.on_subscribe + + if self.mqtt_debug: + self.mqttc.on_log = self.on_log + + logging.info(f'mqtt: Connecting to: {self.mqtt_host}:{self.mqtt_port}') + self.mqttc.connect(self.mqtt_host, self.mqtt_port, self.mqtt_keepalive) + self.mqttc.loop_start() + + def disconnect(self): + self.mqttc.disconnect() + + + def update(self): + if time.time() - self._time_last_pub > self.interval_s: + + for channel in channels: + topic = f'{self.mqtt_topic_root}channel{channel.channel}' + value = channel.sensor.saturation + logging.info(f'mqtt: Publishing: {value} to {topic} at QoS: {self.mqtt_qos}') + self.mqttc.publish(topic, value,qos=self.mqtt_qos)) + self._time_last_pub = time.time() + + + + + + + + + + + + def main(): def handle_button(pin): index = BUTTONS.index(pin) @@ -1055,6 +1197,9 @@ def handle_button(pin): ] ) + mqttController = MqttController(channels) + mqttController.update_from_yml(config.get_mqtt()) + while True: for channel in channels: config.set_channel(channel.channel, channel) @@ -1066,6 +1211,7 @@ def handle_button(pin): viewcontroller.update() viewcontroller.render() + mqttController.update() display.display(image.convert("RGB")) config.set_general( From 361264369036cfbca1e0fb3b76e4a3f9be1dde06 Mon Sep 17 00:00:00 2001 From: James Sutton <1068763+jpwsutton@users.noreply.github.com> Date: Mon, 4 Jan 2021 18:01:45 +0000 Subject: [PATCH 2/7] Fixing some silly mistakes: Signed-off-by: James Sutton <1068763+jpwsutton@users.noreply.github.com> --- examples/monitor.py | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/examples/monitor.py b/examples/monitor.py index 1a65ae5..f5edc22 100644 --- a/examples/monitor.py +++ b/examples/monitor.py @@ -1001,6 +1001,7 @@ def update_from_yml(self, config): self.enabled = False def on_connect(self, mqttc, obj, flags, rc): + self._connected = True logging.info(f'mqtt_connect: RC: {rc}') def on_message(self, mqttc, obj, msg): @@ -1042,7 +1043,7 @@ def connect(self): #if args.insecure: # mqttc.tls_insecure_set(True) - if self.mqtt_username: or self.mqtt_password: + if self.mqtt_username or self.mqtt_password: self.mqttc.username_pw_set(self.mqtt_username, self.mqtt_password) self.mqttc.on_message = self.on_message @@ -1062,23 +1063,18 @@ def disconnect(self): def update(self): + if not self._connected: + self.connect() + if time.time() - self._time_last_pub > self.interval_s: - for channel in channels: - topic = f'{self.mqtt_topic_root}channel{channel.channel}' - value = channel.sensor.saturation - logging.info(f'mqtt: Publishing: {value} to {topic} at QoS: {self.mqtt_qos}') - self.mqttc.publish(topic, value,qos=self.mqtt_qos)) + for channel in self.channels: + topic = f'{self.mqtt_topic_root}channel{channel.channel}' + value = channel.sensor.saturation + logging.info(f'mqtt: Publishing: {value} to {topic} at QoS: {self.mqtt_qos}') + self.mqttc.publish(topic, value,qos=self.mqtt_qos) self._time_last_pub = time.time() - - - - - - - - From a9e6d086f09647d104933755fc91db2f8fc98a3c Mon Sep 17 00:00:00 2001 From: James Sutton <1068763+jpwsutton@users.noreply.github.com> Date: Tue, 5 Jan 2021 10:01:47 +0000 Subject: [PATCH 3/7] Changing loop and connect logic slightly Signed-off-by: James Sutton <1068763+jpwsutton@users.noreply.github.com> --- examples/monitor.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/examples/monitor.py b/examples/monitor.py index f5edc22..b7a6856 100644 --- a/examples/monitor.py +++ b/examples/monitor.py @@ -979,6 +979,7 @@ def __init__( self.interval_s = interval_s self.mqtt_qos = mqtt_qos self._connected = False + self._connecting = False self._time_last_pub = time.time() @@ -1017,6 +1018,7 @@ def on_log(self,mqttc, obj, level, string): logging.info(f'mqtt_log: {string}') def connect(self): + self.connecting = True self.mqttc = mqtt.Client() #-- TODO - Add MQTT TLS Configuration #if self.mqtt_tls: @@ -1056,18 +1058,20 @@ def connect(self): logging.info(f'mqtt: Connecting to: {self.mqtt_host}:{self.mqtt_port}') self.mqttc.connect(self.mqtt_host, self.mqtt_port, self.mqtt_keepalive) - self.mqttc.loop_start() + #self.mqttc.loop_start() + self._connecting = False def disconnect(self): self.mqttc.disconnect() def update(self): - if not self._connected: + if not self._connected and not self._connecting: self.connect() - if time.time() - self._time_last_pub > self.interval_s: + self.mqttc.loop() + if time.time() - self._time_last_pub > self.interval_s: for channel in self.channels: topic = f'{self.mqtt_topic_root}channel{channel.channel}' value = channel.sensor.saturation @@ -1223,4 +1227,5 @@ def handle_button(pin): if __name__ == "__main__": + logging.basicConfig(level=logging.DEBUG) main() From 16f06a534e9dd65c3bb29d97407dbd4fb271309e Mon Sep 17 00:00:00 2001 From: James Sutton <1068763+jpwsutton@users.noreply.github.com> Date: Tue, 5 Jan 2021 10:05:27 +0000 Subject: [PATCH 4/7] Looking at connect RC Signed-off-by: James Sutton <1068763+jpwsutton@users.noreply.github.com> --- examples/monitor.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/examples/monitor.py b/examples/monitor.py index b7a6856..82b9987 100644 --- a/examples/monitor.py +++ b/examples/monitor.py @@ -1057,7 +1057,8 @@ def connect(self): self.mqttc.on_log = self.on_log logging.info(f'mqtt: Connecting to: {self.mqtt_host}:{self.mqtt_port}') - self.mqttc.connect(self.mqtt_host, self.mqtt_port, self.mqtt_keepalive) + rc = self.mqttc.connect(self.mqtt_host, self.mqtt_port, self.mqtt_keepalive) + logging.info("mqtt Connect RC: " + str(rc)) #self.mqttc.loop_start() self._connecting = False @@ -1067,7 +1068,7 @@ def disconnect(self): def update(self): if not self._connected and not self._connecting: - self.connect() + #self.connect() self.mqttc.loop() @@ -1199,6 +1200,7 @@ def handle_button(pin): mqttController = MqttController(channels) mqttController.update_from_yml(config.get_mqtt()) + mqttController.connect() while True: for channel in channels: From 22486279de8b1bec828553f255722d59fe024458 Mon Sep 17 00:00:00 2001 From: James Sutton <1068763+jpwsutton@users.noreply.github.com> Date: Tue, 5 Jan 2021 13:05:05 +0000 Subject: [PATCH 5/7] First working version with some tidy up Signed-off-by: James Sutton <1068763+jpwsutton@users.noreply.github.com> --- examples/monitor.py | 34 +++++++++++++++++----------------- examples/settings.yml | 9 +++++++++ 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/examples/monitor.py b/examples/monitor.py index 82b9987..0d2e884 100644 --- a/examples/monitor.py +++ b/examples/monitor.py @@ -17,7 +17,7 @@ from grow import Piezo from grow.moisture import Moisture from grow.pump import Pump - +import json import paho.mqtt.client as mqtt @@ -962,7 +962,7 @@ def __init__( mqtt_password=None, mqtt_debug=False, mqtt_keepalive=60, - mqtt_topic_root="plants/", + mqtt_topic_root="plants/moisture", mqtt_qos=2, interval_s=60 ): @@ -986,7 +986,6 @@ def __init__( def update_from_yml(self, config): if config is not None: - print(config) self.enabled = True self.mqtt_host = config.get("mqtt_host", self.mqtt_host) self.mqtt_port = config.get("mqtt_port", self.mqtt_port) @@ -1003,19 +1002,19 @@ def update_from_yml(self, config): def on_connect(self, mqttc, obj, flags, rc): self._connected = True - logging.info(f'mqtt_connect: RC: {rc}') + logging.debug(f'mqtt_connect: RC: {rc}') def on_message(self, mqttc, obj, msg): - logging.info(f'mqtt_message: Topic: {msg.topic}, QOS: {msg.qos}, Payload: {msg.payload}') + logging.debug(f'mqtt_message: Topic: {msg.topic}, QOS: {msg.qos}, Payload: {msg.payload}') def on_publish(self, mqttc, obj, mid): - logging.info(f'mqtt_publish: MID: {mid}') + logging.debug(f'mqtt_publish: MID: {mid}') def on_subscribe(self, mqttc, obj, mid, granted_qos): - logging.info(f'mqtt_subscribe: MID: {mid}, Granted QoS: {granted_qos}') + logging.debug(f'mqtt_subscribe: MID: {mid}, Granted QoS: {granted_qos}') def on_log(self,mqttc, obj, level, string): - logging.info(f'mqtt_log: {string}') + logging.debug(f'mqtt_log: {string}') def connect(self): self.connecting = True @@ -1056,9 +1055,9 @@ def connect(self): if self.mqtt_debug: self.mqttc.on_log = self.on_log - logging.info(f'mqtt: Connecting to: {self.mqtt_host}:{self.mqtt_port}') + logging.debug(f'mqtt: Connecting to: {self.mqtt_host}:{self.mqtt_port}') rc = self.mqttc.connect(self.mqtt_host, self.mqtt_port, self.mqtt_keepalive) - logging.info("mqtt Connect RC: " + str(rc)) + logging.debug("mqtt Connect RC: " + str(rc)) #self.mqttc.loop_start() self._connecting = False @@ -1067,17 +1066,19 @@ def disconnect(self): def update(self): - if not self._connected and not self._connecting: - #self.connect() + self.mqttc.loop() if time.time() - self._time_last_pub > self.interval_s: + moistureDict = {} for channel in self.channels: - topic = f'{self.mqtt_topic_root}channel{channel.channel}' - value = channel.sensor.saturation - logging.info(f'mqtt: Publishing: {value} to {topic} at QoS: {self.mqtt_qos}') - self.mqttc.publish(topic, value,qos=self.mqtt_qos) + moistureDict[f'channel{channel.channel}'] = channel.sensor.saturation * 100 + + value = json.dumps(moistureDict) + logging.info(f'mqtt: Publishing: {value} to {self.mqtt_topic_root} at QoS: {self.mqtt_qos}') + self.mqttc.publish(self.mqtt_topic_root, value,qos=self.mqtt_qos) + self._time_last_pub = time.time() @@ -1229,5 +1230,4 @@ def handle_button(pin): if __name__ == "__main__": - logging.basicConfig(level=logging.DEBUG) main() diff --git a/examples/settings.yml b/examples/settings.yml index 88871c1..d8b5bbf 100644 --- a/examples/settings.yml +++ b/examples/settings.yml @@ -16,3 +16,12 @@ channel3: general: alarm_enable: true alarm_interval: 2 +mqtt: + mqtt_host: localhost + mqtt_port: 1883 + mqtt_username: plantpi + mqtt_password: pimoroni + mqtt_debug: false + mqtt_topic_root: plants/moisture + interval_s : 30 + From 0c4e2f72615c7e331f5c29f1ab80d6a8e674ccdd Mon Sep 17 00:00:00 2001 From: James Sutton <1068763+jpwsutton@users.noreply.github.com> Date: Wed, 6 Jan 2021 08:36:13 +0000 Subject: [PATCH 6/7] Updating README and adding client ID Signed-off-by: James Sutton <1068763+jpwsutton@users.noreply.github.com> --- examples/README.md | 27 +++++++++ examples/monitor.py | 126 +++++++++++++++++++++--------------------- examples/settings.yml | 2 +- 3 files changed, 92 insertions(+), 63 deletions(-) diff --git a/examples/README.md b/examples/README.md index e7d12c9..0aaf26a 100644 --- a/examples/README.md +++ b/examples/README.md @@ -80,3 +80,30 @@ An additional `general` section can be used for global settings: * `alarm_enable` - Whether to enable the alarm * `alarm_interval` - The interval at which the alarm should beep (in seconds) + + +## MQTT Settings +If you want to send the moisture saturation levels somewhere to build a dashboard, or log to a database, MQTT is a great way to do this. You'll need an MQTT broker set up somwhere, [Mosquitto](https://mosquitto.org/) is great choice and runs really well on a Raspberry Pi. + +Add the following settings to your `settings.yml` file: + +```yaml +mqtt: + mqtt_host: localhost + mqtt_port: 1883 + mqtt_client_id: plantMonitor + mqtt_username: plantpi + mqtt_password: pimoroni + mqtt_topic_root: plants/moisture + mqtt_qos: 2 + mqtt_interval : 30 +``` + +* `mqtt_host` - This is the hostname or IP address of the server where your MQTT broker is. +* `mqtt_port` - This is the port number for the broker, usually `1883`. +* `mqtt_client_id` - This is the Client ID for your monitor service, can be ignored if you dont mind what the broker sees this device as. +* `mqtt_username` - If you have authentication enabled, this is the username. +* `mqtt_password` - If you have authentication enabled, this is the password. +* `mqtt_topic_root` - This is the MQTT topic where the sensor data will be sent. +* `mqtt_qos` - The QoS Level to publish messages at. Default: `2`. +* `mqtt_interval` - This is the time interval between publishing readings, in seconds. \ No newline at end of file diff --git a/examples/monitor.py b/examples/monitor.py index 0d2e884..df51007 100644 --- a/examples/monitor.py +++ b/examples/monitor.py @@ -958,25 +958,27 @@ def __init__( mqtt_host="", mqtt_port=1883, mqtt_tls=False, + mqtt_client_id="", mqtt_username=None, mqtt_password=None, mqtt_debug=False, mqtt_keepalive=60, mqtt_topic_root="plants/moisture", mqtt_qos=2, - interval_s=60 + mqtt_interval=60 ): self.channels = channels - self.enabled = enabled + self._enabled = enabled self.mqtt_host = mqtt_host self.mqtt_port = mqtt_port self.mqtt_tls = mqtt_tls + self.mqtt_client_id = mqtt_client_id self.mqtt_username = mqtt_username self.mqtt_password = mqtt_password self.mqtt_debug = mqtt_debug self.mqtt_keepalive = mqtt_keepalive self.mqtt_topic_root = mqtt_topic_root - self.interval_s = interval_s + self.mqtt_interval = mqtt_interval self.mqtt_qos = mqtt_qos self._connected = False self._connecting = False @@ -986,19 +988,20 @@ def __init__( def update_from_yml(self, config): if config is not None: - self.enabled = True + self._enabled = True self.mqtt_host = config.get("mqtt_host", self.mqtt_host) self.mqtt_port = config.get("mqtt_port", self.mqtt_port) self.mqtt_tls = config.get("mqtt_tls", self.mqtt_tls) + self.mqtt_client_id = config.get("mqtt_client_id", self.mqtt_client_id) self.mqtt_username = config.get("mqtt_username", self.mqtt_username) self.mqtt_password = config.get("mqtt_password", self.mqtt_password) self.mqtt_debug = config.get("mqtt_debug", self.mqtt_debug) self.mqtt_keepalive = config.get("mqtt_keepalive", self.mqtt_keepalive) self.mqtt_topic_root = config.get("mqtt_topic_root", self.mqtt_topic_root) self.mqtt_qos = config.get("mqtt_qos", self.mqtt_qos) - self.interval_s = config.get("interval_s", self.interval_s) + self.mqtt_interval = config.get("mqtt_interval", self.mqtt_interval) else: - self.enabled = False + self._enabled = False def on_connect(self, mqttc, obj, flags, rc): self._connected = True @@ -1017,69 +1020,68 @@ def on_log(self,mqttc, obj, level, string): logging.debug(f'mqtt_log: {string}') def connect(self): - self.connecting = True - self.mqttc = mqtt.Client() - #-- TODO - Add MQTT TLS Configuration - #if self.mqtt_tls: - #if usetls: - #if args.tls_version == "tlsv1.2": - #tlsVersion = ssl.PROTOCOL_TLSv1_2 - #elif args.tls_version == "tlsv1.1": - #tlsVersion = ssl.PROTOCOL_TLSv1_1 - #elif args.tls_version == "tlsv1": - #tlsVersion = ssl.PROTOCOL_TLSv1 - #elif args.tls_version is None: - #tlsVersion = None - #else: - #print ("Unknown TLS version - ignoring") - #tlsVersion = None - # - #if not args.insecure: - # cert_required = ssl.CERT_REQUIRED - #else: - # cert_required = ssl.CERT_NONE - # - #mqttc.tls_set(ca_certs=args.cacerts, certfile=None, keyfile=None, cert_reqs=cert_required, tls_version=tlsVersion) - # - #if args.insecure: - # mqttc.tls_insecure_set(True) - - if self.mqtt_username or self.mqtt_password: - self.mqttc.username_pw_set(self.mqtt_username, self.mqtt_password) - - self.mqttc.on_message = self.on_message - self.mqttc.on_connect = self.on_connect - self.mqttc.on_publish = self.on_publish - self.mqttc.on_subscribe = self.on_subscribe - - if self.mqtt_debug: - self.mqttc.on_log = self.on_log - - logging.debug(f'mqtt: Connecting to: {self.mqtt_host}:{self.mqtt_port}') - rc = self.mqttc.connect(self.mqtt_host, self.mqtt_port, self.mqtt_keepalive) - logging.debug("mqtt Connect RC: " + str(rc)) - #self.mqttc.loop_start() - self._connecting = False + if self._enabled: + self.connecting = True + self.mqttc = mqtt.Client() + #-- TODO - Add MQTT TLS Configuration + #if self.mqtt_tls: + #if usetls: + #if args.tls_version == "tlsv1.2": + #tlsVersion = ssl.PROTOCOL_TLSv1_2 + #elif args.tls_version == "tlsv1.1": + #tlsVersion = ssl.PROTOCOL_TLSv1_1 + #elif args.tls_version == "tlsv1": + #tlsVersion = ssl.PROTOCOL_TLSv1 + #elif args.tls_version is None: + #tlsVersion = None + #else: + #print ("Unknown TLS version - ignoring") + #tlsVersion = None + # + #if not args.insecure: + # cert_required = ssl.CERT_REQUIRED + #else: + # cert_required = ssl.CERT_NONE + # + #mqttc.tls_set(ca_certs=args.cacerts, certfile=None, keyfile=None, cert_reqs=cert_required, tls_version=tlsVersion) + # + #if args.insecure: + # mqttc.tls_insecure_set(True) + + if self.mqtt_username or self.mqtt_password: + self.mqttc.username_pw_set(self.mqtt_username, self.mqtt_password) + + self.mqttc.on_message = self.on_message + self.mqttc.on_connect = self.on_connect + self.mqttc.on_publish = self.on_publish + self.mqttc.on_subscribe = self.on_subscribe + + if self.mqtt_debug: + self.mqttc.on_log = self.on_log + + logging.debug(f'mqtt: Connecting to: {self.mqtt_host}:{self.mqtt_port}') + rc = self.mqttc.connect(self.mqtt_host, self.mqtt_port, self.mqtt_keepalive) + logging.debug("mqtt Connect RC: " + str(rc)) + #self.mqttc.loop_start() + self._connecting = False def disconnect(self): self.mqttc.disconnect() def update(self): - - - self.mqttc.loop() - - if time.time() - self._time_last_pub > self.interval_s: - moistureDict = {} - for channel in self.channels: - moistureDict[f'channel{channel.channel}'] = channel.sensor.saturation * 100 - - value = json.dumps(moistureDict) - logging.info(f'mqtt: Publishing: {value} to {self.mqtt_topic_root} at QoS: {self.mqtt_qos}') - self.mqttc.publish(self.mqtt_topic_root, value,qos=self.mqtt_qos) + if self._enabled: + self.mqttc.loop() + if time.time() - self._time_last_pub > self.interval_s: + moistureDict = {} + for channel in self.channels: + moistureDict[f'channel{channel.channel}'] = channel.sensor.saturation * 100 - self._time_last_pub = time.time() + value = json.dumps(moistureDict) + logging.info(f'mqtt: Publishing: {value} to {self.mqtt_topic_root} at QoS: {self.mqtt_qos}') + self.mqttc.publish(self.mqtt_topic_root, value,qos=self.mqtt_qos) + + self._time_last_pub = time.time() diff --git a/examples/settings.yml b/examples/settings.yml index d8b5bbf..599ca7f 100644 --- a/examples/settings.yml +++ b/examples/settings.yml @@ -23,5 +23,5 @@ mqtt: mqtt_password: pimoroni mqtt_debug: false mqtt_topic_root: plants/moisture - interval_s : 30 + mqtt_interval : 30 From 65e208a0841b25a5e30ba5e0710af6b5b753e873 Mon Sep 17 00:00:00 2001 From: James Sutton <1068763+jpwsutton@users.noreply.github.com> Date: Wed, 6 Jan 2021 08:38:43 +0000 Subject: [PATCH 7/7] Updating broken mqtt_interval / interval_s Signed-off-by: James Sutton <1068763+jpwsutton@users.noreply.github.com> --- examples/monitor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/monitor.py b/examples/monitor.py index df51007..5c32854 100644 --- a/examples/monitor.py +++ b/examples/monitor.py @@ -1072,7 +1072,7 @@ def disconnect(self): def update(self): if self._enabled: self.mqttc.loop() - if time.time() - self._time_last_pub > self.interval_s: + if time.time() - self._time_last_pub > self.mqtt_interval: moistureDict = {} for channel in self.channels: moistureDict[f'channel{channel.channel}'] = channel.sensor.saturation * 100