diff --git a/octoprint_enclosure/AM2320.py b/octoprint_enclosure/AM2320.py new file mode 100644 index 0000000..d55811e --- /dev/null +++ b/octoprint_enclosure/AM2320.py @@ -0,0 +1,82 @@ +import smbus +import time + +try: + import struct +except ImportError: + import ustruct as struct + +class AM2320Exception(Exception): + """ Base class for exception """ + +class AM2320DeviceNotFound(AM2320Exception, ValueError): + """ Device not found """ + +class AM2320ReadError(AM2320Exception, RuntimeError): + """ Read error or CRC mismatch """ + +def _crc16(data): + crc = 0xFFFF + for byte in data: + crc ^= byte + for _ in range(8): + if crc & 0x0001: + crc >>= 1 + crc ^= 0xA001 + else: + crc >>= 1 + return crc + + +sensor = smbus.SMBus(1) + +def getTemp(bus): + for _ in range(3): + try: + bus.write_byte(0x5C, 0x00) + except: + pass + + bus.write_i2c_block_data(0x5C, 0x03, [0x02, 2]) + time.sleep(0.001) + result = bytearray(bus.read_i2c_block_data(0x5C, 0x00, 6)) + if result[0] != 0x3 or result[1] != 2: + raise AM2320ReadError("Command does not match returned data") + temp = struct.unpack(">H", result[2:-2])[0] + crc1 = struct.unpack("= 32768: + temp = 32768 - temp + return (temp / 10.0) + +def getHumi(bus): + for _ in range(3): + try: + bus.write_byte(0x5C, 0x00) + except: + pass + + bus.write_i2c_block_data(0x5C, 0x03, [0x00, 2]) + time.sleep(0.001) + result = bytearray(bus.read_i2c_block_data(0x5C, 0x00, 6)) + if result[0] != 0x3 or result[1] != 2: + raise AM2320ReadError("Command does not match returned data") + humi = struct.unpack(">H", result[2:-2])[0] + crc1 = struct.unpack("= 0){ + if (['11', '22', '2302', 'bme280', 'am2320', 'si7021'].indexOf(sensor) >= 0){ return true; } return false; diff --git a/octoprint_enclosure/templates/enclosure_settings.jinja2 b/octoprint_enclosure/templates/enclosure_settings.jinja2 index 29bdc5f..45a2b8a 100644 --- a/octoprint_enclosure/templates/enclosure_settings.jinja2 +++ b/octoprint_enclosure/templates/enclosure_settings.jinja2 @@ -536,6 +536,7 @@ + @@ -563,7 +564,7 @@ - +
@@ -605,7 +606,7 @@
- +