working pressure sensor

This commit is contained in:
Filipriec
2026-05-05 16:06:14 +02:00
parent 5b7e108d5c
commit edd59fe5c8

View File

@@ -7,6 +7,7 @@
reason = "mem::forget is generally not safe to do with esp_hal types" reason = "mem::forget is generally not safe to do with esp_hal types"
)] )]
use core::fmt::Write;
use defmt::error; use defmt::error;
use embassy_executor::Spawner; use embassy_executor::Spawner;
use embassy_futures::select::{select, Either}; use embassy_futures::select::{select, Either};
@@ -79,30 +80,19 @@ async fn main(spawner: Spawner) -> ! {
.into_async(); .into_async();
let mut buffer = [0u8; 1]; let mut buffer = [0u8; 1];
let mut devices_count = 0; const SENSOR_ADDR: u8 = 0x5D;
i2c.write_read_async(SENSOR_ADDR, &[0x0F], &mut buffer)
.await
.unwrap();
i2c.write_async(SENSOR_ADDR, &[0x10, 0x22]).await.unwrap();
info!("Starting I2C bus scan..."); let mut data = [0u8; 3];
i2c.write_read_async(SENSOR_ADDR, &[0x28], &mut data)
.await
.unwrap();
for addr in 0x08..=0x77 { let raw_pa = (data[0] as i32) | ((data[1] as i32) << 8) | ((data[2] as i32) << 16);
match i2c.write_read_async(addr, &[0x0F], &mut buffer).await { let pa = raw_pa as f32 / 4096.0;
Ok(_) => {
info!(
"Found device at address: 0x{:02X} (WHO_AM_I: 0x{:02X})",
addr, buffer[0]
);
devices_count += 1;
}
Err(_) => {
// No device responded at this address, continue silently
}
}
}
if devices_count == 0 {
info!("Scan complete: No devices found.");
} else {
info!("Scan complete: {} device(s) found.", devices_count);
}
info!("Initializing WiFi..."); info!("Initializing WiFi...");
let timg0 = TimerGroup::new(peripherals.TIMG0); let timg0 = TimerGroup::new(peripherals.TIMG0);
@@ -144,12 +134,36 @@ async fn main(spawner: Spawner) -> ! {
NETWORK_READY.wait().await; NETWORK_READY.wait().await;
info!("Network ready"); info!("Network ready");
let mut payload: heapless::String<16> = heapless::String::new();
write!(payload, "{:.2}", pa).unwrap();
// mqtt_publish("esp32/post", b"online", QualityOfService::QoS1, false).await; // mqtt_publish("esp32/post", b"online", QualityOfService::QoS1, false).await;
// mqtt_publish("esp32/post", &whoami, QualityOfService::QoS1, false).await; mqtt_publish(
"esp32/post",
payload.as_bytes(),
QualityOfService::QoS1,
false,
)
.await;
loop { loop {
Timer::after(Duration::from_secs(10)).await; let mut data = [0u8; 3];
mqtt_publish("esp32/post", b"aaaa", QualityOfService::QoS1, false).await; i2c.write_read_async(SENSOR_ADDR, &[0x28], &mut data)
.await
.unwrap();
let raw_pa = (data[0] as i32) | ((data[1] as i32) << 8) | ((data[2] as i32) << 16);
let pa = raw_pa as f32 / 4096.0;
Timer::after(Duration::from_millis(400)).await;
let mut payload: heapless::String<16> = heapless::String::new();
write!(payload, "{:.2}", pa).unwrap();
mqtt_publish(
"esp32/post",
payload.as_bytes(),
QualityOfService::QoS1,
false,
)
.await;
} }
} }