Struct MqttClient

Source
pub struct MqttClient<const N: usize> { /* private fields */ }

Implementations§

Source§

impl<const N: usize> MqttClient<N>

Source

pub fn new() -> Self

Examples found in repository?
examples/example.rs (line 9)
8fn main() {
9    let mut client: MqttClient<1024> = MqttClient::new();
10
11    let mut stream = TcpStream::connect(std::env::var("TINYMQTT_HOST").unwrap()).unwrap();
12    stream
13        .write_all(client.connect("12345", None).unwrap())
14        .unwrap();
15    stream.flush().unwrap();
16
17    let mut rx_bytes = [0; 1024];
18    let len = stream.read(&mut rx_bytes).unwrap();
19    client
20        .receive_packet(&rx_bytes[..len], |client, topic, data| {
21            println!("Received: {:?} {:?}", topic, std::str::from_utf8(data));
22        })
23        .unwrap();
24
25    stream
26        .write_all(client.publish("gots/test", b"test").unwrap())
27        .unwrap();
28
29    stream
30        .write_all(client.subscribe("zigbee2mqtt/Power Plug 3").unwrap())
31        .unwrap();
32
33    loop {
34        let len = stream.read(&mut rx_bytes).unwrap();
35        client
36            .receive_packet(&rx_bytes[..len], |client, topic, data| {
37                println!("Received: {:?} {:?}", topic, std::str::from_utf8(data));
38            })
39            .unwrap();
40    }
41}
Source

pub fn connect( &mut self, client_id: &str, username_password: Option<(&str, &str)>, ) -> MqttResult<&[u8]>

Examples found in repository?
examples/example.rs (line 13)
8fn main() {
9    let mut client: MqttClient<1024> = MqttClient::new();
10
11    let mut stream = TcpStream::connect(std::env::var("TINYMQTT_HOST").unwrap()).unwrap();
12    stream
13        .write_all(client.connect("12345", None).unwrap())
14        .unwrap();
15    stream.flush().unwrap();
16
17    let mut rx_bytes = [0; 1024];
18    let len = stream.read(&mut rx_bytes).unwrap();
19    client
20        .receive_packet(&rx_bytes[..len], |client, topic, data| {
21            println!("Received: {:?} {:?}", topic, std::str::from_utf8(data));
22        })
23        .unwrap();
24
25    stream
26        .write_all(client.publish("gots/test", b"test").unwrap())
27        .unwrap();
28
29    stream
30        .write_all(client.subscribe("zigbee2mqtt/Power Plug 3").unwrap())
31        .unwrap();
32
33    loop {
34        let len = stream.read(&mut rx_bytes).unwrap();
35        client
36            .receive_packet(&rx_bytes[..len], |client, topic, data| {
37                println!("Received: {:?} {:?}", topic, std::str::from_utf8(data));
38            })
39            .unwrap();
40    }
41}
Source

pub fn publish( &mut self, topic: &str, payload: &[u8], ) -> Result<&[u8], MqttError>

Examples found in repository?
examples/example.rs (line 26)
8fn main() {
9    let mut client: MqttClient<1024> = MqttClient::new();
10
11    let mut stream = TcpStream::connect(std::env::var("TINYMQTT_HOST").unwrap()).unwrap();
12    stream
13        .write_all(client.connect("12345", None).unwrap())
14        .unwrap();
15    stream.flush().unwrap();
16
17    let mut rx_bytes = [0; 1024];
18    let len = stream.read(&mut rx_bytes).unwrap();
19    client
20        .receive_packet(&rx_bytes[..len], |client, topic, data| {
21            println!("Received: {:?} {:?}", topic, std::str::from_utf8(data));
22        })
23        .unwrap();
24
25    stream
26        .write_all(client.publish("gots/test", b"test").unwrap())
27        .unwrap();
28
29    stream
30        .write_all(client.subscribe("zigbee2mqtt/Power Plug 3").unwrap())
31        .unwrap();
32
33    loop {
34        let len = stream.read(&mut rx_bytes).unwrap();
35        client
36            .receive_packet(&rx_bytes[..len], |client, topic, data| {
37                println!("Received: {:?} {:?}", topic, std::str::from_utf8(data));
38            })
39            .unwrap();
40    }
41}
Source

pub fn subscribe(&mut self, topic_filter: &str) -> Result<&[u8], MqttError>

Examples found in repository?
examples/example.rs (line 30)
8fn main() {
9    let mut client: MqttClient<1024> = MqttClient::new();
10
11    let mut stream = TcpStream::connect(std::env::var("TINYMQTT_HOST").unwrap()).unwrap();
12    stream
13        .write_all(client.connect("12345", None).unwrap())
14        .unwrap();
15    stream.flush().unwrap();
16
17    let mut rx_bytes = [0; 1024];
18    let len = stream.read(&mut rx_bytes).unwrap();
19    client
20        .receive_packet(&rx_bytes[..len], |client, topic, data| {
21            println!("Received: {:?} {:?}", topic, std::str::from_utf8(data));
22        })
23        .unwrap();
24
25    stream
26        .write_all(client.publish("gots/test", b"test").unwrap())
27        .unwrap();
28
29    stream
30        .write_all(client.subscribe("zigbee2mqtt/Power Plug 3").unwrap())
31        .unwrap();
32
33    loop {
34        let len = stream.read(&mut rx_bytes).unwrap();
35        client
36            .receive_packet(&rx_bytes[..len], |client, topic, data| {
37                println!("Received: {:?} {:?}", topic, std::str::from_utf8(data));
38            })
39            .unwrap();
40    }
41}
Source

pub fn unsubscribe(&mut self, topic_filter: &str) -> Result<&[u8], MqttError>

Source

pub fn receive_packet( &mut self, packet: &[u8], on_publish_rec: impl FnMut(&mut Self, &str, &[u8]), ) -> Result<MqttState, MqttError>

Examples found in repository?
examples/example.rs (lines 20-22)
8fn main() {
9    let mut client: MqttClient<1024> = MqttClient::new();
10
11    let mut stream = TcpStream::connect(std::env::var("TINYMQTT_HOST").unwrap()).unwrap();
12    stream
13        .write_all(client.connect("12345", None).unwrap())
14        .unwrap();
15    stream.flush().unwrap();
16
17    let mut rx_bytes = [0; 1024];
18    let len = stream.read(&mut rx_bytes).unwrap();
19    client
20        .receive_packet(&rx_bytes[..len], |client, topic, data| {
21            println!("Received: {:?} {:?}", topic, std::str::from_utf8(data));
22        })
23        .unwrap();
24
25    stream
26        .write_all(client.publish("gots/test", b"test").unwrap())
27        .unwrap();
28
29    stream
30        .write_all(client.subscribe("zigbee2mqtt/Power Plug 3").unwrap())
31        .unwrap();
32
33    loop {
34        let len = stream.read(&mut rx_bytes).unwrap();
35        client
36            .receive_packet(&rx_bytes[..len], |client, topic, data| {
37                println!("Received: {:?} {:?}", topic, std::str::from_utf8(data));
38            })
39            .unwrap();
40    }
41}
Source

pub fn is_connected(&self) -> bool

Auto Trait Implementations§

§

impl<const N: usize> Freeze for MqttClient<N>

§

impl<const N: usize> RefUnwindSafe for MqttClient<N>

§

impl<const N: usize> Send for MqttClient<N>

§

impl<const N: usize> Sync for MqttClient<N>

§

impl<const N: usize> Unpin for MqttClient<N>

§

impl<const N: usize> UnwindSafe for MqttClient<N>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.