send received messages via mqtt in modem main
This commit is contained in:
parent
23ff182b65
commit
78df516fba
5 changed files with 52 additions and 22 deletions
|
@ -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<Msg>) -> 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(())
|
||||
|
|
20
src/gps.rs
20
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<UART: serial::Uart> {
|
||||
|
@ -176,15 +176,15 @@ pub fn main<T: Sync + Send>(
|
|||
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));
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
|
|
|
@ -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::<modem::Msg>(1);
|
||||
let (gps_sender, receiver) = std::sync::mpsc::sync_channel::<Msg>(1);
|
||||
|
||||
let accel_sender = gps_sender.clone();
|
||||
|
||||
|
|
32
src/modem.rs
32
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<UART: serial::Uart> std::io::Write for Modem<UART> {
|
|||
}
|
||||
}
|
||||
|
||||
pub enum Msg {
|
||||
Location(String),
|
||||
Movement(String),
|
||||
}
|
||||
|
||||
pub fn main<T: Sync + Send>(
|
||||
rx: esp_idf_hal::gpio::Gpio26<T>,
|
||||
tx: esp_idf_hal::gpio::Gpio27<T>,
|
||||
|
@ -484,10 +480,28 @@ pub fn main<T: Sync + Send>(
|
|||
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()?;
|
||||
}
|
||||
|
|
14
src/types.rs
Normal file
14
src/types.rs
Normal 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),
|
||||
}
|
||||
|
Loading…
Reference in a new issue