From 65c015243733dd09e1fb588fc8f43cb9283cc2cf Mon Sep 17 00:00:00 2001 From: Mitch Gallman Date: Mon, 3 May 2021 23:17:22 -0400 Subject: [PATCH 1/4] Add hum_raw_i2c & temp_raw_i2c as humidity capable --- octoprint_enclosure/static/js/enclosure.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/octoprint_enclosure/static/js/enclosure.js b/octoprint_enclosure/static/js/enclosure.js index 048b45e..7dab1b4 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', 'si7021'].indexOf(sensor) >= 0){ + if (['11', '22', '2302', 'bme280', 'am2320', 'si7021', 'hum_raw_i2c', 'temp_raw_i2c'].indexOf(sensor) >= 0){ return true; } return false; -- 2.39.5 From 04d57cc6d7691de938e4b971d0109ab8f8a0a01c Mon Sep 17 00:00:00 2001 From: Mitch Gallman Date: Mon, 3 May 2021 23:22:52 -0400 Subject: [PATCH 2/4] Change read_raw_i2c_temp to return both temp & hum Change read_raw_i2c_temp to return both temperature and humidity. Slave code concatenates the two float values into a byte array. --- octoprint_enclosure/__init__.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/octoprint_enclosure/__init__.py b/octoprint_enclosure/__init__.py index 8116e81..969d6a2 100644 --- a/octoprint_enclosure/__init__.py +++ b/octoprint_enclosure/__init__.py @@ -993,11 +993,9 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, octoprint.plugin.TemplateP temp = self.read_mcp_temp(sensor['temp_sensor_address']) hum = 0 elif sensor['temp_sensor_type'] == "temp_raw_i2c": - temp = self.read_raw_i2c_temp(sensor) - hum = 0 + temp, hum = self.read_raw_i2c_temp(sensor) elif sensor['temp_sensor_type'] == "hum_raw_i2c": - temp = 0 - hum = self.read_raw_i2c_temp(sensor) + hum, temp = self.read_raw_i2c_temp(sensor) else: self._logger.info("temp_sensor_type no match") temp = None @@ -1053,14 +1051,16 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, octoprint.plugin.TemplateP i2creg = self.to_int(sensor['temp_i2c_register']) with SMBus(i2cbus) as bus: - data = bus.read_i2c_block_data(i2caddr, i2creg, 4) - val = struct.unpack('f', bytearray(data)) - fval = val[0] + data = bus.read_i2c_block_data(i2caddr, i2creg, 8) + val = struct.unpack('f', bytearray(data[0:4])) + fval1 = val[0] + val = struct.unpack('f', bytearray(data[4:8])) + fval2 = val[0] - self._logger.debug("read_raw_i2c_temp(i2cbus=%s, i2caddr=%s, i2creg=%s) data == %s (%s)", - i2cbus, i2caddr, i2creg, data, fval) + self._logger.debug("read_raw_i2c_temp(i2cbus=%s, i2caddr=%s, i2creg=%s) data == %s (%s, %s)", + i2cbus, i2caddr, i2creg, data, fval1, fval2) - return str(fval) + return (fval1, fval2) except Exception as ex: template = "An exception of type {0} occurred on {1} when reading on i2c address {2}, reg {3}. Arguments:\n{4!r}" -- 2.39.5 From 0c88c333a1fe2350cabaa5edcc401727fb52db1e Mon Sep 17 00:00:00 2001 From: Mitch Gallman Date: Tue, 4 May 2021 20:11:04 -0400 Subject: [PATCH 3/4] Handle nan in read_raw_i2c_temp --- octoprint_enclosure/__init__.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/octoprint_enclosure/__init__.py b/octoprint_enclosure/__init__.py index 969d6a2..100908d 100644 --- a/octoprint_enclosure/__init__.py +++ b/octoprint_enclosure/__init__.py @@ -1054,9 +1054,13 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, octoprint.plugin.TemplateP data = bus.read_i2c_block_data(i2caddr, i2creg, 8) val = struct.unpack('f', bytearray(data[0:4])) fval1 = val[0] + if fval1 != fval1: + fval1 = 0 val = struct.unpack('f', bytearray(data[4:8])) fval2 = val[0] - + if fval2 != fval2: + fval2 = 0 + self._logger.debug("read_raw_i2c_temp(i2cbus=%s, i2caddr=%s, i2creg=%s) data == %s (%s, %s)", i2cbus, i2caddr, i2creg, data, fval1, fval2) -- 2.39.5 From f90347105bc62788584419c95f1f080a6d675085 Mon Sep 17 00:00:00 2001 From: Mitch Gallman Date: Tue, 4 May 2021 20:38:15 -0400 Subject: [PATCH 4/4] Update __init__.py --- octoprint_enclosure/__init__.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/octoprint_enclosure/__init__.py b/octoprint_enclosure/__init__.py index 100908d..81fa589 100644 --- a/octoprint_enclosure/__init__.py +++ b/octoprint_enclosure/__init__.py @@ -1052,12 +1052,10 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, octoprint.plugin.TemplateP with SMBus(i2cbus) as bus: data = bus.read_i2c_block_data(i2caddr, i2creg, 8) - val = struct.unpack('f', bytearray(data[0:4])) - fval1 = val[0] + fval1 = struct.unpack('f', bytearray(data[0:4]))[0] if fval1 != fval1: fval1 = 0 - val = struct.unpack('f', bytearray(data[4:8])) - fval2 = val[0] + fval2 = struct.unpack('f', bytearray(data[4:8]))[0] if fval2 != fval2: fval2 = 0 -- 2.39.5