From 01e727cb94fc104e3b96e4f27a47aee917ec57fb Mon Sep 17 00:00:00 2001 From: munzli Date: Sun, 29 Sep 2019 19:00:31 +0200 Subject: [PATCH] configurable sensor address for mcp9808 --- octoprint_enclosure/__init__.py | 6 +++--- octoprint_enclosure/mcp9808.py | 18 ++++++++++++------ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/octoprint_enclosure/__init__.py b/octoprint_enclosure/__init__.py index 54b16c0..51165b7 100644 --- a/octoprint_enclosure/__init__.py +++ b/octoprint_enclosure/__init__.py @@ -513,7 +513,7 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, octoprint.plugin.TemplateP temp = self.read_max31855_temp(sensor['temp_sensor_address']) hum = 0 elif sensor['temp_sensor_type'] == "mcp9808": - temp = self.read_mcp_temp() + temp = self.read_mcp_temp(sensor['temp_sensor_address']) hum = 0 else: self._logger.info("temp_sensor_type no match") @@ -559,10 +559,10 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, octoprint.plugin.TemplateP return return_value, return_value - def read_mcp_temp(self): + def read_mcp_temp(self, address): try: script = os.path.dirname(os.path.realpath(__file__)) + "/mcp9808.py" - args = ["python", script] + args = ["python", script, str(address)] if self._settings.get(["debug_temperature_log"]) is True: self._logger.debug("Temperature MCP9808 cmd: %s", " ".join(args)) proc = Popen(args, stdout=PIPE) diff --git a/octoprint_enclosure/mcp9808.py b/octoprint_enclosure/mcp9808.py index d324b47..672045b 100644 --- a/octoprint_enclosure/mcp9808.py +++ b/octoprint_enclosure/mcp9808.py @@ -1,3 +1,4 @@ +import sys import smbus # default I2C address for device. @@ -27,24 +28,29 @@ MCP9808_REG_CONFIG_ALERTMODE = 0x0001 def main(): + # get bus address if provided or use default address + address = MCP9808_I2CADDR_DEFAULT + if len(sys.argv) == 2: + address = int(sys.argv[1], 16) + # get I2C bus bus = smbus.SMBus(1) - # MCP9808 address, 0x18(24) + # MCP9808 address, default 0x18(24) # configuration register, 0x01(1) # continuous conversion mode, power-up default config = [MCP9808_REG_CONFIG_CONTCONV, 0x00] - bus.write_i2c_block_data(MCP9808_I2CADDR_DEFAULT, MCP9808_REG_CONFIG, config) + bus.write_i2c_block_data(address, MCP9808_REG_CONFIG, config) - # MCP9808 address, 0x18(24) + # MCP9808 address, default 0x18(24) # select resolution rgister, 0x08(8) # resolution = +0.0625 / C, 0x03(03) - bus.write_byte_data(MCP9808_I2CADDR_DEFAULT, MCP9808_REG_RESOLUTION, 0x03) + bus.write_byte_data(address, MCP9808_REG_RESOLUTION, 0x03) - # MCP9808 address, 0x18(24) + # MCP9808 address, default 0x18(24) # read data back from 0x05(5), 2 bytes # temp MSB, TEMP LSB - data = bus.read_i2c_block_data(MCP9808_I2CADDR_DEFAULT, MCP9808_REG_AMBIENT_TEMP, 2) + data = bus.read_i2c_block_data(address, MCP9808_REG_AMBIENT_TEMP, 2) # convert the data to 13-bits ctemp = ((data[0] & 0x1F) * 256) + data[1]