debugged MPU working mqtt is dropping

This commit is contained in:
Priec
2026-01-18 21:50:10 +01:00
parent 94c591c087
commit 70e1559b24
3 changed files with 106 additions and 21 deletions

View File

@@ -43,7 +43,7 @@ use projekt_final::{
bus,
display,
mpu,
mqtt::client::{mqtt_events, mqtt_publish, mqtt_subscribe, mqtt_task, IncomingMsg},
mqtt::client::{mqtt_events, mqtt_try_publish, mqtt_publish, mqtt_subscribe, mqtt_task, IncomingMsg},
};
extern crate alloc;
@@ -145,6 +145,7 @@ async fn main(spawner: Spawner) -> ! {
let imu_rx = mpu::api::events();
let mut imu_reading_count: u32 = 0;
let mut mqtt_msg_count: u32 = 0;
let mut mqtt_publish_drops: u32 = 0;
loop {
match select3(
@@ -168,19 +169,30 @@ async fn main(spawner: Spawner) -> ! {
log::info!("IMU drained {} stale readings before display", drained);
}
imu_reading_count += 1;
display::api::show_imu(reading).await;
// CRITICAL FIX: show_imu is now non-blocking (uses try_send internally)
// This prevents display channel backpressure from blocking the main loop
display::api::show_imu(reading);
if imu_reading_count % MQTT_PUBLISH_DIVIDER == 0 {
let payload = format!(
"{{\"ax\":{:.2},\"ay\":{:.2},\"az\":{:.2},\"t\":{:.1}}}",
reading.accel_g[0], reading.accel_g[1], reading.accel_g[2], reading.temp_c
);
mqtt_publish("esp32/imu", payload.as_bytes(), QualityOfService::QoS0, false).await;
// CRITICAL FIX: Use non-blocking publish for sensor data
// If MQTT channel is full, we drop this reading rather than blocking
if !mqtt_try_publish("esp32/imu", payload.as_bytes(), QualityOfService::QoS0, false) {
mqtt_publish_drops += 1;
if mqtt_publish_drops % 10 == 1 {
log::warn!("MQTT publish dropped (total: {})", mqtt_publish_drops);
}
}
}
}
Either3::Third(_) => {
crate::mpu::api::IMU_CHANNEL.clear();
info!("IMU heartbeat: force-cleared queue, {} readings total", imu_reading_count);
// info!("Heartbeat: {} IMU readings", imu_reading_count);
info!("IMU heartbeat: force-cleared queue, {} readings total, {} mqtt drops",
imu_reading_count, mqtt_publish_drops);
}
}
}