measure pressure and temp!

This commit is contained in:
Clara Dautermann 2025-07-08 20:55:28 +02:00
parent c3768283d3
commit ff2a2e2440
Signed by: clara
GPG key ID: 223391B52FAD4463
3 changed files with 155 additions and 6 deletions

View file

@ -1,10 +1,72 @@
use bme280::i2c::BME280;
use esp_idf_hal::{
delay::Delay,
i2c::{I2cConfig, I2cDriver},
prelude::Peripherals,
};
fn main() {
// It is necessary to call this function once. Otherwise some patches to the runtime
// implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71
esp_idf_svc::sys::link_patches();
// Bind the log crate to the ESP Logging facilities
//set up logger
esp_idf_sys::link_patches(); // Important!
esp_idf_svc::log::EspLogger::initialize_default();
log::info!("Meow :3");
log::info!("Hello, world!");
// Initialize peripherals
let peripherals = Peripherals::take().unwrap();
// Define SCL and SDA pins
let sda = peripherals.pins.gpio21;
let scl = peripherals.pins.gpio22;
// Create I2C config
let config = I2cConfig::new().baudrate(100000.into());
log::info!("Initializing I2C bus…");
// Initialize I2C driver
let i2c_bus = match I2cDriver::new(
peripherals.i2c0, // or i2c1/i2c2 based on board
sda,
scl,
&config,
) {
Ok(i2c_connection) => i2c_connection,
Err(err) => {
log::error!("Initializing I2C connection failed with {err}!");
panic!()
}
};
log::info!("I2C up!");
log::info!("Initializing bme280…");
let mut bme280 = BME280::new_primary(i2c_bus);
//initialize Sensor
match bme280.init(&mut Delay::new(100000)) {
Ok(_) => {}
Err(err) => {
log::error!("Error initializing bme280 sensor:");
match err {
bme280::Error::CompensationFailed => log::error!("Failed to compensate a raw measurement"),
bme280::Error::Bus(buserr) => log::error!("Bus error: {}", buserr),
bme280::Error::InvalidData => log::error!("Failed to pare sensor data!"),
bme280::Error::NoCalibrationData => log::error!("No calibration data is available (probably forgot to call or check BME280::init for failure)"),
bme280::Error::UnsupportedChip => log::error!("Chip ID doesn't match expected value"),
bme280::Error::Delay => log::error!("Delay error"),
}
}
}
log::info!("BME280 up!");
loop {
let measurement = bme280.measure(&mut Delay::new_default()).unwrap();
println!(
"1temperature:{0}°C, pressure:{1}",
measurement.temperature, measurement.pressure
);
Delay::default().delay_ms(1000);
}
}