mqtt works ... connect + publish

This commit is contained in:
Vladan Popovic 2022-07-04 17:04:09 +02:00
parent a215c628a7
commit d023f5db76
3 changed files with 202 additions and 87 deletions

View file

@ -151,23 +151,7 @@ impl Command {
} }
} }
pub fn http_enable_ssl() -> Command { pub fn http_set_url(url: &str) -> Command {
Command {
text: "AT+HTTPSSL=1".to_string(),
timeout: Duration::from_millis(3000),
contains: Some("OK".to_string()),
}
}
pub fn http_disable_ssl() -> Command {
Command {
text: "AT+HTTPSSL=0".to_string(),
timeout: Duration::from_millis(3000),
contains: Some("OK".to_string()),
}
}
pub fn http_init_url(url: &str) -> Command {
Command { Command {
text: format!("AT+HTTPPARA=\"URL\",\"{}\"", url), text: format!("AT+HTTPPARA=\"URL\",\"{}\"", url),
timeout: Duration::from_millis(3000), timeout: Duration::from_millis(3000),
@ -199,6 +183,14 @@ impl Command {
} }
} }
pub fn http_post() -> Command {
Command {
text: "AT+HTTPACTION=1".to_string(),
timeout: Duration::from_millis(10000),
contains: Some("HTTPACTION".to_string()),
}
}
pub fn http_set_content(content: &str) -> Command { pub fn http_set_content(content: &str) -> Command {
Command { Command {
text: format!("AT+HTTPPARA=\"CONTENT\",\"{}\"", content), text: format!("AT+HTTPPARA=\"CONTENT\",\"{}\"", content),
@ -207,6 +199,14 @@ impl Command {
} }
} }
pub fn http_set_redirect(redirect: bool) -> Command {
Command {
text: format!("AT+HTTPPARA=\"REDIR\",\"{}\"", redirect as u8),
timeout: Duration::from_millis(3000),
contains: Some("OK".to_string()),
}
}
pub fn http_post_len(size: usize, time: usize) -> Command { pub fn http_post_len(size: usize, time: usize) -> Command {
Command { Command {
text: format!("AT+HTTPDATA={},{}", size, time), text: format!("AT+HTTPDATA={},{}", size, time),
@ -215,15 +215,7 @@ impl Command {
} }
} }
pub fn http_post() -> Command { pub fn http_read_response() -> Command {
Command {
text: "AT+HTTPACTION=1".to_string(),
timeout: Duration::from_millis(10000),
contains: Some("HTTPACTION".to_string()),
}
}
pub fn http_response() -> Command {
Command { Command {
text: "AT+HTTPREAD".to_string(), text: "AT+HTTPREAD".to_string(),
timeout: Duration::from_millis(3000), timeout: Duration::from_millis(3000),
@ -243,7 +235,7 @@ impl Command {
Command { Command {
text: "AT".to_string(), text: "AT".to_string(),
timeout: Duration::from_millis(3000), timeout: Duration::from_millis(3000),
contains: Some("OK".to_string()), contains: Some("+CIEV".to_string()),
} }
} }
@ -335,9 +327,9 @@ impl Command {
} }
} }
pub fn tcp_set_manual_receive() -> Command { pub fn tcp_set_manual_receive(is_manual: bool) -> Command {
Command { Command {
text: "AT+CIPRXGET=1".to_string(), text: format!("AT+CIPRXGET={}", is_manual as u8),
timeout: Duration::from_millis(3000), timeout: Duration::from_millis(3000),
contains: Some("OK".to_string()), contains: Some("OK".to_string()),
} }
@ -390,4 +382,52 @@ impl Command {
contains: Some("OK".to_string()), contains: Some("OK".to_string()),
} }
} }
pub fn ssl_set_client_cert(path: &str, password: &str) -> Command {
Command {
text: format!("AT+SSLSETCERT={},{}", path, password),
timeout: Duration::from_millis(2000),
contains: Some("SSLSETCERT".to_string()),
}
}
pub fn ssl_set_root_cert(path: &str, size: usize) -> Command {
Command {
text: format!("AT+SSLSETROOT={},{}", path, size),
timeout: Duration::from_millis(2000),
contains: Some("SSLSETCERT".to_string()),
}
}
pub fn fs_file_create(path: &str) -> Command {
Command {
text: format!("AT+FSCREATE={}", path),
timeout: Duration::from_millis(2000),
contains: Some("OK".to_string()),
}
}
pub fn fs_file_write(path: &str, append: bool, size: usize, input_time_sec: usize) -> Command {
Command {
text: format!("AT+FSWRITE={},{},{},{}", path, append as u8, size, input_time_sec),
timeout: Duration::from_millis(20000),
contains: Some("OK".to_string()),
}
}
pub fn fs_list(path: &str) -> Command {
Command {
text: format!("AT+FSLS={}", path),
timeout: Duration::from_millis(2000),
contains: Some("OK".to_string()),
}
}
pub fn fs_free_size() -> Command {
Command {
text: "AT+FSMEM".to_string(),
timeout: Duration::from_millis(2000),
contains: Some("OK".to_string()),
}
}
} }

