bme680-rust/examples/reading_temperature.rs

59 lines
1.9 KiB
Rust
Raw Normal View History

2018-05-25 20:38:24 +00:00
extern crate bme680;
2018-05-18 12:57:34 +00:00
extern crate embedded_hal;
2018-05-18 20:12:42 +00:00
extern crate env_logger;
2018-05-18 12:57:34 +00:00
extern crate linux_embedded_hal as hal;
#[macro_use]
extern crate log;
2018-05-18 12:57:34 +00:00
2018-05-25 20:38:24 +00:00
use bme680::*;
use embedded_hal::blocking::i2c;
2018-05-18 12:57:34 +00:00
use hal::*;
use std::result;
use std::thread;
2018-05-18 12:57:34 +00:00
use std::time::Duration;
fn main() -> result::Result<
(),
Bme680Error<<hal::I2cdev as i2c::Read>::Error, <hal::I2cdev as i2c::Write>::Error>,
> {
2018-05-18 20:12:42 +00:00
env_logger::init();
2018-05-18 12:57:34 +00:00
let i2c = I2cdev::new("/dev/i2c-1").unwrap();
2018-05-30 06:28:48 +00:00
let mut dev = Bme680_dev::init(i2c, Delay {}, I2CAddress::Primary)?;
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_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");
2018-05-30 06:28:48 +00:00
dev.set_sensor_settings(settings)?;
info!("Setting forced power modes");
2018-05-18 12:57:34 +00:00
dev.set_sensor_mode(PowerMode::ForcedMode)?;
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 {
2018-05-21 14:26:14 +00:00
thread::sleep(Duration::from_millis(5000));
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(PowerMode::ForcedMode)?;
info!("Retrieving sensor data");
2018-05-24 20:02:23 +00:00
let (data, _state) = dev.get_sensor_data()?;
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
}