clear Rx buffer before TCP connect

and ofc some other things too :)
This commit is contained in:
Vladan Popovic 2023-02-08 01:24:54 +01:00
parent 8308bdb9a0
commit b30acab823
5 changed files with 59 additions and 18 deletions

View File

@ -19,7 +19,7 @@ esp-idf-hal = "0.37.4"
esp-idf-sys = { version = "0.31.5", features = ["binstart", "native"] } esp-idf-sys = { version = "0.31.5", features = ["binstart", "native"] }
mqtt-protocol = "0.11.2" mqtt-protocol = "0.11.2"
nb = "1.0.0" nb = "1.0.0"
ublox = { git = "https://github.com/lkolbly/ublox.git", branch = "master" } ublox = "0.4.2"
[build-dependencies] [build-dependencies]
embuild = "0.29" embuild = "0.29"

View File

@ -126,7 +126,7 @@ pub fn main<T: Sync + Send>(
mode: UartMode::new(DataBits::Eight, Parity::None, StopBits::One), mode: UartMode::new(DataBits::Eight, Parity::None, StopBits::One),
baud_rate: 9600, baud_rate: 9600,
in_proto_mask: InProtoMask::all(), in_proto_mask: InProtoMask::all(),
out_proto_mask: OutProtoMask::UBLOX, out_proto_mask: OutProtoMask::UBOX,
flags: 0, flags: 0,
reserved5: 0, reserved5: 0,
} }

View File

