adafruit/Adafruit_CircuitPython_BMP280

Input / Output Error

Closed this issue · 1 comments

Hello all,
I recently encountered an error with the BMP280.
The weird part is, the code seems to run fine (crashed seemingly random after 3 hours), but I let it run during the night and apparently it had some problems with the BMP280 communication.

I am using the sensor connected to a Raspberry Pi 4 Model B, running "default" python but with blinka to be able to use all of the convenient CircuitPython libraries.

Also, I noticed that the sensor still seemed to have returned useful measurement values even though it encountered errors, plus when I restarted the script without changing anything in code or hardware, it runs fine again.
Any help is appreciated ;)

The error callstack is below:

[2023-09-25 19:10:00] Error: [Errno 5] Input/output error
[2023-09-25 19:10:00] Traceback (most recent call last):
  File "/home/pi/CloudTracking/JobRunner.py", line 73, in invoke_tasks
    baroval = gBarometer.get_value() # Barometer list
  File "/home/pi/CloudTracking/modules/Sensors/SensorTypes.py", line 60, in get_value
    return [self.bmp280.temperature, self.bmp280.pressure , self.bmp280.altitude]
  File "/home/pi/.local/lib/python3.9/site-packages/adafruit_bmp280.py", line 331, in temperature
    self._read_temperature()
  File "/home/pi/.local/lib/python3.9/site-packages/adafruit_bmp280.py", line 161, in _read_temperature
    self.mode = MODE_FORCE
  File "/home/pi/.local/lib/python3.9/site-packages/adafruit_bmp280.py", line 226, in mode
    self._write_ctrl_meas()
  File "/home/pi/.local/lib/python3.9/site-packages/adafruit_bmp280.py", line 192, in _write_ctrl_meas
    self._write_register_byte(_REGISTER_CTRL_MEAS, self._ctrl_meas)
  File "/home/pi/.local/lib/python3.9/site-packages/adafruit_bmp280.py", line 476, in _write_register_byte
    i2c.write(bytes([register & 0xFF, value & 0xFF]))
  File "/usr/local/lib/python3.9/dist-packages/adafruit_bus_device/i2c_device.py", line 100, in write
    self.i2c.writeto(self.device_address, buf, start=start, end=end)
  File "/usr/local/lib/python3.9/dist-packages/busio.py", line 202, in writeto
    return self._i2c.writeto(address, memoryview(buffer)[start:end], stop=stop)
  File "/usr/local/lib/python3.9/dist-packages/adafruit_blinka/microcontroller/generic_linux/i2c.py", line 52, in writeto
    self._i2c_bus.write_bytes(address, buffer[start:end])
  File "/usr/local/lib/python3.9/dist-packages/Adafruit_PureIO/smbus.py", line 303, in write_bytes
    self._device.write(buf)
OSError: [Errno 5] Input/output error

My relevant code piece is here as well:

# Just a basic setup class for using it on raspberry pi
class Barometer(SensorBaseClass):
    def __init__(self):        
        super().__init__()
        # specific code here
        self.i2c = I2C(1)
        self.bmp280 = adafruit_bmp280.Adafruit_BMP280_I2C(self.i2c, address=0x76)
        self.bmp280.sea_level_pressure = 1021

    def get_value(self):
        # get the values as a list
        return [self.bmp280.temperature, self.bmp280.pressure , self.bmp280.altitude]

I think the best thing to do for this is to use try/except in your code to catch the OSError and either skip getting the value that iteration, or add a bit of logic to retry again until it's successful, or ultimately bail out and reboot or something if it continues to fail over and over depending on your needs.

If you try that and still have trouble please feel free to re-open or create a new issue.