pub struct Client<'a> { /* private fields */ }
Expand description
W5500 MQTT client.
§Topic Arguments
In the interest of minimal code size topic arguments are not validated, you must adhere to the topic rules yourself, see Topic Names and Topic Filters. The MQTT server will disconnect if an invalid topic is used.
Implementations§
Source§impl<'a> Client<'a>
impl<'a> Client<'a>
Sourcepub fn new(sn: Sn, src_port: u16, server: SocketAddrV4) -> Self
pub fn new(sn: Sn, src_port: u16, server: SocketAddrV4) -> Self
Create a new MQTT client.
§Arguments
sn
- The socket number to use for MQTT.src_port
- The MQTT source port, this is typicallySRC_PORT
.server
- The IP address and port for the MQTT server.
§Example
use w5500_mqtt::{
ll::{
net::{Ipv4Addr, SocketAddrV4},
Sn,
},
Client, DST_PORT, SRC_PORT,
};
let client: Client = Client::new(
Sn::Sn2,
SRC_PORT,
SocketAddrV4::new(Ipv4Addr::new(192, 168, 5, 6), DST_PORT),
);
Sourcepub fn set_client_id(&mut self, client_id: ClientId<'a>)
pub fn set_client_id(&mut self, client_id: ClientId<'a>)
Set the MQTT client ID.
This will only apply for new connections.
Call this after new
, before calling process
.
§Example
use w5500_mqtt::{
ll::{
net::{Ipv4Addr, SocketAddrV4},
Sn,
},
Client, ClientId, DST_PORT, SRC_PORT,
};
let mut client: Client = Client::new(
Sn::Sn2,
SRC_PORT,
SocketAddrV4::new(Ipv4Addr::new(192, 168, 5, 6), DST_PORT),
);
const CLIENT_ID: ClientId = ClientId::new_unwrapped("MYDEVICE");
client.set_client_id(CLIENT_ID);
Sourcepub fn process<'w, W5500: Registers>(
&mut self,
w5500: &'w mut W5500,
monotonic_secs: u32,
) -> Result<Event<W5500::Error, TcpReader<'w, W5500>>, Error<W5500::Error>>
pub fn process<'w, W5500: Registers>( &mut self, w5500: &'w mut W5500, monotonic_secs: u32, ) -> Result<Event<W5500::Error, TcpReader<'w, W5500>>, Error<W5500::Error>>
Process the MQTT client.
This should be called repeatedly until it returns:
Err(_)
What to do upon errors is up to you. Callingprocess
again will re-initialize the client.Ok(Event::CallAfter(seconds))
Call this method again after the number of seconds indicated.Ok(Event::None)
The client is idle; you can callsubscribe
andpublish
.
This should also be called when there is a pending socket interrupt.
§Example
use w5500_mqtt::{Client, Error, Event};
fn my_rtos_task(client: &mut Client, w5500: &mut MyW5500) -> Result<(), Error<W5500Error>> {
loop {
match client.process(w5500, monotonic_secs()) {
Ok(Event::ConnAck) => {
client.subscribe(w5500, "/demo/topic/#")?;
}
Ok(Event::CallAfter(secs)) => {
spawn_at_this_many_seconds_in_the_future(secs);
break;
}
Ok(Event::Publish(mut reader)) => {
let mut topic_buf: [u8; 32] = [0; 32];
let mut payload_buf: [u8; 32] = [0; 32];
let topic_len: u16 = reader.read_topic(&mut topic_buf)?;
let payload_len: u16 = reader.read_payload(&mut payload_buf)?;
reader.done()?;
// do something with the topic and payload
}
Ok(Event::SubAck(ack)) => {
// this does not handle failed subscriptions
log::info!("SubAck {:?}", ack)
}
Ok(Event::UnSubAck(ack)) => {
log::info!("UnsubAck {:?}", ack)
}
Ok(Event::None) => break,
Err(e) => {
log::error!("oh no, an error! {e:?}");
// try again in a minute
spawn_at_this_many_seconds_in_the_future(60);
break;
}
}
}
Ok(())
}
Sourcepub fn is_connected(&self) -> bool
pub fn is_connected(&self) -> bool
Returns true
if the MQTT client is connected.
Sourcepub fn publish<W5500: Registers>(
&mut self,
w5500: &mut W5500,
topic: &str,
payload: &[u8],
) -> Result<(), Error<W5500::Error>>
pub fn publish<W5500: Registers>( &mut self, w5500: &mut W5500, topic: &str, payload: &[u8], ) -> Result<(), Error<W5500::Error>>
Publish data to the MQTT broker.
§Examples
use w5500_mqtt::{
ll::{
net::{Ipv4Addr, SocketAddrV4},
Sn,
},
Client, ClientId, Event, DST_PORT, SRC_PORT,
};
let mut client: Client = Client::new(
Sn::Sn2,
SRC_PORT,
SocketAddrV4::new(Ipv4Addr::new(192, 168, 5, 6), DST_PORT),
);
// wait for a connection or die trying
while !matches!(client.process(&mut w5500, monotonic_secs())?, Event::None) {}
client.publish(&mut w5500, "topic", b"data")?;
Sourcepub fn subscribe<W5500: Registers>(
&mut self,
w5500: &mut W5500,
filter: &str,
) -> Result<u16, Error<W5500::Error>>
pub fn subscribe<W5500: Registers>( &mut self, w5500: &mut W5500, filter: &str, ) -> Result<u16, Error<W5500::Error>>
Subscribe to a topic.
§Return Value
The return value is a u16
packet identifier.
This can be compared to Event::SubAck
to determine when the
subscription is active.
The packet identifier is zero (invalid) when filter
is empty.
§Examples
use w5500_mqtt::{
ll::{
net::{Ipv4Addr, SocketAddrV4},
Sn,
},
Client, ClientId, Event, DST_PORT, SRC_PORT,
};
let mut client: Client = Client::new(
Sn::Sn2,
SRC_PORT,
SocketAddrV4::new(Ipv4Addr::new(192, 168, 5, 6), DST_PORT),
);
// wait for a connection or die trying
while !matches!(client.process(&mut w5500, monotonic_secs())?, Event::None) {}
client.subscribe(&mut w5500, "topic")?;
Sourcepub fn unsubscribe<W5500: Registers>(
&mut self,
w5500: &mut W5500,
filter: &str,
) -> Result<u16, Error<W5500::Error>>
pub fn unsubscribe<W5500: Registers>( &mut self, w5500: &mut W5500, filter: &str, ) -> Result<u16, Error<W5500::Error>>
Unsubscribe from a topic.
§Return Value
The return value is a u16
packet identifier.
This can be compared to Event::UnsubAck
to determine when the
subscription has been deleted.
The packet identifier is zero (invalid) when filter
is empty.