read MQTT username and password from files
and some other tweaks here and there ...
This commit is contained in:
parent
47b333d354
commit
10c1018e07
6 changed files with 29 additions and 16 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -2,3 +2,4 @@
|
||||||
/.embuild
|
/.embuild
|
||||||
/target
|
/target
|
||||||
/Cargo.lock
|
/Cargo.lock
|
||||||
|
/secret
|
||||||
|
|
0
secret/.keep
Normal file
0
secret/.keep
Normal file
|
@ -12,7 +12,7 @@ impl Command {
|
||||||
Command {
|
Command {
|
||||||
text: "ATI".to_string(),
|
text: "ATI".to_string(),
|
||||||
timeout: Duration::from_millis(6000),
|
timeout: Duration::from_millis(6000),
|
||||||
contains: Some("+CIEV".to_string()),
|
contains: Some("OK".to_string()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -236,7 +236,7 @@ impl Command {
|
||||||
Command {
|
Command {
|
||||||
text: "AT".to_string(),
|
text: "AT".to_string(),
|
||||||
timeout: Duration::from_millis(3000),
|
timeout: Duration::from_millis(3000),
|
||||||
contains: Some("+CIEV".to_string()),
|
contains: Some("OK".to_string()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
#![allow(dead_code)]
|
||||||
|
|
||||||
pub struct GprsAp<'a> {
|
pub struct GprsAp<'a> {
|
||||||
pub apn: &'a str,
|
pub apn: &'a str,
|
||||||
pub username: &'a str,
|
pub username: &'a str,
|
||||||
|
|
35
src/modem.rs
35
src/modem.rs
|
@ -1,3 +1,5 @@
|
||||||
|
#![allow(dead_code)]
|
||||||
|
|
||||||
use crate::command::Command;
|
use crate::command::Command;
|
||||||
use crate::serial::SerialIO;
|
use crate::serial::SerialIO;
|
||||||
use crate::types::*;
|
use crate::types::*;
|
||||||
|
@ -67,7 +69,7 @@ impl<UART: serial::Uart> Modem<UART> {
|
||||||
pub fn init(&mut self, mut pwrkey: impl OutputPin, mut rst: impl OutputPin, mut power: impl OutputPin) -> Result<()> {
|
pub fn init(&mut self, mut pwrkey: impl OutputPin, mut rst: impl OutputPin, mut power: impl OutputPin) -> Result<()> {
|
||||||
println!("Turning SIM800L on ...");
|
println!("Turning SIM800L on ...");
|
||||||
power.set_high().map_err(|_| ModemError::SetupError("Error setting POWER to high.".to_string()))?;
|
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
|
// 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()))?;
|
pwrkey.set_high().map_err(|_| ModemError::SetupError("Error setting PWRKEY to high.".to_string()))?;
|
||||||
thread::sleep(Duration::from_millis(1500));
|
thread::sleep(Duration::from_millis(1500));
|
||||||
|
@ -76,18 +78,18 @@ impl<UART: serial::Uart> Modem<UART> {
|
||||||
pwrkey.set_high().map_err(|_| ModemError::SetupError("Error setting PWRKEY to high.".to_string()))?;
|
pwrkey.set_high().map_err(|_| ModemError::SetupError("Error setting PWRKEY to high.".to_string()))?;
|
||||||
println!("Waiting for sim module to come online ...");
|
println!("Waiting for sim module to come online ...");
|
||||||
thread::sleep(Duration::from_millis(3000));
|
thread::sleep(Duration::from_millis(3000));
|
||||||
loop {
|
for _ in 0..10 {
|
||||||
match self.send_command(Command::probe()) {
|
let _ = self.send_command(Command::probe()).unwrap_or("".to_string());
|
||||||
Ok(_) => break,
|
thread::sleep(Duration::from_millis(1000));
|
||||||
_ => {
|
|
||||||
thread::sleep(Duration::from_millis(2000));
|
|
||||||
continue
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Ok(())
|
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
|
/// 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
|
/// 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
|
/// input via the `timeout` argument. The first argument `contains` is checked against every
|
||||||
|
@ -114,7 +116,7 @@ impl<UART: serial::Uart> Modem<UART> {
|
||||||
if response.contains(&c) {
|
if response.contains(&c) {
|
||||||
Ok(response)
|
Ok(response)
|
||||||
} else {
|
} 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 {
|
} else {
|
||||||
Ok(response)
|
Ok(response)
|
||||||
|
@ -463,11 +465,13 @@ impl<UART: serial::Uart> Modem<UART> {
|
||||||
Err(ModemError::ReadError("TCP server didn't respond!".into()))
|
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 buf = Vec::new();
|
||||||
let mut conn = ConnectPacket::new(device_id);
|
let mut conn = ConnectPacket::new(device_id);
|
||||||
conn.set_clean_session(true);
|
conn.set_clean_session(true);
|
||||||
conn.set_keep_alive(100);
|
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)?;
|
let _ = conn.encode(&mut buf)?;
|
||||||
self.tcp_manual_send(&mut buf).ok();
|
self.tcp_manual_send(&mut buf).ok();
|
||||||
|
|
||||||
|
@ -535,6 +539,9 @@ where
|
||||||
let (tx, rx) = serial.split();
|
let (tx, rx) = serial.split();
|
||||||
let mut mdm = Modem::new(tx, rx);
|
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)?;
|
mdm.init(pwrkey, rst, power)?;
|
||||||
|
|
||||||
// thread::sleep(Duration::from_millis(500));
|
// thread::sleep(Duration::from_millis(500));
|
||||||
|
@ -547,12 +554,13 @@ where
|
||||||
//let _ = mdm.ssl_set_client_cert(client_cert_path, "t")?;
|
//let _ = mdm.ssl_set_client_cert(client_cert_path, "t")?;
|
||||||
//let _ = mdm.fs_list("C:\\USER\\")?;
|
//let _ = mdm.fs_list("C:\\USER\\")?;
|
||||||
|
|
||||||
|
let _ = mdm.echo(false);
|
||||||
// Retry 5 times to open a TCP connection, otherwise fail and wait for reboot.
|
// Retry 5 times to open a TCP connection, otherwise fail and wait for reboot.
|
||||||
let mut retries = 0;
|
let mut retries = 0;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
if !mdm.is_gprs_attached()? {
|
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() {
|
if let Ok(()) = mdm.try_connect_gprs() {
|
||||||
// When command AT+CIPQSEND=0, it is in normal sending mode. In this mode, after user
|
// 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 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 ...");
|
println!("entering queue receive loop ...");
|
||||||
let mut err_count = 0;
|
let mut err_count = 0;
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
#![allow(dead_code)]
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Solution {
|
pub struct Solution {
|
||||||
pub latitude: f64,
|
pub latitude: f64,
|
||||||
|
@ -11,4 +13,3 @@ pub enum Msg {
|
||||||
Gps(Solution),
|
Gps(Solution),
|
||||||
Accelerometer(String),
|
Accelerometer(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue