2022-06-13 00:55:08 +02:00
|
|
|
use std::fmt::Write;
|
|
|
|
use std::thread;
|
|
|
|
use std::time::Duration;
|
2022-06-11 16:49:16 +02:00
|
|
|
|
2022-06-13 00:55:08 +02:00
|
|
|
use embedded_hal::digital::v2::OutputPin;
|
|
|
|
use embedded_hal::serial::Read;
|
|
|
|
use esp_idf_hal::serial::{self, Rx, Tx};
|
|
|
|
|
|
|
|
pub fn init(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_owned()))?;
|
|
|
|
rst.set_high().map_err(|_| ModemError::SetupError("Error setting RST to high.".to_owned()))?;
|
|
|
|
// 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_owned()))?;
|
|
|
|
thread::sleep(Duration::from_millis(100));
|
|
|
|
pwrkey.set_low().map_err(|_| ModemError::SetupError("Error setting PWRKEY to low.".to_owned()))?;
|
|
|
|
thread::sleep(Duration::from_millis(1000));
|
|
|
|
pwrkey.set_high().map_err(|_| ModemError::SetupError("Error setting PWRKEY to high.".to_owned()))?;
|
|
|
|
println!("Waiting 5s for sim module to come online ...");
|
|
|
|
thread::sleep(Duration::from_millis(5000));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Modem<UART: serial::Uart> {
|
2022-06-11 16:49:16 +02:00
|
|
|
is_connected: bool,
|
2022-06-13 00:55:08 +02:00
|
|
|
rx: Rx<UART>,
|
|
|
|
tx: Tx<UART>,
|
2022-06-11 16:49:16 +02:00
|
|
|
}
|
|
|
|
|
2022-06-13 00:55:08 +02:00
|
|
|
#[derive(Debug)]
|
2022-06-12 12:02:59 +02:00
|
|
|
pub enum ModemError {
|
|
|
|
GprsAPConnectionError(String),
|
|
|
|
ATCommandError(String),
|
|
|
|
CommandError(String),
|
2022-06-13 00:55:08 +02:00
|
|
|
SetupError(String),
|
2022-06-12 12:02:59 +02:00
|
|
|
}
|
2022-06-11 16:49:16 +02:00
|
|
|
|
2022-06-12 12:02:59 +02:00
|
|
|
impl std::fmt::Display for ModemError {
|
2022-06-11 16:49:16 +02:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
2022-06-13 00:55:08 +02:00
|
|
|
write!(f, "{:?}", self)
|
2022-06-11 16:49:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-12 12:02:59 +02:00
|
|
|
pub type Result<T> = std::result::Result<T, ModemError>;
|
2022-06-11 16:49:16 +02:00
|
|
|
|
2022-06-13 00:55:08 +02:00
|
|
|
impl<UART: serial::Uart> Modem<UART> {
|
|
|
|
pub fn new(tx: Tx<UART>, rx: Rx<UART>) -> Self {
|
2022-06-11 16:49:16 +02:00
|
|
|
Self {
|
|
|
|
is_connected: false,
|
2022-06-12 12:02:59 +02:00
|
|
|
rx,
|
|
|
|
tx,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn at_command(&mut self, cmd: &str) -> Result<String> {
|
|
|
|
let mut msg = "AT+".to_owned();
|
|
|
|
msg.push_str(cmd);
|
2022-06-13 00:55:08 +02:00
|
|
|
self.send_command(&msg)
|
2022-06-12 12:02:59 +02:00
|
|
|
.map_err(|err| ModemError::ATCommandError(format!("{}", err)))
|
|
|
|
}
|
|
|
|
|
2022-06-13 00:55:08 +02:00
|
|
|
fn read_response(&mut self) -> Result<String> {
|
|
|
|
let len = self.rx.count()
|
|
|
|
.map_err(|_| ModemError::CommandError("Error getting RX fifo length".to_owned()))?;
|
|
|
|
|
|
|
|
if cfg!(debug_assertions) {
|
|
|
|
println!("Reading {} bytes from serial RX ...", len);
|
|
|
|
}
|
|
|
|
|
2022-06-12 12:02:59 +02:00
|
|
|
let mut response = String::new();
|
2022-06-13 00:55:08 +02:00
|
|
|
for _ in 0..len {
|
|
|
|
nb::block!(self.rx.read())
|
|
|
|
.map(|b| response.push(b as char))
|
|
|
|
.map_err(|_| ModemError::CommandError("Error reading from RX".to_owned()))?;
|
|
|
|
}
|
|
|
|
response = response.trim().to_owned();
|
|
|
|
|
2022-06-12 12:02:59 +02:00
|
|
|
if cfg!(debug_assertions) {
|
2022-06-13 00:55:08 +02:00
|
|
|
println!("Received: {}", response);
|
2022-06-11 16:49:16 +02:00
|
|
|
}
|
2022-06-12 12:02:59 +02:00
|
|
|
Ok(response)
|
2022-06-11 16:49:16 +02:00
|
|
|
}
|
2022-06-12 12:02:59 +02:00
|
|
|
|
2022-06-13 00:55:08 +02:00
|
|
|
fn send_command(&mut self, cmd: &str) -> Result<String> {
|
|
|
|
writeln!(self.tx, "{}", cmd)
|
|
|
|
.map_err(|_| ModemError::CommandError("error writing to serial".to_owned()))?;
|
|
|
|
self.read_response()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn connect_to_gprs_ap(&mut self, apn: &str, username: &str, password: &str)-> Result<String> {
|
|
|
|
println!("connecting to {} with {}:{}", apn, username, password);
|
2022-06-12 12:02:59 +02:00
|
|
|
self.at_command("CGATTCGATT=1")
|
2022-06-13 00:55:08 +02:00
|
|
|
.map(|response| {
|
|
|
|
println!("connected to {} with response: {}", apn, response);
|
2022-06-12 12:02:59 +02:00
|
|
|
self.is_connected = true;
|
2022-06-13 00:55:08 +02:00
|
|
|
response
|
|
|
|
})
|
|
|
|
.map_err(|err| ModemError::GprsAPConnectionError(format!("{}", err)))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn test_modem(&mut self)-> Result<String> {
|
|
|
|
println!("testing modem with AP command");
|
|
|
|
self.at_command("AT")
|
|
|
|
.map(|response| {
|
|
|
|
println!("modem responded with: {}", response);
|
|
|
|
response
|
2022-06-12 12:02:59 +02:00
|
|
|
})
|
|
|
|
.map_err(|err| ModemError::GprsAPConnectionError(format!("{}", err)))
|
2022-06-11 16:49:16 +02:00
|
|
|
}
|
2022-06-10 01:32:52 +02:00
|
|
|
}
|