From 7b4b208c8579ede963f2c4ecd2543ca0900e2210 Mon Sep 17 00:00:00 2001 From: Mark Leary Date: Wed, 15 Dec 2021 10:43:19 -0500 Subject: [PATCH 1/5] Retry when DHT temp sensor read fails --- octoprint_enclosure/getDHTTemp.py | 34 +++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/octoprint_enclosure/getDHTTemp.py b/octoprint_enclosure/getDHTTemp.py index cb9bfef..e898977 100644 --- a/octoprint_enclosure/getDHTTemp.py +++ b/octoprint_enclosure/getDHTTemp.py @@ -1,9 +1,10 @@ import sys +import time import adafruit_dht # Parse command line parameters. -sensor_args = { +sensor_args = { '11': adafruit_dht.DHT11, '22': adafruit_dht.DHT22, '2302': adafruit_dht.DHT22 @@ -16,12 +17,29 @@ else: sys.exit(1) dht_dev = sensor(pin) -humidity = dht_dev.humidity -temperature = dht_dev.temperature -if humidity is not None and temperature is not None: - print('{0:0.1f} | {1:0.1f}'.format(temperature, humidity)) -else: - print('-1 | -1') +# DHT sensor read fails quite often, causing enclosure plugin to report value of 0. +# If this happens, retry as suggested in the adafruit_dht docs. +max_retries = 3 +retry_count = 0 +while retry_count <= max_retries: + try: + humidity = dht_dev.humidity + temperature = dht_dev.temperature -sys.exit(1) + if humidity is not None and temperature is not None: + print('{0:0.1f} | {1:0.1f}'.format(temperature, humidity)) + sys.exit(1) + except RuntimeError as e: + time.sleep(2) + retry_count += 1 + continue + except Exception as e: + dht_dev.exit() + raise e + + time.sleep(1) + retry_count += 1 + +print('-1 | -1') +sys.exit(1) \ No newline at end of file From e1f0372a63905f5f9de89b68c689aa5be6b209c5 Mon Sep 17 00:00:00 2001 From: Dracrius Date: Thu, 30 Dec 2021 14:26:34 -0500 Subject: [PATCH 2/5] Fixed DHT11,22,2302 Not Reading Many open issues are just because airquality was added but the if for DHT11,22,2302 was missing the "airquality = 0" it would need to read. --- octoprint_enclosure/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/octoprint_enclosure/__init__.py b/octoprint_enclosure/__init__.py index bfedb92..b580b96 100644 --- a/octoprint_enclosure/__init__.py +++ b/octoprint_enclosure/__init__.py @@ -982,6 +982,7 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, octoprint.plugin.TemplateP else: if sensor['temp_sensor_type'] in ["11", "22", "2302"]: temp, hum = self.read_dht_temp(sensor['temp_sensor_type'], sensor['gpio_pin']) + airquality = 0 elif sensor['temp_sensor_type'] == "18b20": temp = self.read_18b20_temp(sensor['ds18b20_serial']) hum = 0 From 43901015c95b9e5c4d13c4166c3ed4c1d312034e Mon Sep 17 00:00:00 2001 From: Dracrius Date: Fri, 31 Dec 2021 02:44:41 -0500 Subject: [PATCH 3/5] Added Basic MQTT Publishing Support using OctoPrint/OctoPrint-MQTT's Helper --- octoprint_enclosure/__init__.py | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/octoprint_enclosure/__init__.py b/octoprint_enclosure/__init__.py index b580b96..6472e1b 100644 --- a/octoprint_enclosure/__init__.py +++ b/octoprint_enclosure/__init__.py @@ -72,7 +72,17 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, octoprint.plugin.TemplateP development_mode = False dummy_value = 30.0 dummy_delta = 0.5 - + + def __init__(self): + # mqtt helpers + self.mqtt_publish = lambda *args, **kwargs: None + self.mqtt_subscribe = lambda *args, **kwargs: None + self.mqtt_unsubscribe = lambda *args, **kwargs: None + # hardcoded + self.mqtt_root_topic = "Monolith/temperature/enclosure" + self.mqtt_sensor_topic = self.mqtt_root_topic + "/" + "enclosure" + self.mqtt_message = "{\"temperature\": 0, \"humidity\": 0}" + def start_timer(self): """ Function to start timer that checks enclosure temperature @@ -134,7 +144,22 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, octoprint.plugin.TemplateP return -1 # ~~ StartupPlugin mixin - def on_after_startup(self): + def on_after_startup(self): + helpers = self._plugin_manager.get_helpers("mqtt", "mqtt_publish", "mqtt_subscribe", "mqtt_unsubscribe") + + if helpers: + if "mqtt_publish" in helpers: + self.mqtt_publish = helpers["mqtt_publish"] + if "mqtt_subscribe" in helpers: + self.mqtt_subscribe = helpers["mqtt_subscribe"] + if "mqtt_unsubscribe" in helpers: + self.mqtt_unsubscribe = helpers["mqtt_unsubscribe"] + else: + self._logger.info("mqtt helpers not found. mqtt functions won't work") + + # Test MQTT + #self.mqtt_publish(self.mqtt_sensor_topic, self.mqtt_message) + self.pwm_instances = [] self.event_queue = [] self.rpi_outputs_not_changed = [] @@ -820,6 +845,9 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, octoprint.plugin.TemplateP self.handle_temperature_events() self.handle_pwm_linked_temperature() self.update_ui() + self.mqtt_sensor_topic = self.mqtt_root_topic + "/" + sensor['label'] + self.mqtt_message = {"temperature": temp, "humidity": hum} + self.mqtt_publish(self.mqtt_sensor_topic, self.mqtt_message) except Exception as ex: self.log_error(ex) From b636429c0dde08b4f58b4d34dc06f51125b8447d Mon Sep 17 00:00:00 2001 From: Dracrius Date: Fri, 31 Dec 2021 02:53:40 -0500 Subject: [PATCH 4/5] Cleaned up unneeded MQTT code --- octoprint_enclosure/__init__.py | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/octoprint_enclosure/__init__.py b/octoprint_enclosure/__init__.py index 6472e1b..2bb8d7b 100644 --- a/octoprint_enclosure/__init__.py +++ b/octoprint_enclosure/__init__.py @@ -74,10 +74,8 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, octoprint.plugin.TemplateP dummy_delta = 0.5 def __init__(self): - # mqtt helpers + # mqtt helper self.mqtt_publish = lambda *args, **kwargs: None - self.mqtt_subscribe = lambda *args, **kwargs: None - self.mqtt_unsubscribe = lambda *args, **kwargs: None # hardcoded self.mqtt_root_topic = "Monolith/temperature/enclosure" self.mqtt_sensor_topic = self.mqtt_root_topic + "/" + "enclosure" @@ -150,15 +148,8 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, octoprint.plugin.TemplateP if helpers: if "mqtt_publish" in helpers: self.mqtt_publish = helpers["mqtt_publish"] - if "mqtt_subscribe" in helpers: - self.mqtt_subscribe = helpers["mqtt_subscribe"] - if "mqtt_unsubscribe" in helpers: - self.mqtt_unsubscribe = helpers["mqtt_unsubscribe"] else: - self._logger.info("mqtt helpers not found. mqtt functions won't work") - - # Test MQTT - #self.mqtt_publish(self.mqtt_sensor_topic, self.mqtt_message) + self._logger.info("mqtt helpers not found. mqtt functions won't work") self.pwm_instances = [] self.event_queue = [] From 599d7460aeabbdb433d9cebacb00e82ca022c2c6 Mon Sep 17 00:00:00 2001 From: Dracrius Date: Fri, 31 Dec 2021 02:58:41 -0500 Subject: [PATCH 5/5] Changed Root Topic to a more standard hardcoded one "octoprint/plugins/enclosure" may add config field later if I see the need. Those who really want to change it can do so here for now. --- octoprint_enclosure/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/octoprint_enclosure/__init__.py b/octoprint_enclosure/__init__.py index 2bb8d7b..f07ea9c 100644 --- a/octoprint_enclosure/__init__.py +++ b/octoprint_enclosure/__init__.py @@ -77,7 +77,7 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, octoprint.plugin.TemplateP # mqtt helper self.mqtt_publish = lambda *args, **kwargs: None # hardcoded - self.mqtt_root_topic = "Monolith/temperature/enclosure" + self.mqtt_root_topic = "octoprint/plugins/enclosure" self.mqtt_sensor_topic = self.mqtt_root_topic + "/" + "enclosure" self.mqtt_message = "{\"temperature\": 0, \"humidity\": 0}"