diff --git a/.gitignore b/.gitignore index 73a638b..bfa5184 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ /.embuild /target /Cargo.lock +/secret diff --git a/secret/.keep b/secret/.keep new file mode 100644 index 0000000..e69de29 diff --git a/src/command.rs b/src/command.rs index 00fb9ed..13c5370 100644 --- a/src/command.rs +++ b/src/command.rs @@ -12,7 +12,7 @@ impl Command { Command { text: "ATI".to_string(), timeout: Duration::from_millis(6000), - contains: Some("+CIEV".to_string()), + contains: Some("OK".to_string()), } } @@ -236,7 +236,7 @@ impl Command { Command { text: "AT".to_string(), timeout: Duration::from_millis(3000), - contains: Some("+CIEV".to_string()), + contains: Some("OK".to_string()), } } diff --git a/src/config.rs b/src/config.rs index 85dc37a..67e62e5 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,3 +1,5 @@ +#![allow(dead_code)] + pub struct GprsAp<'a> { pub apn: &'a str, pub username: &'a str, diff --git a/src/modem.rs b/src/modem.rs index 78d0cb9..4594936 100644 --- a/src/modem.rs +++ b/src/modem.rs @@ -1,3 +1,5 @@ +#![allow(dead_code)] + use crate::command::Command; use crate::serial::SerialIO; use crate::types::*; @@ -67,7 +69,7 @@ impl Modem { pub fn init(&mut self, mut pwrkey: impl OutputPin, mut rst: impl OutputPin, mut power: impl OutputPin) -> Result<()> { println!("Turning SIM800L on ..."); power.set_high().map_err(|_| ModemError::SetupError("Error setting POWER to high.".to_string()))?; - //rst.set_high().map_err(|_| ModemError::SetupError("Error setting RST to high.".to_string()))?; + rst.set_high().map_err(|_| ModemError::SetupError("Error setting RST to high.".to_string()))?; // Pull down PWRKEY for more than 1 second according to manual requirements pwrkey.set_high().map_err(|_| ModemError::SetupError("Error setting PWRKEY to high.".to_string()))?; thread::sleep(Duration::from_millis(1500)); @@ -76,18 +78,18 @@ impl Modem { pwrkey.set_high().map_err(|_| ModemError::SetupError("Error setting PWRKEY to high.".to_string()))?; println!("Waiting for sim module to come online ..."); thread::sleep(Duration::from_millis(3000)); - loop { - match self.send_command(Command::probe()) { - Ok(_) => break, - _ => { - thread::sleep(Duration::from_millis(2000)); - continue - }, - } + for _ in 0..10 { + let _ = self.send_command(Command::probe()).unwrap_or("".to_string()); + thread::sleep(Duration::from_millis(1000)); } Ok(()) } + pub fn echo(&mut self, enabled: bool) -> Result<()> { + let cmd = format!("ATE{}", if enabled { 1 } else { 0 }); + self.send(&cmd, "OK").map(|_| ()) + } + /// Reads the serial RX until the `contains` string is encoutered if `contains` is Some(s), if /// None, then the first line is returned. If a timeout is reached. The timeout is provided on /// input via the `timeout` argument. The first argument `contains` is checked against every @@ -114,7 +116,7 @@ impl Modem { if response.contains(&c) { Ok(response) } else { - Err(ModemError::CommandError(format!("Didn't get expected ({}) from modem.", c))) + Err(ModemError::CommandError(format!("Didn't get expected ({}) from modem. Got: {}", c, response))) } } else { Ok(response) @@ -463,11 +465,13 @@ impl Modem { Err(ModemError::ReadError("TCP server didn't respond!".into())) } - fn mqtt_connect(&mut self, device_id: &str) -> anyhow::Result<()> { + fn mqtt_connect(&mut self, device_id: &str, username: &str, password: &str) -> anyhow::Result<()> { let mut buf = Vec::new(); let mut conn = ConnectPacket::new(device_id); conn.set_clean_session(true); conn.set_keep_alive(100); + conn.set_user_name(Some(username.to_string())); + conn.set_password(Some(password.to_string())); let _ = conn.encode(&mut buf)?; self.tcp_manual_send(&mut buf).ok(); @@ -535,6 +539,9 @@ where let (tx, rx) = serial.split(); let mut mdm = Modem::new(tx, rx); + let mqtt_username = include_str!("../secret/username").trim(); + let mqtt_password = include_str!("../secret/password").trim(); + mdm.init(pwrkey, rst, power)?; // thread::sleep(Duration::from_millis(500)); @@ -547,12 +554,13 @@ where //let _ = mdm.ssl_set_client_cert(client_cert_path, "t")?; //let _ = mdm.fs_list("C:\\USER\\")?; + let _ = mdm.echo(false); // Retry 5 times to open a TCP connection, otherwise fail and wait for reboot. let mut retries = 0; loop { if !mdm.is_gprs_attached()? { - let _ = mdm.gprs_attach_ap(crate::config::A1)?; + let _ = mdm.gprs_attach_ap(crate::config::MTS)?; } if let Ok(()) = mdm.try_connect_gprs() { // When command AT+CIPQSEND=0, it is in normal sending mode. In this mode, after user @@ -570,7 +578,8 @@ where } let device_id = "c36a72df-5bd6-4f9b-995d-059433bc3267"; - let _ = mdm.mqtt_connect(device_id)?; + println!("connecting to MQTT with ({}:{})", mqtt_username, mqtt_password); + let _ = mdm.mqtt_connect(device_id, mqtt_username, mqtt_password)?; println!("entering queue receive loop ..."); let mut err_count = 0; diff --git a/src/types.rs b/src/types.rs index daed150..ff52839 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,3 +1,5 @@ +#![allow(dead_code)] + #[derive(Debug)] pub struct Solution { pub latitude: f64, @@ -11,4 +13,3 @@ pub enum Msg { Gps(Solution), Accelerometer(String), } -