Add complete example for reading sensor data

This commit is contained in:
Denis-Cosmin Nutiu 2024-11-16 16:05:40 +02:00
parent 877c8bb6bc
commit 46c0c67c67
3 changed files with 87 additions and 4 deletions

View file

@ -13,10 +13,15 @@ To use this library, create a new project and add it as a dependency:
bme680 = {git = "https://github.com/dnutiu/bme680-rust.git", version = "0.9.0"}
```
# Alternative
[drogue-bme680](https://github.com/drogue-iot/drogue-bme680)
# Getting started on Raspberry Pi
# Example getting started Linux
Assuming that you have connected the sensor to the Raspberry PI's GPIO ports.
Install required libraries for developing I2C.
```bash
sudo apt-get install build-essential libi2c-dev i2c-tools python-dev libffi-dev
```
Determine the I2C device path
@ -40,3 +45,17 @@ pi@raspberrypi:~ $ i2cdetect -y 1
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- 76
```
The read-sensor-data from the examples folder is configured to use the I2C device `/dev/i2c-1` and the `0x76` address.
To run the example, clone the repository and go to the examples' folder:
```bash
git clone https://github.com/dnutiu/bme680-rust.git && cd bme680-rust/examples/read-sensor-data
```
Then run the example:
```bash
export RUST_LOG=info
cargo run
```

Before

Width:  |  Height:  |  Size: 1.4 KiB

After

Width:  |  Height:  |  Size: 1.9 KiB

View file

@ -0,0 +1,12 @@
[package]
name = "read-sensor-data"
version = "0.1.0"
edition = "2021"
[dependencies]
bme680 = {path = "../"}
env_logger = "0.11.5"
linux-embedded-hal = "0.4.0"
anyhow = { version = "1.0.80" }
log = "0.4"
embedded-hal = "=1.0.0"

View file

@ -0,0 +1,52 @@
use bme680::i2c::Address;
use bme680::{Bme680, 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;
// Please export RUST_LOG=info in order to see logs in the console.
fn main() -> Result<(), anyhow::Error> {
env_logger::init();
let i2c = hal::I2cdev::new("/dev/i2c-1").unwrap();
let mut delayer = Delay {};
let mut dev = Bme680::init(i2c, &mut delayer, Address::Primary)?;
let mut delay = Delay {};
let settings = SettingsBuilder::new()
.with_humidity_oversampling(OversamplingSetting::OS2x)
.with_pressure_oversampling(OversamplingSetting::OS4x)
.with_temperature_oversampling(OversamplingSetting::OS8x)
.with_temperature_filter(IIRFilterSize::Size3)
.with_gas_measurement(Duration::from_millis(1500), 320, 23)
.with_run_gas(true)
.build();
let profile_dur = dev.get_profile_duration(&settings.0)?;
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)?;
let sensor_settings = dev.get_sensor_settings(settings.1);
info!("Sensor settings: {:?}", sensor_settings);
loop {
let _ = delay.delay_ms(5000u32);
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_measurement(&mut delayer)?;
info!("Sensor Data {:?}", data);
info!("Temperature {}°C", data.temperature_celsius());
info!("Pressure {}hPa", data.pressure_hpa());
info!("Humidity {}%", data.humidity_percent());
info!("Gas Resistence {}Ω", data.gas_resistance_ohm());
}
}