From c6beb6c3bafd3fa62c847024566947857441210b Mon Sep 17 00:00:00 2001 From: Jerry Needell Date: Mon, 20 Apr 2020 19:56:42 -0400 Subject: [PATCH] revert PR_9 --- adafruit_am2320.py | 21 +------ pi@gjnpi4desk.local | 138 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+), 18 deletions(-) create mode 100644 pi@gjnpi4desk.local diff --git a/adafruit_am2320.py b/adafruit_am2320.py index 0370773..654b54a 100644 --- a/adafruit_am2320.py +++ b/adafruit_am2320.py @@ -77,21 +77,6 @@ def _crc16(data): return crc -class AM2320Exception(Exception): - """Base class for exceptions.""" - - -class AM2320DeviceNotFound(AM2320Exception, ValueError): - """Indicates that a device couldn't be found.""" - - -class AM2320ReadError(AM2320Exception, RuntimeError): - """indicates that valid data could not be read from the sensor. - - This may be due to a regular I2C read failure, or due to a checksum - mismatch.""" - - class AM2320: """A driver for the AM2320 temperature and humidity sensor. @@ -109,7 +94,7 @@ class AM2320: except ValueError: pass time.sleep(0.25) - raise AM2320DeviceNotFound("AM2320 not found") + raise ValueError("AM2320 not found") def _read_register(self, register, length): with self._i2c as i2c: @@ -130,12 +115,12 @@ class AM2320: # print("$%02X => %s" % (register, [hex(i) for i in result])) # Check preamble indicates correct readings if result[0] != 0x3 or result[1] != length: - raise AM2320ReadError("I2C read failure") + raise RuntimeError("I2C read failure") # Check CRC on all but last 2 bytes crc1 = struct.unpack("`_ (Product ID: 3721) + +**Software and Dependencies:** + +* Adafruit CircuitPython firmware for the ESP8622 and M0-based boards: + https://github.com/adafruit/circuitpython/releases +* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice + +""" + +# imports +try: + import struct +except ImportError: + import ustruct as struct + +import time + +from adafruit_bus_device.i2c_device import I2CDevice +from micropython import const + +__version__ = "0.0.0-auto.0" +__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_am2320.git" + + +AM2320_DEFAULT_ADDR = const(0x5C) +AM2320_CMD_READREG = const(0x03) +AM2320_REG_TEMP_H = const(0x02) +AM2320_REG_HUM_H = const(0x00) + + +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 + + +class AM2320: + """A driver for the AM2320 temperature and humidity sensor. + + :param i2c_bus: The `busio.I2C` object to use. This is the only required parameter. + :param int address: (optional) The I2C address of the device. + + """ + + def __init__(self, i2c_bus, address=AM2320_DEFAULT_ADDR): + for _ in range(3): + # retry since we have to wake up the devices + try: + self._i2c = I2CDevice(i2c_bus, address) + return + except ValueError: + pass + time.sleep(0.25) + raise ValueError("AM2320 not found") + + def _read_register(self, register, length): + with self._i2c as i2c: + # wake up sensor + try: + i2c.write(bytes([0x00])) + except OSError: + pass + time.sleep(0.01) # wait 10 ms + + # Send command to read register + cmd = [AM2320_CMD_READREG, register & 0xFF, length] + # print("cmd: %s" % [hex(i) for i in cmd]) + i2c.write(bytes(cmd)) + time.sleep(0.002) # wait 2 ms for reply + result = bytearray(length + 4) # 2 bytes pre, 2 bytes crc + i2c.readinto(result) + # print("$%02X => %s" % (register, [hex(i) for i in result])) + # Check preamble indicates correct readings + if result[0] != 0x3 or result[1] != length: + raise RuntimeError("I2C read failure") + # Check CRC on all but last 2 bytes + crc1 = struct.unpack("H", self._read_register(AM2320_REG_TEMP_H, 2))[0] + if temperature >= 32768: + temperature = 32768 - temperature + return temperature / 10.0 + + @property + def relative_humidity(self): + """The measured relative humidity in percent.""" + humidity = struct.unpack(">H", self._read_register(AM2320_REG_HUM_H, 2))[0] + return humidity / 10.0