From 78df516fba925fd923efa331afeb07aa6e3ba5c4 Mon Sep 17 00:00:00 2001 From: Vladan Popovic Date: Wed, 20 Jul 2022 13:32:13 +0200 Subject: [PATCH] send received messages via mqtt in modem main --- src/accel.rs | 4 ++-- src/gps.rs | 20 ++++++++++---------- src/main.rs | 4 +++- src/modem.rs | 32 +++++++++++++++++++++++--------- src/types.rs | 14 ++++++++++++++ 5 files changed, 52 insertions(+), 22 deletions(-) create mode 100644 src/types.rs diff --git a/src/accel.rs b/src/accel.rs index 5eef823..50c0e56 100644 --- a/src/accel.rs +++ b/src/accel.rs @@ -4,13 +4,13 @@ use std::sync::mpsc::SyncSender; use std::thread; use std::time::Duration; -use crate::modem::Msg; +use crate::types::*; pub fn main(sender: SyncSender) -> Result<(), anyhow::Error> { println!("entering ACCELERATOR sender loop ..."); for i in 0..20 { println!("sending ACCELERATOR message ({}) of 20 ...", i); - let _ = sender.send(Msg::Movement("{\"velocity\": 21.43, \"altitude\": 367}".to_string()))?; + let _ = sender.send(Msg::Accelerometer("{\"velocity\": 21.43, \"altitude\": 367}".to_string()))?; thread::sleep(Duration::from_millis(2000)); } Ok(()) diff --git a/src/gps.rs b/src/gps.rs index ef3f90a..025cad7 100644 --- a/src/gps.rs +++ b/src/gps.rs @@ -12,7 +12,7 @@ use esp_idf_hal::serial::{self, Rx, Tx}; use ublox::*; -use crate::modem::Msg; +use crate::types::*; use crate::serial::SerialIO; struct GpsModule { @@ -176,15 +176,15 @@ pub fn main( if has_posvel { let pos: Position = (&sol).into(); let vel: Velocity = (&sol).into(); - println!( - "Latitude: {:.5} Longitude: {:.5} Altitude: {:.2}m", - pos.lat, pos.lon, pos.alt - ); - println!( - "Speed: {:.2} m/s Heading: {:.2} degrees", - vel.speed, vel.heading - ); - println!("Sol: {:?}", sol); + let solution = Solution { + latitude: pos.lat, + longitude: pos.lon, + altitude: pos.alt, + speed: vel.speed, + direction: vel.heading, + }; + println!("Sol: {:?}", solution); + sender.send(Msg::Gps(solution)); } } _ => { diff --git a/src/main.rs b/src/main.rs index 3265b58..0505ed7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ mod command; mod gps; mod modem; mod serial; +mod types; use anyhow; use std::{ @@ -13,6 +14,7 @@ use std::{ }; use esp_idf_hal::peripherals::Peripherals; +use types::*; fn main() -> anyhow::Result<()> { esp_idf_sys::link_patches(); @@ -40,7 +42,7 @@ fn main() -> anyhow::Result<()> { let gps_uart = dp.uart2; - let (gps_sender, receiver) = std::sync::mpsc::sync_channel::(1); + let (gps_sender, receiver) = std::sync::mpsc::sync_channel::(1); let accel_sender = gps_sender.clone(); diff --git a/src/modem.rs b/src/modem.rs index d209211..9d1e46e 100644 --- a/src/modem.rs +++ b/src/modem.rs @@ -1,5 +1,6 @@ use crate::command::Command; use crate::serial::SerialIO; +use crate::types::*; use anyhow; use std::{ @@ -406,11 +407,6 @@ impl std::io::Write for Modem { } } -pub enum Msg { - Location(String), - Movement(String), -} - pub fn main( rx: esp_idf_hal::gpio::Gpio26, tx: esp_idf_hal::gpio::Gpio27, @@ -484,10 +480,28 @@ pub fn main( let _ = mdm.mqtt_connect(device_id)?; println!("entering queue receive loop ..."); - while let Ok(Msg::Location(msg)) = receiver.recv() { - println!("received message {} | sending to mqtt ...", msg); - let _ = mdm.mqtt_publish(device_id, &msg)?; - } + let mut err_count = 0; + loop { + match receiver.recv() { + Ok(Msg::Gps(solution)) => { + println!("received GPS solution {:?} | sending to mqtt ...", solution); + let _ = mdm.mqtt_publish(device_id, &format!("{:?}", solution))?; + }, + Ok(Msg::Accelerometer(acc)) => { + println!("received message {} | sending to mqtt ...", acc); + let _ = mdm.mqtt_publish(device_id, &format!("{:?}", acc))?; + } + Err(e) => { + println!("received error {} | NOT sending to mqtt ...", e); + if err_count < 10 { + err_count += 1; + } + else { + break + } + } + } + }; let _ = mdm.tcp_close_connection()?; } diff --git a/src/types.rs b/src/types.rs new file mode 100644 index 0000000..daed150 --- /dev/null +++ b/src/types.rs @@ -0,0 +1,14 @@ +#[derive(Debug)] +pub struct Solution { + pub latitude: f64, + pub longitude: f64, + pub altitude: f64, + pub speed: f64, + pub direction: f64, +} + +pub enum Msg { + Gps(Solution), + Accelerometer(String), +} +