diff --git a/src/modem.rs b/src/modem.rs index b468232..36f4dd3 100644 --- a/src/modem.rs +++ b/src/modem.rs @@ -178,21 +178,24 @@ impl Modem { self.read_response(cmd.contains, cmd.timeout) } - fn send_command_data(&mut self, cmd: Command, payload: &str) -> Result { - println!("-----------------------------------------------------------"); - println!("Sending {} ...", cmd.text); - let _ = self.send_bytes(cmd.text.as_bytes())?; - - let prompt: String = self.rx.reset(cmd.timeout) - .map(|b| char::from(b)) - .take_while(|c| *c != ' ') - .collect(); - - if prompt != ">".to_string() { - println!("invalid prompt: {}", prompt); + fn send_data(&mut self, payload: &str) -> Result { + let _ = self.send_bytes("AT+CIPSEND".as_bytes())?; + for c in self.rx.reset(Duration::from_millis(2000)).map(char::from) { + println!("{}", c); + if c == '>' { + for b in payload.as_bytes() { + self.send(*b)?; + } + self.send(26)?; + return self.read_response(Some("DATA ACCEPT".to_string()), Duration::from_millis(3000)); + } } - let _ = self.send_bytes(payload.as_bytes())?; - self.read_response(cmd.contains, cmd.timeout) + self.send_command(Command { + text: "AT+CIPACK".to_string(), + contains: Some("OK".to_string()), + timeout: Duration::from_millis(3000), + })?; + Ok("OK".to_string()) } pub fn get_ip_addr(&mut self) -> Result { @@ -259,8 +262,7 @@ impl Modem { } pub fn tcp_send(&mut self, payload: &str) -> Result<()> { - self.send_command(Command::tcp_send_size(payload.len()))?; - self.send_command_data(Command::tcp_send(), payload)?; + self.send_data(payload)?; Ok(()) } }