send received messages via mqtt in modem main

This commit is contained in:
Vladan Popovic 2022-07-20 13:32:13 +02:00
parent 23ff182b65
commit 78df516fba
5 changed files with 52 additions and 22 deletions

View File

@ -4,13 +4,13 @@ use std::sync::mpsc::SyncSender;
use std::thread; use std::thread;
use std::time::Duration; use std::time::Duration;
use crate::modem::Msg; use crate::types::*;
pub fn main(sender: SyncSender<Msg>) -> Result<(), anyhow::Error> { pub fn main(sender: SyncSender<Msg>) -> Result<(), anyhow::Error> {
println!("entering ACCELERATOR sender loop ..."); println!("entering ACCELERATOR sender loop ...");
for i in 0..20 { for i in 0..20 {
println!("sending ACCELERATOR message ({}) of 20 ...", i); 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)); thread::sleep(Duration::from_millis(2000));
} }
Ok(()) Ok(())

View File

@ -12,7 +12,7 @@ use esp_idf_hal::serial::{self, Rx, Tx};
use ublox::*; use ublox::*;
use crate::modem::Msg; use crate::types::*;
use crate::serial::SerialIO; use crate::serial::SerialIO;
struct GpsModule<UART: serial::Uart> { struct GpsModule<UART: serial::Uart> {
@ -176,15 +176,15 @@ pub fn main<T: Sync + Send>(
if has_posvel { if has_posvel {
let pos: Position = (&sol).into(); let pos: Position = (&sol).into();
let vel: Velocity = (&sol).into(); let vel: Velocity = (&sol).into();
println!( let solution = Solution {
"Latitude: {:.5} Longitude: {:.5} Altitude: {:.2}m", latitude: pos.lat,
pos.lat, pos.lon, pos.alt longitude: pos.lon,
); altitude: pos.alt,
println!( speed: vel.speed,
"Speed: {:.2} m/s Heading: {:.2} degrees", direction: vel.heading,
vel.speed, vel.heading };
); println!("Sol: {:?}", solution);
println!("Sol: {:?}", sol); sender.send(Msg::Gps(solution));
} }
} }
_ => { _ => {

View File

@ -5,6 +5,7 @@ mod command;
mod gps; mod gps;
mod modem; mod modem;
mod serial; mod serial;
mod types;
use anyhow; use anyhow;
use std::{ use std::{
@ -13,6 +14,7 @@ use std::{
}; };
use esp_idf_hal::peripherals::Peripherals; use esp_idf_hal::peripherals::Peripherals;
use types::*;
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
esp_idf_sys::link_patches(); esp_idf_sys::link_patches();
@ -40,7 +42,7 @@ fn main() -> anyhow::Result<()> {
let gps_uart = dp.uart2; let gps_uart = dp.uart2;
let (gps_sender, receiver) = std::sync::mpsc::sync_channel::<modem::Msg>(1); let (gps_sender, receiver) = std::sync::mpsc::sync_channel::<Msg>(1);
let accel_sender = gps_sender.clone(); let accel_sender = gps_sender.clone();

View File

@ -1,5 +1,6 @@
use crate::command::Command; use crate::command::Command;
use crate::serial::SerialIO; use crate::serial::SerialIO;
use crate::types::*;
use anyhow; use anyhow;
use std::{ use std::{
@ -406,11 +407,6 @@ impl<UART: serial::Uart> std::io::Write for Modem<UART> {
} }
} }
pub enum Msg {
Location(String),
Movement(String),
}
pub fn main<T: Sync + Send>( pub fn main<T: Sync + Send>(
rx: esp_idf_hal::gpio::Gpio26<T>, rx: esp_idf_hal::gpio::Gpio26<T>,
tx: esp_idf_hal::gpio::Gpio27<T>, tx: esp_idf_hal::gpio::Gpio27<T>,
@ -484,10 +480,28 @@ pub fn main<T: Sync + Send>(
let _ = mdm.mqtt_connect(device_id)?; let _ = mdm.mqtt_connect(device_id)?;
println!("entering queue receive loop ..."); println!("entering queue receive loop ...");
while let Ok(Msg::Location(msg)) = receiver.recv() { let mut err_count = 0;
println!("received message {} | sending to mqtt ...", msg); loop {
let _ = mdm.mqtt_publish(device_id, &msg)?; 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()?; let _ = mdm.tcp_close_connection()?;
} }

14
src/types.rs Normal file
View File

@ -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),
}