RADI RADNJA!!

This commit is contained in:
Vladan Popovic 2022-06-13 00:55:08 +02:00
parent e3c3aa7391
commit 737e29f108
3 changed files with 110 additions and 37 deletions

View file

@ -1,32 +1,50 @@
use std;
use std::io::{Read, Write};
use std::fmt::Write;
use std::thread;
use std::time::Duration;
pub struct Modem<R: Read, W: Write> {
is_connected: bool,
rx: R,
tx: W,
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> {
is_connected: bool,
rx: Rx<UART>,
tx: Tx<UART>,
}
#[derive(Debug)]
pub enum ModemError {
GprsAPConnectionError(String),
ATCommandError(String),
CommandError(String),
SetupError(String),
}
impl std::fmt::Display for ModemError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Self::GprsAPConnectionError(msg) => write!(f, "gprs connection error {}", msg),
Self::ATCommandError(msg) => write!(f, "AT command error {}", msg),
Self::CommandError(msg) => write!(f, "Modem command error {}", msg),
}
write!(f, "{:?}", self)
}
}
pub type Result<T> = std::result::Result<T, ModemError>;
impl<R: Read, W: Write> Modem<R, W> {
pub fn new(rx: R, tx: W) -> Self {
impl<UART: serial::Uart> Modem<UART> {
pub fn new(tx: Tx<UART>, rx: Rx<UART>) -> Self {
Self {
is_connected: false,
rx,
@ -37,27 +55,55 @@ impl<R: Read, W: Write> Modem<R, W> {
fn at_command(&mut self, cmd: &str) -> Result<String> {
let mut msg = "AT+".to_owned();
msg.push_str(cmd);
self.send_command(msg.to_owned())
self.send_command(&msg)
.map_err(|err| ModemError::ATCommandError(format!("{}", err)))
}
fn send_command(&mut self, cmd: String) -> Result<String> {
self.tx.write(cmd.as_bytes())
.map_err(|err| ModemError::CommandError(format!("{}", err)))?;
let mut response = String::new();
self.rx.read_to_string(&mut response)
.map_err(|err| ModemError::CommandError(format!("{}", err)))?;
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!("{} = {}", cmd, response);
println!("Reading {} bytes from serial RX ...", len);
}
let mut response = String::new();
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();
if cfg!(debug_assertions) {
println!("Received: {}", response);
}
Ok(response)
}
pub fn connect_to_gprs_ap(&mut self, apn: &str, username: &str, password: &str)-> Result<()> {
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);
self.at_command("CGATTCGATT=1")
.map(|_| {
println!("connecting to {} with {}:{}", apn, username, password);
.map(|response| {
println!("connected to {} with response: {}", apn, response);
self.is_connected = true;
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
})
.map_err(|err| ModemError::GprsAPConnectionError(format!("{}", err)))
}