1technophile / OpenMQTTGateway

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.
https://docs.openmqttgateway.com
GNU General Public License v3.0
3.53k stars 781 forks source link

The Wire.requestFrom() does not need Wire.beginTransmission() or Wire.endTransmission(). #89

Closed Koepel closed 6 years ago

Koepel commented 7 years ago

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
1technophile commented 7 years ago

Hello thanks for submitting this, @hannesdi, would it be possible to take a look to that please?

1technophile commented 6 years ago

@HannesDi do you have some thoughts on this