BME280 only works wrapped in Option?

Hi there! I'm coding Rust for ESP32, my repository can be found here: thomas351 / esp32-rust-playground · GitLab

The current state of my main branch works, but if I replace my lines 138 & 153-157:

let mut bme280: Option<BME280<I2cDriver>>;
...
bme280 = Some(BME280::new_primary(i2c_driver));
  match bme280.as_mut().unwrap().init(&mut delay::FreeRtos) {
      Ok(_) => {
          println!("bme280 initialisation ok! starting timer!");
          let timer = timer_service?.timer(move || read_bme280(bme280.as_mut().unwrap()))?;

by

let mut bme280: BME280<I2cDriver>;
...
bme280 = BME280::new_primary(i2c_driver);
  match bme280.init(&mut delay::FreeRtos) {
      Ok(_) => {
          println!("bme280 initialisation ok! starting timer!");
          let timer = timer_service?.timer(move || read_bme280(&mut bme280))?;

The BME280 library gives me an InvalidData Error instead of the data struct I want and get in the first example.

The code doing the sensor reading:

fn read_bme280(bme280: &mut BME280<I2cDriver>) {
    match bme280.measure(&mut delay::FreeRtos) {
        Ok(measurements) => {
            println!("Relative Humidity = {}%", measurements.humidity);
            println!("Temperature = {} deg C", measurements.temperature);
            println!("Pressure = {} pascals", measurements.pressure);
        },
        Err(error) => {
            println!("failed to read bme280 sensor! error: {:?}", error);
        },
    }
}

Can somebody explain to me the difference of those two code segments? Since I never have to deal with the "None", I thought I should be able to remove the Option without any side effects, but that believe was wrong, and I don't understand why...

Are you sure you didn't make any other changes? Generally the changes you showed should not behave any differently

1 Like

UPDATE: okey, so it turns out that observation was flawed, as expected. Though now it looks to be random if a build will print InvalidData or the correct sensor readings, which is even worse since I have no idea how to fix it.

UPDATE2: okey, so I found a workaround: simply reinitialize the bme280 sensor when reading fails makes it work reliable :slight_smile:

(ps.: sorry about the confusion with the Option wrapper preventing this error, it looked that way because by pure chance it first failed when I removed the Option and worked again after I undid the changes and built again.

The error is not even dependent on build, but it seems on some boots the first init call leaves the bme280 interface in a bad state which is fixed by calling init again.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.