we now have address of the I2C

This commit is contained in:
Filipriec
2026-05-05 14:51:26 +02:00
parent 1d63fb5d93
commit 5b7e108d5c

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 defmt::error;
use embassy_executor::Spawner; use embassy_executor::Spawner;
use embassy_futures::select::{select, Either}; use embassy_futures::select::{select, Either};
use embassy_net::{Runner, StackResources}; use embassy_net::{Runner, StackResources};
@@ -78,10 +79,30 @@ async fn main(spawner: Spawner) -> ! {
.into_async(); .into_async();
let mut buffer = [0u8; 1]; let mut buffer = [0u8; 1];
let SAD = 0x5C; let mut devices_count = 0;
let SUB = &[0x0F];
i2c.write_read_async(SAD, SUB, &mut buffer).await.unwrap(); info!("Starting I2C bus scan...");
info!("WHO_AM_I: {:x}", buffer[0]);
for addr in 0x08..=0x77 {
match i2c.write_read_async(addr, &[0x0F], &mut buffer).await {
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);
@@ -123,11 +144,12 @@ async fn main(spawner: Spawner) -> ! {
NETWORK_READY.wait().await; NETWORK_READY.wait().await;
info!("Network ready"); info!("Network ready");
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;
loop { loop {
Timer::after(Duration::from_secs(10)).await; Timer::after(Duration::from_secs(10)).await;
mqtt_publish("esp32/post", b"abba", QualityOfService::QoS1, false).await; mqtt_publish("esp32/post", b"aaaa", QualityOfService::QoS1, false).await;
} }
} }