Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better error handling if not two bytes are available for read. #41

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

bjung82
Copy link

@bjung82 bjung82 commented Feb 26, 2018

Do not tear down I2C bus forever if not 2 bytes are available for read.

This is sometimes the case, and after such an unsuccessful read the I2C bus
was held down forever and stopped working (for all other components on the bus,
too!)

@bjung82 bjung82 mentioned this pull request Feb 26, 2018
@bjung82
Copy link
Author

bjung82 commented Feb 26, 2018

I also changed
#include "Wire.h" to
#include <Wire.h>

I use the sensor on an ESP32 - on arduino it seems like there is no "reset()" available?
The "reset" fixed the sporadic issues I had in my setup ESP32+BH1750.

@coelner
Copy link
Contributor

coelner commented Feb 28, 2018

the Wire.reset() is defined in the specific library (espressif/arduino-esp32#678)
therefore we need a #ifdeffor each architecture.
ESP8266 issue: esp8266/Arduino#1025

But it is nothing we should do in this library. It is part of the SoC and the handling over there. the library for the architecture needs a fix.

Please try this instead:

// Read two bytes from the sensor, which are low and high parts of the sensor
// value
if (Wire.requestFrom(BH1750_I2CADDR, 2) == 2) {
    level = __wire_read();
    level <<= 8;
    level |= __wire_read();
}
else {
//do some error routine like return max uint16 value.
  Serial.print(F("[BH1750] i2c read error. Not enough bytes received! "));
  return 65535;
}

@guillaume-dorczynski
Copy link

For anyone having I2C issues on ESP32, try this: stickbreaker/arduino-esp32#16 (comment) . I had problems with reading DS3231, I2C was crashing and required a hardware reset. Since I use his modified files I have yet to see another I2C error! I see some debug message about I2C, but it's always recovered and working.

@coelner coelner mentioned this pull request Mar 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants