diff --git a/tprais_semestralka1/src/bin/main.rs b/tprais_semestralka1/src/bin/main.rs index 5d63a68..8d88012 100644 --- a/tprais_semestralka1/src/bin/main.rs +++ b/tprais_semestralka1/src/bin/main.rs @@ -6,7 +6,6 @@ clippy::mem_forget, reason = "mem::forget is generally not safe to do with esp_hal types" )] -// TODO WARNING core 1 should be logic, core 0 wifi, its flipped now use embassy_executor::Spawner; use embassy_futures::select::{select, Either}; @@ -14,15 +13,13 @@ use embassy_net::{Runner, StackResources}; use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; use embassy_sync::signal::Signal; use embassy_time::{Duration, Instant, Timer}; -use projekt_final::mqtt::client; +use projekt_final::mqtt::client::{mqtt_publish, mqtt_task}; use esp_alloc as _; use esp_backtrace as _; use esp_hal::{ clock::CpuClock, - gpio::InputConfig, - gpio::{Input, Pull}, rng::Rng, system::{CpuControl, Stack}, timer::timg::TimerGroup, @@ -32,18 +29,12 @@ use esp_wifi::{ EspWifiController, }; -use core::cell::RefCell; -use core::fmt::Write; -use heapless::String; +// use core::cell::RefCell; +// use core::fmt::Write; use log::info; -use pages_tui::input::Key; use rust_mqtt::packet::v5::publish_packet::QualityOfService; use static_cell::StaticCell; -use projekt_final::mqtt::client::{ - mqtt_events, mqtt_publish, mqtt_subscribe, mqtt_task, mqtt_try_publish, IncomingMsg, -}; - extern crate alloc; static APP_CORE_STACK: StaticCell> = StaticCell::new(); @@ -93,11 +84,7 @@ async fn main(spawner: Spawner) -> ! { let seed = (rng.random() as u64) << 32 | rng.random() as u64; - let config = InputConfig::default().with_pull(Pull::Down); - let button_select = Input::new(peripherals.GPIO32, config); - let button_next = Input::new(peripherals.GPIO35, config); - - // Core1 is now general stuff and core 0 is for wifi and mqtt + // Core1 is now general stuff and core0 is for wifi and mqtt let mut cpu_control = CpuControl::new(peripherals.CPU_CTRL); let _guard = cpu_control .start_app_core(APP_CORE_STACK.init(Stack::new()), move || { @@ -111,33 +98,20 @@ async fn main(spawner: Spawner) -> ! { }) .unwrap(); - // Wait for network to be ready (signaled from core 1) - NETWORK_READY.wait().await; - info!("Network ready, starting core 0 tasks"); - - // Start core 0 for WiFi and MQTT (network stack created there) + // Core0 WiFi and MQTT spawner .spawn(network_task(spawner, controller, wifi_interface, seed)) .unwrap(); - mqtt_subscribe("esp32/read").await; - mqtt_publish("esp32/imu", b"online", QualityOfService::QoS1, false).await; - let mqtt_rx = mqtt_events(); - let mut mqtt_msg_count: u32 = 0; - let mut mqtt_publish_drops: u32 = 0; - let mut last_mqtt_publish = Instant::now(); - let mqtt_publish_interval = Duration::from_secs(3); + // Wait for network to be ready (signaled from core0) + NETWORK_READY.wait().await; + info!("Network ready"); + + mqtt_publish("esp32/post", b"online", QualityOfService::QoS1, false).await; loop { - match select(mqtt_rx.receive(), Timer::after(Duration::from_secs(5))).await { - Either::First(msg) => { - mqtt_msg_count += 1; - handle_mqtt_message(msg).await; - } - Either::Second(_) => { - info!("Ahoj",); - } - } + Timer::after(Duration::from_secs(10)).await; + mqtt_publish("esp32/post", b"abba", QualityOfService::QoS1, false).await; } } @@ -182,17 +156,6 @@ async fn network_task( spawner.spawn(mqtt_task(stack)).ok(); } -async fn handle_mqtt_message(msg: IncomingMsg) { - if let Ok(txt) = core::str::from_utf8(&msg.payload) { - match txt { - "status" => { - mqtt_publish("esp32/status", b"running", QualityOfService::QoS1, false).await; - } - _ => {} - } - } -} - #[embassy_executor::task] async fn connection_task(mut controller: WifiController<'static>) { loop {