adafruit / Adafruit_BMP280_Library

Arduino Library for BMP280 sensors

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Failing Gracefully?

sellensr opened this issue · comments

  • Arduino board: Itsy Bitsy M0 Express and others

  • Arduino IDE version (found in Arduino -> About Arduino menu): 1.8.16 and others

  • List the steps to reproduce the problem below (if possible attach a sketch or
    copy the sketch code in too):

begin() does not return if the sensor is mis-wired, e.g. power lead is not connected. I would have hoped for a timeout after a while and returning with a failure. I suspect this may be farther downstream in the unified sensor or bus io code.

Functions like readTemperature() don't return if called before begin()

None of this is really unexpected, but my students are finding ingenious ways to do things in the wrong order and more resilient software might set a good example. A start at a simple solution might have begin() set a flag that a sensor has already been found and have the others test the flag before trying to talk to the sensor. I'm wondering if this is a problem worth solving...

The begin() function attempts to do some checking and return true/false as appropriate. However, for things like an unpowered sensor, it may not be possible, or at least gets tricky. This library sort of assumes the I2C bus is OK. You're correct that it would be up to the lower level libraries to attempt to detect these kinds of things.

The general way to check things before proceeding is shown in the example:

if (!bmp.begin()) {
Serial.println(F("Could not find a valid BMP280 sensor, check wiring or "
"try a different address!"));
while (1) delay(10);
}

If the call to begin() is not returning, then there's likely not much this library can do to remedy that, since it's probably one of the calls to the lower libraries that is actually hanging.

The equivalent areas of the BME280 library code set the _sensorID variable and print some additional diagnostic information in the test example code. That would be easy to implement here and would pave the way for testing _sensorID to see if a valid sensor has been found and initialized before trying to read from it. _sensorID was declared, but not initialized or set in the current code.

That's essentially the same thing. Using the return of the call to begin(). It just prints more information when it fails.

Checking chip ID is one of the things done in begin():

if (read8(BMP280_REGISTER_CHIPID) != chipid)
return false;

But successfully reading that requires the device has power and is wired correctly, etc.

I think that's as fixed as it can get locally -- anything else would need to happen in the unified sensor library or wire