Skip to content

Commit

Permalink
Fix I2C Error during wakeup, add retry logic
Browse files Browse the repository at this point in the history
Closes Humidity data I2C Error - ESP32 #13
  • Loading branch information
tyeth committed Aug 21, 2023
1 parent 8f8be1a commit 99c1152
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions Adafruit_AM2320.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,24 @@ uint32_t Adafruit_AM2320::readRegister32(uint8_t reg) {
uint8_t buffer[8] = {0, 0, 0, 0, 0, 0, 0, 0};

// wake up
i2c_dev->write(buffer, 1);
delay(10); // wait 10 ms
bool writtenBytes = i2c_dev->write(buffer, 1);
delay(10); // wait 10 ms to wake up
if (!writtenBytes) {
delay(90); // wait remainder of 100ms for wakeup before retrying
i2c_dev->write(buffer, 1);
delay(10); // wait 10 ms to wake up
}

// send a command to read register
buffer[0] = AM2320_CMD_READREG;
buffer[1] = reg;
buffer[2] = 4; // 4 bytes
i2c_dev->write(buffer, 3);
writtenBytes = i2c_dev->write(buffer, 3);
delay(2); // wait 2 ms
if (!writtenBytes) {
i2c_dev->write(buffer, 3); // try once more...
delay(2);
}

// 2 bytes preamble, 4 bytes data, 2 bytes CRC
i2c_dev->read(buffer, 8);
Expand Down

0 comments on commit 99c1152

Please sign in to comment.