bme280-rs icon indicating copy to clipboard operation
bme280-rs copied to clipboard

called `Result::unwrap()` on an `Err` value: InvalidData

Open weiying-chen opened this issue 1 year ago • 10 comments

With this code (to read the weather with esp32-c3 and bm280):

use esp_idf_svc::hal::{
    delay,
    i2c::{I2cConfig, I2cDriver},
    peripherals::Peripherals,
    prelude::*,
};

use bme280::i2c::BME280;

fn main() {
    esp_idf_svc::sys::link_patches();
    esp_idf_svc::log::EspLogger::initialize_default();

    let peripherals = Peripherals::take().unwrap();
    let sda = peripherals.pins.gpio2;
    let scl = peripherals.pins.gpio3;
    let config = I2cConfig::new().baudrate(400_u32.kHz().into());
    let i2c = I2cDriver::new(peripherals.i2c0, sda, scl, &config).unwrap();
    let mut bme280 = BME280::new_primary(i2c);
    let mut delay = delay::Ets;

    bme280.init(&mut delay);

    loop {
        let measurements = bme280.measure(&mut delay).unwrap();

        log::info!("Relative Humidity = {}%", measurements.humidity);
        log::info!("Temperature = {} deg C", measurements.temperature);
        log::info!("Pressure = {} pascals", measurements.pressure);
        delay::FreeRtos::delay_ms(10000u32);
    }
}

And I get this error:

thread 'main' panicked at src/main.rs:40:55:
called `Result::unwrap()` on an `Err` value: InvalidData

It panics here:

let measurements = bme280.measure(&mut delay).unwrap();

It's strange because this almost identical code works:

#![no_std]
#![no_main]

use bme280::i2c::BME280;
use esp32c3_hal::{clock::ClockControl, i2c::I2C, peripherals::Peripherals, prelude::*, Delay, IO};
use esp_backtrace as _;
use esp_println::println;

#[entry]
fn main() -> ! {
    let peripherals = Peripherals::take();
    let system = peripherals.SYSTEM.split();
    let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
    let mut delay = Delay::new(&clocks);
    let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
    let sda = io.pins.gpio2;
    let scl = io.pins.gpio3;
    let i2c = I2C::new(peripherals.I2C0, sda, scl, 400u32.kHz(), &clocks);
    let mut bme280 = BME280::new_primary(i2c);

    bme280.init(&mut delay).unwrap();

    loop {
        let measurements = bme280.measure(&mut delay).unwrap();
        println!("Relative Humidity = {}%", measurements.humidity);
        println!("Temperature = {} deg C", measurements.temperature);
        println!("Pressure = {} pascals", measurements.pressure);
        delay.delay_ms(1000u32);
    }
}

What could be the issue with the first code?

weiying-chen avatar Feb 17 '24 14:02 weiying-chen

I'm experiencing a similar issue in my project after the e-h-1 update. I have several sensors on the I2C bus (100 kHz) and all of them work but the BME280. I could not find the solution yet.

crespum avatar Feb 20 '24 12:02 crespum

@crespum I created a repo with a working example plus instructions.

weiying-chen avatar Feb 20 '24 14:02 weiying-chen

Try this patch from @RobinThrift. It seems to work for me.

diff --git a/Cargo.toml b/Cargo.toml
index 414e9d5..a0bc11b 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -26,7 +26,8 @@ embassy = ["esp-idf-svc/embassy-sync", "esp-idf-svc/critical-section", "esp-idf-
 [dependencies]
 log = { version = "0.4", default-features = false }
 esp-idf-svc = { version = "0.48", default-features = false }
-bme280 = "0.5.0"
+# bme280 = "0.5.0"
+bme280 = { git = "https://github.com/RobinThrift/bme280-rs.git", commit = "12d5e98253e0e8fadbd43895a608263f4a84884d"}

 [build-dependencies]
 embuild = "0.31.3"

crespum avatar Feb 20 '24 15:02 crespum

Okay, I'll check it out. But my working example reads the sensor successfully.

weiying-chen avatar Feb 20 '24 15:02 weiying-chen

I've run your example. Sometimes it works, sometimes it doesn't work.

crespum avatar Feb 20 '24 15:02 crespum

Ah, that's strange. It runs every time for me. I'll keep an eye on that.

But if it sometimes run and sometimes it doesn't, I think it's more a hardware thing than a software thing?

weiying-chen avatar Feb 20 '24 15:02 weiying-chen

Well. I'm not completely sure. I've never run into any similar issue with the previous version of the crate (using the same hardware).

To me, the bug is somehow related to the delay.

crespum avatar Feb 20 '24 15:02 crespum

Yes, there was an issue with the delay. That's why I added a patch in Cargo.toml.

weiying-chen avatar Feb 20 '24 16:02 weiying-chen

seems like this was introduced here when upgrading embedded-hal https://github.com/VersBinarii/bme280-rs/commit/960eaa76edcda97eb5224485499f4cb569f06acd#diff-b1a35a68f14e696205874893c07fd24fdb88882b47c23cc0e0c80a30c7d53759L590

kbleeke avatar Feb 24 '24 16:02 kbleeke

I just merged the PR and published the new version.

VersBinarii avatar Apr 20 '24 12:04 VersBinarii