View file

@ -6,13 +6,12 @@ mod command;
use anyhow; use anyhow;
use std::time::Duration; use std::time::Duration;
use std::thread; use std::thread;
use esp_idf_hal::delay;
use esp_idf_hal::prelude::*; use esp_idf_hal::prelude::*;
use esp_idf_hal::peripherals::Peripherals; use esp_idf_hal::peripherals::Peripherals;
use esp_idf_hal::serial; use esp_idf_hal::serial;
use mqtt::control::ConnectReturnCode; use mqtt::control::ConnectReturnCode;
use mqtt::packet::{ConnackPacket, ConnectPacket, PublishPacketRef, QoSWithPacketIdentifier}; use mqtt::packet::{ConnackPacket, ConnectPacket, PublishPacket, QoSWithPacketIdentifier};
use mqtt::{Decodable, Encodable, TopicName}; use mqtt::{Decodable, Encodable, TopicName};
@ -59,7 +58,13 @@ fn main() -> anyhow::Result<()> {
thread::sleep(Duration::from_millis(2000)); thread::sleep(Duration::from_millis(2000));
let _ = mdm.chip_info()?; //println!("setting up client TLS cert");
//let client_cert = include_bytes!("../certs/full-bin.p12");
//let client_cert_path = "C:\\USER\\fullchain.pem";
//let _ = mdm.upload_cert(client_cert_path, client_cert)?;
//let _ = mdm.ssl_set_client_cert(client_cert_path, "t")?;
//let _ = mdm.fs_list("C:\\USER\\")?;
loop { loop {
if mdm.is_gprs_attached()? { if mdm.is_gprs_attached()? {
@ -73,26 +78,54 @@ fn main() -> anyhow::Result<()> {
} }
} }
let _ = mdm.location()?; //println!("connecting to server!");
let _ = mdm.ssl_opt()?;
println!("connecting to server!");
//if !mdm.tcp_is_ssl_enabled()? { //if !mdm.tcp_is_ssl_enabled()? {
// let _ = mdm.tcp_ssl_enable()?; // let _ = mdm.tcp_ssl_enable()?;
//} //}
//if mdm.tcp_is_ssl_enabled()? { if mdm.tcp_is_ssl_enabled()? {
// let _ = mdm.tcp_ssl_disable()?; let _ = mdm.tcp_ssl_disable()?;
//} }
//
let client_id = "c36a72df-5bd6-4f9b-995d-059433bc3267";
let message = "{\"lat\": 20.475370, \"long\": 44.747224}"; let message = "{\"lat\": 20.475370, \"long\": 44.747224}";
let url = "https://a.tracker.called.quest";
//let url = "https://rest.iot.fr-par.scw.cloud"; let _ = mdm.tcp_set_quick_mode(false);
let token = "eyJOZXRJRCI6ImUwYTc5YzM2LWExZjYtNDNiMC1hMGY3LWE2YTk1OGI3Zjk1ZCIsIk5ldEtleSI6IjZhZGU2ZWZkLThiMGYtNDQ3ZC1hNWY5LThkNDJjNDk4NDQ3MSJ9"; let _ = mdm.tcp_set_manual_receive(true);
let reply = mdm.http_post(url, token, message.as_bytes())?;
println!("+++++++++++++++++++++++++++++++++"); let _ = mdm.tcp_connect("51.158.66.64", 1883)?;
println!("REPLY({}) = {}", reply.len(), reply);
println!("+++++++++++++++++++++++++++++++++"); let mut buf = Vec::new();
let _ = mdm.http_close()?;
let mut conn = ConnectPacket::new(client_id);
conn.set_clean_session(true);
conn.set_keep_alive(0);
let _ = conn.encode(&mut buf)?;
let _ = mdm.tcp_send(&mut buf)?;
buf.clear();
thread::sleep(Duration::from_millis(1000));
let size = mdm.tcp_receive_reply_len()?;
let mut reply = vec![0 as u8; size];
let _ = mdm.tcp_receive(&mut reply);
println!("TCP Received: {}", reply.iter().map(|b| char::from(*b)).collect::<String>());
println!("sending publish packet");
let packet = PublishPacket::new(
TopicName::new("bajsevi/test").unwrap(),
QoSWithPacketIdentifier::Level0,
message.as_bytes(),
);
let _ = packet.encode(&mut buf)?;
let _ = mdm.tcp_send(&mut buf)?;
buf.clear();
let size = mdm.tcp_receive_reply_len()?;
let mut reply = vec![0 as u8; size];
let _ = mdm.tcp_receive(&mut reply);
println!("TCP Received: {}", reply.iter().map(|b| char::from(*b)).collect::<String>());
let _ = mdm.tcp_close_connection()?;
break break
} }

