MQTT gateway for ESP8266 or ESP32 with bidirectional 433mhz/315mhz/868mhz, Infrared communications, BLE, Bluetooth, beacons detection, mi flora, mi jia, LYWSD02, LYWSD03MMC, Mi Scale, TPMS, BBQ thermometer compatibility & LoRa.
In the file "ZsensorBH1750.ino", the Wire.requestFrom() is encapsulated by Wire.beginTransmission() and Wire.endTransmission(). Those can be removed, since the Wire.beginTransmission() and Wire.endTransmission() should only be used when writing data.
The Wire.requestFrom() does a complete I2C transaction on its own.
If the sensor is not connected or there was a detectable problem on the I2C bus, the Wire.available() will probably return zero. If you want, you could test if the same number of bytes is received as was requested. However, if that fails, there is a serious hardware I2C problem.
The BH1750 itself always returns a value, but if it is read too soon, the value is from a previous measurement.
// Check if reads failed and exit early (to try again).
Wire.requestFrom(BH1750_i2c_addr, 2);
if(Wire.available() != 2) {
trc(F("Failed to read from LightSensor BH1750!"));
}else{
i = Wire.read();
i <<=8;
i |= Wire.read();
// Calculate the Values
In the file "ZsensorBH1750.ino", the Wire.requestFrom() is encapsulated by Wire.beginTransmission() and Wire.endTransmission(). Those can be removed, since the Wire.beginTransmission() and Wire.endTransmission() should only be used when writing data.
The Wire.requestFrom() does a complete I2C transaction on its own.
If the sensor is not connected or there was a detectable problem on the I2C bus, the Wire.available() will probably return zero. If you want, you could test if the same number of bytes is received as was requested. However, if that fails, there is a serious hardware I2C problem. The BH1750 itself always returns a value, but if it is read too soon, the value is from a previous measurement.