move connecting and sending to func
This commit is contained in:
parent
09402bbf83
commit
9d536bdce9
1 changed files with 53 additions and 55 deletions
34
src/modem.rs
34
src/modem.rs
|
@ -483,7 +483,7 @@ impl<UART: serial::Uart> Modem<UART> {
|
||||||
conn.set_user_name(Some(username.to_string()));
|
conn.set_user_name(Some(username.to_string()));
|
||||||
conn.set_password(Some(password.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();
|
let _ = self.tcp_manual_send(&mut buf)?;
|
||||||
|
|
||||||
let reply = self.mqtt_receive_reply()?;
|
let reply = self.mqtt_receive_reply()?;
|
||||||
println!("mqtt decoded packet: ({:?})", reply);
|
println!("mqtt decoded packet: ({:?})", reply);
|
||||||
|
@ -503,9 +503,7 @@ impl<UART: serial::Uart> Modem<UART> {
|
||||||
message.as_bytes(),
|
message.as_bytes(),
|
||||||
);
|
);
|
||||||
let _ = packet.encode(&mut buf)?;
|
let _ = packet.encode(&mut buf)?;
|
||||||
println!("created mqtt publish packet ... ({})",
|
self.tcp_manual_send(&mut buf)?;
|
||||||
std::str::from_utf8(buf.as_slice()).unwrap_or(""));
|
|
||||||
self.tcp_manual_send(&mut buf).ok();
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -552,8 +550,6 @@ where
|
||||||
let mqtt_username = include_str!("../secret/username").trim();
|
let mqtt_username = include_str!("../secret/username").trim();
|
||||||
let mqtt_password = include_str!("../secret/password").trim();
|
let mqtt_password = include_str!("../secret/password").trim();
|
||||||
|
|
||||||
mdm.init(pwrkey, rst, power)?;
|
|
||||||
|
|
||||||
// thread::sleep(Duration::from_millis(500));
|
// thread::sleep(Duration::from_millis(500));
|
||||||
|
|
||||||
//println!("setting up client TLS cert");
|
//println!("setting up client TLS cert");
|
||||||
|
@ -564,15 +560,11 @@ 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);
|
fn start_sending(mdm: &mut Modem<serial::UART1>, mqtt_username: &str, mqtt_password: &str, receiver: Receiver<Msg>) -> anyhow::Result<()> {
|
||||||
// Retry 5 times to open a TCP connection, otherwise fail and wait for reboot.
|
|
||||||
let mut retries = 0;
|
|
||||||
|
|
||||||
loop {
|
|
||||||
if !mdm.is_gprs_attached()? {
|
if !mdm.is_gprs_attached()? {
|
||||||
let _ = mdm.gprs_attach_ap(crate::config::MTS)?;
|
let _ = mdm.gprs_attach_ap(crate::config::MTS)?;
|
||||||
|
let _ = 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
|
||||||
// sends data by AT+CIPSEND, if the server receives TCP data, it will give ACK message
|
// 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.
|
// to module, and the module will respond SEND OK.
|
||||||
|
@ -580,10 +572,9 @@ where
|
||||||
// Enables getting data from network manually.
|
// Enables getting data from network manually.
|
||||||
let _ = mdm.send("AT+CIPRXGET=1", "OK");
|
let _ = mdm.send("AT+CIPRXGET=1", "OK");
|
||||||
|
|
||||||
if let Err(_) = mdm.tcp_connect("51.158.66.64", 7887) {
|
for _ in 0..5 {
|
||||||
if retries < 5 {
|
if let Ok(_) = mdm.tcp_connect("51.158.66.64", 7887) {
|
||||||
retries += 1;
|
break
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -618,7 +609,14 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
mdm.init(pwrkey, rst, power)?;
|
||||||
|
let _ = mdm.echo(false)?;
|
||||||
|
let _ = start_sending(&mut mdm, mqtt_username, mqtt_password, receiver)?;
|
||||||
let _ = mdm.tcp_close_connection()?;
|
let _ = mdm.tcp_close_connection()?;
|
||||||
}
|
|
||||||
}
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue