diff --git a/adafruit_am2320.py b/adafruit_am2320.py index 3e38195..59fac53 100644 --- a/adafruit_am2320.py +++ b/adafruit_am2320.py @@ -85,12 +85,23 @@ class AM2320: """ def __init__(self, i2c_bus, address=AM2320_DEFAULT_ADDR): - self._i2c = I2CDevice(i2c_bus, address) + 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 - i2c.write(bytes([0x00])) + try: + i2c.write(bytes([0x00])) + except OSError: + pass time.sleep(0.01) # wait 10 ms # Send command to read register @@ -103,7 +114,7 @@ def _read_register(self, register, length): # 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 modbus read failure') + raise RuntimeError('I2C read failure') # Check CRC on all but last 2 bytes crc1 = struct.unpack("