View file

@ -140,12 +140,10 @@ impl<UART: serial::Uart> Modem<UART> {
Ok(()) Ok(())
} }
/// Reads the serial RX until a \\n char is encoutered, or a timeout is reached. The timeout is /// Reads the serial RX until the `contains` string is encoutered if `contains` is Some(s), if
/// provided on input via the `timeout` argument. The first argument `contains` is checked /// None, then the first line is returned. If a timeout is reached. The timeout is provided on
/// against a line in the response, if it's there the reading stops. /// input via the `timeout` argument. The first argument `contains` is checked against every
/// /// line in the response.
/// If `contains` is `None`, the first line only is returned in the response. If it's
/// `Some(match_txt)`, then the end of the response is matched against `match_txt`.
fn read_response(&mut self, contains: Option<String>, timeout: Duration) -> Result<String> { fn read_response(&mut self, contains: Option<String>, timeout: Duration) -> Result<String> {
let mut response = String::new(); let mut response = String::new();
let start = Instant::now(); let start = Instant::now();
@ -166,7 +164,7 @@ impl<UART: serial::Uart> Modem<UART> {
#[inline(always)] #[inline(always)]
fn send_bytes(&mut self, payload: &[u8], eos: Option<u8>) -> Result<()> { fn send_bytes(&mut self, payload: &[u8], eos: Option<u8>) -> Result<()> {
self.rx.clear(); //self.rx.clear();
for b in payload.iter() { for b in payload.iter() {
nb::block!(self.tx.write(*b)) nb::block!(self.tx.write(*b))
.map_err(|_| ModemError::CommandError(format!("error writing {} to serial", b)))?; .map_err(|_| ModemError::CommandError(format!("error writing {} to serial", b)))?;
@ -188,19 +186,13 @@ impl<UART: serial::Uart> Modem<UART> {
.map(char::from) .map(char::from)
.take_while(|c| *c != '>').collect(); .take_while(|c| *c != '>').collect();
if send_request == "" { if send_request != "\r\n" {
println!("{:?}", send_request.as_bytes());
return Err(ModemError::SendDataError); return Err(ModemError::SendDataError);
} }
self.send_bytes(buf, Some(26))?; // 26_u8 = Ctrl+z - to end sending data self.send_bytes(buf, Some(26))?; // 26_u8 = Ctrl+z - to end sending data
let _ = self.read_response(Some("DATA ACCEPT".to_string()), Duration::from_millis(3000)); self.read_response(Some("SEND OK".to_string()), Duration::from_millis(3000))
let res = self.send_command(Command {
text: "AT+CIPACK".to_string(),
contains: Some("OK".to_string()),
timeout: Duration::from_millis(3000),
})?;
Ok(res)
} }
pub fn gprs_status(&mut self) -> Result<String> { pub fn gprs_status(&mut self) -> Result<String> {
@ -257,8 +249,8 @@ impl<UART: serial::Uart> Modem<UART> {
Ok(()) Ok(())
} }
pub fn tcp_set_manual_receive(&mut self) -> Result<()> { pub fn tcp_set_manual_receive(&mut self, is_manual: bool) -> Result<()> {
self.send_command(Command::tcp_set_manual_receive())?; self.send_command(Command::tcp_set_manual_receive(is_manual))?;
Ok(()) Ok(())
} }
@ -289,25 +281,22 @@ impl<UART: serial::Uart> Modem<UART> {
pub fn tcp_receive(&mut self, buf: &mut [u8]) -> Result<usize> { pub fn tcp_receive(&mut self, buf: &mut [u8]) -> Result<usize> {
let mut size = 0; let mut size = 0;
loop { loop {
let reply: usize = self.send_command(Command::tcp_receive(MAX_TCP_MANUAL_REPLY_SIZE)) let reply = self.send_command(Command::tcp_receive(MAX_TCP_MANUAL_REPLY_SIZE))
.map(|reply: String| { .map(|reply| {
reply.lines() reply
.map(|line| { .split("\r\n")
if !line.starts_with("\r") && !line.contains("+CIPRXGET: 2,") && !line.contains("OK") { .filter(|line| line.len() > 2 && !line.contains("+CIPRXGET: 2,"))
line.chars().enumerate().map(|(idx, c)| { buf[size + idx] = c as u8; }).count() .next()
} .map(|line| line.chars().enumerate().map(|(idx, c)| buf[size + idx] = c as u8).count())
else {
0
}
})
.sum()
})?; })?;
if reply == 0 { match reply {
Some(0) | None => {
break Ok(size) break Ok(size)
} },
else { Some(x) => {
size += reply; size += x;
continue continue
},
} }
} }
} }
@ -318,16 +307,26 @@ impl<UART: serial::Uart> Modem<UART> {
pub fn http_post(&mut self, url: &str, token: &str, content: &[u8]) -> Result<String> { pub fn http_post(&mut self, url: &str, token: &str, content: &[u8]) -> Result<String> {
let _ = self.send_command(Command::http_init()); let _ = self.send_command(Command::http_init());
let _ = self.send_command(Command::http_set_ssl(true));
let _ = self.send_command(Command::http_set_cid()); let _ = self.send_command(Command::http_set_cid());
let _ = self.send_command(Command::http_init_url(url)); let _ = self.send_command(Command::http_set_url(url));
let _ = self.send_command(Command::http_set_header("X-Secret", token)); let _ = self.send_command(Command::http_set_header("X-Secret", token));
let _ = self.send_command(Command::http_set_header("X-Topic", "device-dev")); let _ = self.send_command(Command::http_set_header("X-Topic", "device-dev"));
let _ = self.send_command(Command::http_set_content("application/json")); let _ = self.send_command(Command::http_set_content("application/json"));
let _ = self.send_command(Command::http_set_ssl(true));
let _ = self.send_command(Command::http_post_len(content.len(), 100000)); let _ = self.send_command(Command::http_post_len(content.len(), 100000));
let _ = self.send_bytes(content, Some(26)); let _ = self.send_bytes(content, Some(26));
let _ = self.send_command(Command::http_post()); let _ = self.send_command(Command::http_post());
self.send_command(Command::http_response()) self.send_command(Command::http_read_response())
}
pub fn http_get(&mut self, url: &str) -> Result<String> {
let _ = self.send_command(Command::http_init());
let _ = self.send_command(Command::http_set_cid());
let _ = self.send_command(Command::http_set_url(url));
let _ = self.send_command(Command::http_set_redirect(true));
let _ = self.send_command(Command::http_set_ssl(true));
let _ = self.send_command(Command::http_get());
self.send_command(Command::http_read_response())
} }
pub fn http_close(&mut self) -> Result<()> { pub fn http_close(&mut self) -> Result<()> {
@ -353,4 +352,47 @@ impl<UART: serial::Uart> Modem<UART> {
let _ = self.send_command(Command::ssl_opt())?; let _ = self.send_command(Command::ssl_opt())?;
Ok(()) Ok(())
} }
fn file_write(&mut self, buf: &[u8], path: &str, append: bool, input_time_sec: usize) -> Result<()> {
let cmd = Command::fs_file_write(path, append, buf.len(), input_time_sec);
let _ = self.send_bytes(cmd.text.as_bytes(), Some('\r' as u8))?;
let send_request: String = self.rx.reset(Duration::from_millis(3000))
.map(char::from)
.take_while(|c| *c != '>').collect();
if send_request == "" {
return Err(ModemError::SendDataError);
}
self.send_bytes(buf, None)?;
let _ = self.read_response(Some("OK".to_string()), Duration::from_millis(3000));
Ok(())
}
pub fn upload_cert(&mut self, path: &str, cert: &[u8]) -> Result<()> {
let _ = self.send_command(Command::fs_file_create(path))?;
let _ = self.file_write(cert, path, false, 20000)?;
Ok(())
}
pub fn fs_list(&mut self, path: &str) -> Result<()> {
let _ = self.send_command(Command::fs_list(path))?;
Ok(())
}
pub fn fs_free_space(&mut self) -> Result<()> {
let _ = self.send_command(Command::fs_free_size())?;
Ok(())
}
pub fn ssl_set_client_cert(&mut self, path: &str, password: &str) -> Result<()> {
let _ = self.send_command(Command::ssl_set_client_cert(path, password))?;
Ok(())
}
pub fn ssl_set_root_cert(&mut self, path: &str, filesize: usize) -> Result<()> {
let _ = self.send_command(Command::ssl_set_root_cert(path, filesize))?;
Ok(())
}
} }