From e3aee07b46b1ad76b55051ffc821ba8f5bf231d3 Mon Sep 17 00:00:00 2001 From: deafloo Date: Sun, 16 Jan 2022 13:16:37 +0100 Subject: [PATCH] add DHT20 support --- octoprint_enclosure/DHT20.py | 43 +++++++++++++++++++ octoprint_enclosure/__init__.py | 24 +++++++++++ octoprint_enclosure/static/js/enclosure.js | 2 +- .../templates/enclosure_settings.jinja2 | 3 +- 4 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 octoprint_enclosure/DHT20.py diff --git a/octoprint_enclosure/DHT20.py b/octoprint_enclosure/DHT20.py new file mode 100644 index 0000000..9ba45ea --- /dev/null +++ b/octoprint_enclosure/DHT20.py @@ -0,0 +1,43 @@ +import time +import smbus +import sys + +class DHT20Error(Exception): + """ Bast class for exception """ + +if len(sys.argv) == 2 or len(sys.argv) == 3: + address = int(sys.argv[1],16) + if len(sys.argv) == 3: + busNum = int(sys.argv[2],16) + else: + busNum = 1 +else: + print('-1 | -1') + sys.exit(1) + + +sensor = smbus.SMBus(busNum) + +data = sensor.read_i2c_block_data(address,0x71,1) +if(data[0] | 0x08) == 0: + raise DHT20Error('Initialization error') + + +def getValue(bus): + bus.write_i2c_block_data(address,0xac,[0x33,0x00]) + data = bus.read_i2c_block_data(address,0x71,7) + Traw = ((data[3] & 0xf) << 16) + (data[4] << 8) + data[5] + Hraw = ((data[3] & 0xf0) << 4) + (data[1] << 12) + (data[2] << 4) + temp = 200*float(Traw)/2**20 - 50 + humi = 100*float(Hraw)/2**20 + return temp,humi + +def main(): + try: + temperature,humidity = getValue(sensor) + print('{0:0.1f} | {1:0.1f}'.format(temperature, humidity)) + except: + print('-1 | -1') + +if __name__ == "__main__": + main() diff --git a/octoprint_enclosure/__init__.py b/octoprint_enclosure/__init__.py index 441bc71..301e904 100644 --- a/octoprint_enclosure/__init__.py +++ b/octoprint_enclosure/__init__.py @@ -1002,6 +1002,9 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, octoprint.plugin.TemplateP 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'] == "20": + temp, hum = self.read_dht20_temp(sensor['temp_sensor_address'], sensor['temp_sensor_i2cbus']) + airquality = 0 elif sensor['temp_sensor_type'] == "18b20": temp = self.read_18b20_temp(sensor['ds18b20_serial']) hum = 0 @@ -1155,6 +1158,27 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, octoprint.plugin.TemplateP self.log_error(ex) return (0, 0) + def read_dht20_temp(self, address, i2cbus): + try: + script = os.path.dirname(os.path.realpath(__file__)) + "/DHT20.py " + if self._settings.get(["use_sudo"]): + sudo_str = "sudo " + else: + sudo_str = "" + cmd = sudo_str + "python " + script + str(address) + " " + str(i2cbus) + if self._settings.get(["debug_temperature_log"]) is True: + self._logger.debug("Temperature DHT20 cmd: %s", cmd) + stdout = (Popen(cmd, shell=True, stdout=PIPE).stdout).read() + if self._settings.get(["debug_temperature_log"]) is True: + self._logger.debug("DHT20 result: %s", stdout) + temp, hum = stdout.decode("utf-8").split("|") + return (self.to_float(temp.strip()), self.to_float(hum.strip())) + except Exception as ex: + self._logger.info( + "Failed to execute python scripts, try disabling use SUDO on advanced section of the plugin.") + self.log_error(ex) + return (0, 0) + def read_bme280_temp(self, address): try: script = os.path.dirname(os.path.realpath(__file__)) + "/BME280.py" diff --git a/octoprint_enclosure/static/js/enclosure.js b/octoprint_enclosure/static/js/enclosure.js index d2a0093..9bbb5db 100644 --- a/octoprint_enclosure/static/js/enclosure.js +++ b/octoprint_enclosure/static/js/enclosure.js @@ -56,7 +56,7 @@ $(function () { self.notifications = ko.observableArray([]); self.humidityCapableSensor = function(sensor){ - if (['11', '22', '2302', 'bme280', 'am2320', 'aht10' , 'si7021', 'hum_raw_i2c', 'temp_raw_i2c'].indexOf(sensor) >= 0){ + if (['11', '20', '22', '2302', 'bme280', 'am2320', 'aht10' , 'si7021', 'hum_raw_i2c', 'temp_raw_i2c'].indexOf(sensor) >= 0){ return true; } return false; diff --git a/octoprint_enclosure/templates/enclosure_settings.jinja2 b/octoprint_enclosure/templates/enclosure_settings.jinja2 index 394f8fc..adc7f7b 100644 --- a/octoprint_enclosure/templates/enclosure_settings.jinja2 +++ b/octoprint_enclosure/templates/enclosure_settings.jinja2 @@ -604,6 +604,7 @@