#[cfg(target_os = "linux")]
fn main() -> Result<(), Box<dyn std::error::Error>> {
use linux_embedded_hal::{Delay, I2cdev};
use sht4x::{Precision, Sht4x, DEFAULT_ADDRESS};
use std::{thread, time::Duration};
let i2c = I2cdev::new("/dev/i2c-1")?;
let mut sensor = Sht4x::new(i2c, Delay, DEFAULT_ADDRESS);
sensor
.soft_reset()
.map_err(|e| format!("soft reset failed: {e:?}"))?;
let sn = sensor
.read_serial_number()
.map_err(|e| format!("serial read failed: {e:?}"))?;
println!("SHT4x serial number: 0x{sn:08X}");
loop {
match sensor.measure(Precision::High) {
Ok(m) => {
println!(
"T = {:.2} °C ({} m°C) RH = {:.2} % ({} m%)",
m.temperature_celsius(),
m.temperature_milli_celsius(),
m.humidity_percent(),
m.humidity_milli_percent(),
);
}
Err(e) => eprintln!("measurement error: {e:?}"),
}
thread::sleep(Duration::from_secs(2));
}
}
#[cfg(not(target_os = "linux"))]
fn main() {
eprintln!("This example only builds on Linux (it uses linux-embedded-hal).");
}