@ -2,7 +2,6 @@ mod accel;
mod config; mod config;
#[allow(dead_code)] #[allow(dead_code)]
mod command; mod command;
mod gps;
mod modem; mod modem;
mod serial; mod serial;
mod types; mod types;
@ -43,7 +42,7 @@ fn main() -> anyhow::Result<()> {
// let _ = gps::main(gps_tx, gps_rx, gps_uart, gps_sender)?; // let _ = gps::main(gps_tx, gps_rx, gps_uart, gps_sender)?;
// threads.push(thread::spawn(move || gps::main(gps_rx, gps_tx, gps_uart, gps_sender))); // threads.push(thread::spawn(move || gps::main(gps_rx, gps_tx, gps_uart, gps_sender)));
//thread::sleep(Duration::from_millis(1000)); thread::sleep(Duration::from_millis(1000));
threads.push(thread::spawn(move || accel::main(accel_sender))); threads.push(thread::spawn(move || accel::main(accel_sender)));
let _ = modem::main(modem_rx, modem_tx, modem_uart, modem_pwrkey, modem_rst, modem_power, receiver)?; let _ = modem::main(modem_rx, modem_tx, modem_uart, modem_pwrkey, modem_rst, modem_power, receiver)?;

View File

@ -67,10 +67,10 @@ 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(100)); thread::sleep(Duration::from_millis(1500));
pwrkey.set_low().map_err(|_| ModemError::SetupError("Error setting PWRKEY to low.".to_string()))?; pwrkey.set_low().map_err(|_| ModemError::SetupError("Error setting PWRKEY to low.".to_string()))?;
thread::sleep(Duration::from_millis(1000)); thread::sleep(Duration::from_millis(1000));
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()))?;
@ -122,14 +122,24 @@ impl<UART: serial::Uart> Modem<UART> {
} }
fn send_command(&mut self, cmd: Command) -> Result<String> { fn send_command(&mut self, cmd: Command) -> Result<String> {
if let Some(contains) = cmd.contains {
self.send(&cmd.text, &contains)
} else {
self.send(&cmd.text, "")
}
}
fn send(&mut self, at_command: &str, contains: &str) -> Result<String> {
println!("-----------------------------------------------------------"); println!("-----------------------------------------------------------");
println!("Sending {} ...", cmd.text); println!("Sending {} ...", at_command);
let _ = nb::block!(self.serial let _ = nb::block!(self.serial
.write_bytes(&[cmd.text.as_bytes(), &['\r' as u8]].concat())) .write_bytes(&[at_command.as_bytes(), &['\r' as u8]].concat()))
.map_err(|_| ModemError::SendDataError(format!("Error in send_command({})", cmd.text)))?; .map_err(|_| ModemError::SendDataError(format!("Error in send_command({})", at_command)))?;
self.command_read_response(cmd.contains) let contains_opt = if contains == "" { None } else { Some(contains.to_string()) };
self.command_read_response(contains_opt)
} }
fn handle_prompt(&mut self) -> Result<()> { fn handle_prompt(&mut self) -> Result<()> {
@ -247,7 +257,8 @@ impl<UART: serial::Uart> Modem<UART> {
} }
pub fn tcp_connect(&mut self, addr: &str, port: u16) -> Result<()> { pub fn tcp_connect(&mut self, addr: &str, port: u16) -> Result<()> {
let _ = self.send_command(Command::tcp_connect(addr, port))?; let at_command = format!("AT+CIPSTART=\"TCP\",\"{}\",\"{}\"", addr, port);
let _ = self.send(&at_command, "CONNECT OK");
for _ in 0..3 { for _ in 0..3 {
if let Ok(reply) = self.command_read_response(Some("CONNECT OK".to_string())) { if let Ok(reply) = self.command_read_response(Some("CONNECT OK".to_string())) {
println!("TCP connect replied with {}", reply); println!("TCP connect replied with {}", reply);
@ -515,6 +526,9 @@ pub fn main<T: Sync + Send>(
//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\\")?;
// Retry 5 times to open a TCP connection, otherwise fail and wait for reboot.
let mut retries = 0;
loop { loop {
if !mdm.is_gprs_attached()? { if !mdm.is_gprs_attached()? {
let _ = mdm.gprs_attach_ap( let _ = mdm.gprs_attach_ap(
@ -526,13 +540,21 @@ pub fn main<T: Sync + Send>(
if let Ok(()) = mdm.try_connect_gprs() { if let Ok(()) = mdm.try_connect_gprs() {
let device_id = "c36a72df-5bd6-4f9b-995d-059433bc3267"; let device_id = "c36a72df-5bd6-4f9b-995d-059433bc3267";
let _ = mdm.tcp_set_quick_mode(false); // When command AT+CIPQSEND=0, it is in normal sending mode. In this mode, after user
let _ = mdm.tcp_set_manual_receive(true); // sends data by AT+CIPSEND, if the server receives TCP data, it will give ACK message
// to module, and the module will respond SEND OK.
let _ = mdm.send("AT+CIPQSEND=0", "OK");
// Enables getting data from network manually.
let _ = mdm.send("AT+CIPRXGET=1", "OK");
if let Err(_) = mdm.tcp_connect("51.158.66.64", 7887) { if let Err(_) = mdm.tcp_connect("51.158.66.64", 7887) {
if retries < 5 {
retries += 1;
continue; continue;
} }
thread::sleep(Duration::from_secs(1)); }
thread::sleep(Duration::from_millis(500));
mdm.serial.clear();
let _ = mdm.mqtt_connect(device_id)?; let _ = mdm.mqtt_connect(device_id)?;
@ -551,7 +573,7 @@ pub fn main<T: Sync + Send>(
err_count = 0; err_count = 0;
} }
Err(e) => { Err(e) => {
if err_count < 10 { if err_count < 5 {
err_count += 1; err_count += 1;
println!("received error {} | NOT sending to mqtt ...", e); println!("received error {} | NOT sending to mqtt ...", e);
} }
@ -561,7 +583,6 @@ pub fn main<T: Sync + Send>(
} }
} }
}; };
let _ = mdm.tcp_close_connection()?; let _ = mdm.tcp_close_connection()?;
} }
} }

View File

@ -54,7 +54,7 @@ impl<UART: serial::Uart> SerialIO<UART> {
} }
} }
fn read_bytes(&mut self, buf: &mut [u8]) -> Result<usize> { pub fn read_bytes(&mut self, buf: &mut [u8]) -> Result<usize> {
let mut started_reading = false; let mut started_reading = false;
let mut count = 0; let mut count = 0;
let mut retries = 0; let mut retries = 0;
@ -85,6 +85,27 @@ impl<UART: serial::Uart> SerialIO<UART> {
Err(nb::Error::Other(SerialError::ReadError("Rx buffer empty.".to_string()))) Err(nb::Error::Other(SerialError::ReadError("Rx buffer empty.".to_string())))
} }
} }
pub fn clear(&mut self) {
let mut started_reading = false;
let mut retries = 0;
loop {
match self.rx.read() {
Ok(_) => {
started_reading = true;
},
Err(nb::Error::WouldBlock) => {
if started_reading || retries > READ_MAX_RETRIES { break; }
else {
thread::sleep(Duration::from_millis(READ_WAIT_TIME));
retries += 1;
}
},
Err(nb::Error::Other(err)) => println!("Serial read error :: {:?}", err),
}
}
}
} }
impl<UART: serial::Uart> io::Read for SerialIO<UART> { impl<UART: serial::Uart> io::Read for SerialIO<UART> {