bme680-rust/examples/read_sensor_data.rs

56 lines
2.1 KiB
Rust
Raw Normal View History

#![no_std]
2024-02-27 20:09:16 +00:00
use bme680::i2c::Address;
use bme680::{Bme680, Bme680Error, IIRFilterSize, OversamplingSetting, PowerMode, SettingsBuilder};
use core::time::Duration;
use embedded_hal::delay::DelayNs;
use linux_embedded_hal as hal;
use linux_embedded_hal::Delay;
use log::info;
2024-02-25 19:00:59 +00:00
2024-02-25 18:48:10 +00:00
// Please export RUST_LOG=info in order to see logs in the console.
2024-02-27 20:09:16 +00:00
fn main() -> Result<(), Bme680Error> {
2018-05-18 20:12:42 +00:00
env_logger::init();
let i2c = hal::I2cdev::new("/dev/i2c-1").unwrap();
let mut delayer = Delay {};
2018-05-18 12:57:34 +00:00
let mut dev = Bme680::init(i2c, &mut delayer, Address::Primary)?;
let mut delay = Delay {};
2018-05-30 06:28:48 +00:00
let settings = SettingsBuilder::new()
.with_humidity_oversampling(OversamplingSetting::OS2x)
.with_pressure_oversampling(OversamplingSetting::OS4x)
.with_temperature_oversampling(OversamplingSetting::OS8x)
2018-05-30 18:53:18 +00:00
.with_temperature_filter(IIRFilterSize::Size3)
2018-05-30 06:28:48 +00:00
.with_gas_measurement(Duration::from_millis(1500), 320, 25)
.with_temperature_offset(-2.2)
2018-05-30 06:28:48 +00:00
.with_run_gas(true)
.build();
let profile_dur = dev.get_profile_dur(&settings.0)?;
2018-05-30 18:53:18 +00:00
info!("Profile duration {:?}", profile_dur);
info!("Setting sensor settings");
dev.set_sensor_settings(&mut delayer, settings)?;
info!("Setting forced power modes");
dev.set_sensor_mode(&mut delayer, PowerMode::ForcedMode)?;
2018-05-18 12:57:34 +00:00
2018-05-30 06:28:48 +00:00
let sensor_settings = dev.get_sensor_settings(settings.1);
2018-05-20 20:08:34 +00:00
info!("Sensor settings: {:?}", sensor_settings);
loop {
2024-02-24 20:20:22 +00:00
let _ = delay.delay_ms(5000u32);
2018-05-20 20:08:34 +00:00
let power_mode = dev.get_sensor_mode();
info!("Sensor power mode: {:?}", power_mode);
info!("Setting forced power modes");
dev.set_sensor_mode(&mut delayer, PowerMode::ForcedMode)?;
info!("Retrieving sensor data");
let (data, _state) = dev.get_sensor_data(&mut delayer)?;
info!("Sensor Data {:?}", data);
2018-05-21 14:26:14 +00:00
info!("Temperature {}°C", data.temperature_celsius());
info!("Pressure {}hPa", data.pressure_hpa());
info!("Humidity {}%", data.humidity_percent());
info!("Gas Resistence {}Ω", data.gas_resistance_ohm());
}
2018-05-18 12:57:34 +00:00
}