Giter Site home page Giter Site logo

bme280-rs's Introduction

Rust BME280 Crate

Version Documentation Downloads License MSRV

A Rust crate to query temperature, pressure and humidity from sensor BME280

https://gitlab.com/claudiomattera/bme280-rs/

This crate support both embedded-hal and embedded-hal-async.

See the changelog for this project.

Usage

Add the dependency to Cargo.toml.

[dependencies.bme280-rs]
version = "0.2.0"

A Bme280 structure can be created from an I²C interface and a delay function. The initial sampling configuration disables all measurements, so it is necessary to reconfigure the chip with the desired settings before read samples.

use bme280_rs::{Bme280, Configuration, Oversampling, SensorMode};

let i2c = ...
let delay = ...

let mut bme280 = Bme280::new(i2c, delay);

bme280.init()?;

bme280.set_sampling_configuration(
    Configuration::default()
        .with_temperature_oversampling(Oversampling::Oversample1)
        .with_pressure_oversampling(Oversampling::Oversample1)
        .with_humidity_oversampling(Oversampling::Oversample1)
        .with_sensor_mode(SensorMode::Normal)
)?;

if let Some(temperature) = bme280.read_temperature()? {
    println!("Temperature: {} C", temperature);
} else {
    println!("Temperature reading was disabled");
}

An AsyncBme280 structure can be used with asynchronous HALs. Its API is completely identical to Bme280, just with .async at the end of function calls.

use bme280_rs::{AsyncBme280, Configuration, Oversampling, SensorMode};

let i2c = ...
let delay = ...

let mut bme280 = AsyncBme280::new(i2c, delay);

bme280.init()?;

bme280.set_sampling_configuration(
    Configuration::default()
        .with_temperature_oversampling(Oversampling::Oversample1)
        .with_pressure_oversampling(Oversampling::Oversample1)
        .with_humidity_oversampling(Oversampling::Oversample1)
        .with_sensor_mode(SensorMode::Normal)
).await?;

if let Some(temperature) = bme280.read_temperature().await? {
    println!("Temperature: {} C", temperature);
} else {
    println!("Temperature reading was disabled");
}

Unit of Measurements

By default, this crate uses f32 values for all the measurements temperature, pressure and humidity. When instead enabling the Cargo feature uom, it uses quantities from crate uom. Temperature measurements have type uom::si::f32::ThermodynamicTemperature, pressure measurements have type uom::si::f32::Pressure, and humidity measurements have type uom::si::f32::Ratio.

License

Copyright Claudio Mattera 2022-2024

You are free to copy, modify, and distribute this application with attribution under the terms of either

at your option.

This project is entirely original work, and it is not affiliated with nor endorsed in any way by Bosch Sensortec.

bme280-rs's People

Contributors

claudiomattera avatar

Stargazers

Seiji Ainoguchi avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.