mqtt continues
This commit is contained in:
parent
8d7d4d898d
commit
edf427dcb1
3 changed files with 79 additions and 59 deletions
|
@ -1,5 +1,5 @@
|
|||
# Rust often needs a bit of an extra main task stack size compared to C (the default is 3K)
|
||||
CONFIG_ESP_MAIN_TASK_STACK_SIZE=7000
|
||||
CONFIG_ESP_MAIN_TASK_STACK_SIZE=17000
|
||||
|
||||
# Use this to set FreeRTOS kernel tick frequency to 1000 Hz (100 Hz by default).
|
||||
# This allows to use 1 ms granuality for thread sleeps (10 ms by default).
|
||||
|
|
30
src/main.rs
30
src/main.rs
|
@ -6,6 +6,7 @@ mod command;
|
|||
use anyhow;
|
||||
use std::time::Duration;
|
||||
use std::thread;
|
||||
use esp_idf_hal::delay;
|
||||
use esp_idf_hal::prelude::*;
|
||||
use esp_idf_hal::peripherals::Peripherals;
|
||||
use esp_idf_hal::serial;
|
||||
|
@ -14,6 +15,7 @@ use mqtt::control::ConnectReturnCode;
|
|||
use mqtt::packet::{ConnackPacket, ConnectPacket, PublishPacketRef, QoSWithPacketIdentifier};
|
||||
use mqtt::{Decodable, Encodable, TopicName};
|
||||
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
esp_idf_sys::link_patches();
|
||||
|
||||
|
@ -55,7 +57,6 @@ fn main() -> anyhow::Result<()> {
|
|||
)?;
|
||||
}
|
||||
|
||||
loop {
|
||||
if mdm.is_gprs_attached()? {
|
||||
let _ = mdm.get_ip_addr()?;
|
||||
|
||||
|
@ -77,21 +78,40 @@ fn main() -> anyhow::Result<()> {
|
|||
let _ = conn.encode(&mut buf)?;
|
||||
|
||||
let _ = mdm.tcp_send(&mut buf)?;
|
||||
thread::sleep(Duration::from_millis(1000));
|
||||
drop(buf);
|
||||
|
||||
println!("+++++++++++++++++++++++++++++++++");
|
||||
let size = mdm.tcp_receive_reply_len()?;
|
||||
|
||||
let mut reply = vec![0 as u8; size];
|
||||
let received_size = mdm.tcp_receive(&mut reply)?;
|
||||
|
||||
println!("expected: {} / received: {}", size, received_size);
|
||||
println!("+++++++++++++++++++++++++++++++++");
|
||||
drop(reply);
|
||||
|
||||
let topic = TopicName::new("location")?;
|
||||
let message = "{\"lat\": 20, \"long\": 44}";
|
||||
let qos = QoSWithPacketIdentifier::Level0;
|
||||
|
||||
let publish_packet = PublishPacketRef::new(&topic, qos, message.as_bytes());
|
||||
let mut buf = Vec::new();
|
||||
publish_packet.encode(&mut buf)?;
|
||||
let _ = mdm.tcp_send(&mut buf)?;
|
||||
drop(buf);
|
||||
|
||||
thread::sleep(Duration::from_millis(300));
|
||||
let size = mdm.tcp_receive_reply_len()?;
|
||||
|
||||
let mut reply = vec![0 as u8; size];
|
||||
let received_size = mdm.tcp_receive(&mut reply)?;
|
||||
println!("expected: {} / received: {}", size, received_size);
|
||||
println!("+++++++++++++++++++++++++++++++++");
|
||||
println!("REPLY({}) = {}", reply.len(), reply.iter().map(|b| char::from(*b)).collect::<String>());
|
||||
println!("+++++++++++++++++++++++++++++++++");
|
||||
drop(reply);
|
||||
|
||||
let _ = mdm.tcp_close_connection()?;
|
||||
break;
|
||||
}
|
||||
println!("!!!!!!!!!!!!GPRS NOT ATTACHED!!!!!!!!!!!");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
42
src/modem.rs
42
src/modem.rs
|
@ -164,17 +164,14 @@ impl<UART: serial::Uart> Modem<UART> {
|
|||
}
|
||||
}
|
||||
|
||||
fn send(&mut self, b: u8) -> Result<()> {
|
||||
nb::block!(self.tx.write(b))
|
||||
.map_err(|_| ModemError::CommandError(format!("error writing {} to serial", b)))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn send_bytes(&mut self, payload: &[u8], eos: char) -> Result<()> {
|
||||
for b in payload.iter() {
|
||||
self.send(*b)?;
|
||||
nb::block!(self.tx.write(*b))
|
||||
.map_err(|_| ModemError::CommandError(format!("error writing {} to serial", b)))?;
|
||||
}
|
||||
self.send(eos as u8)?;
|
||||
nb::block!(self.tx.write(eos as u8))
|
||||
.map_err(|_| ModemError::CommandError(format!("error writing {} to serial", eos)))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -290,22 +287,25 @@ impl<UART: serial::Uart> Modem<UART> {
|
|||
pub fn tcp_receive(&mut self, buf: &mut [u8]) -> Result<usize> {
|
||||
let mut size = 0;
|
||||
loop {
|
||||
let reply = self.send_command(Command::tcp_receive(MAX_TCP_MANUAL_REPLY_SIZE))
|
||||
.map(|reply| {
|
||||
reply
|
||||
.split("\r\n")
|
||||
.filter(|line| line.len() > 2 && !line.contains("+CIPRXGET: 2,"))
|
||||
.next()
|
||||
.map(|line| line.chars().enumerate().map(|(idx, c)| buf[size + idx] = c as u8).count())
|
||||
let reply: usize = self.send_command(Command::tcp_receive(MAX_TCP_MANUAL_REPLY_SIZE))
|
||||
.map(|reply: String| {
|
||||
reply.lines()
|
||||
.map(|line| {
|
||||
if !line.starts_with("\r") && !line.contains("+CIPRXGET: 2,") && !line.contains("OK") {
|
||||
line.chars().enumerate().map(|(idx, c)| { buf[size + idx] = c as u8; }).count()
|
||||
}
|
||||
else {
|
||||
0
|
||||
}
|
||||
})
|
||||
.sum()
|
||||
})?;
|
||||
match reply {
|
||||
Some(0) | None => {
|
||||
if reply == 0 {
|
||||
break Ok(size)
|
||||
},
|
||||
Some(x) => {
|
||||
size += x;
|
||||
}
|
||||
else {
|
||||
size += reply;
|
||||
continue
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue