Async Embedded MQTT Client
An async
, no_std
-compatible MQTT client library in Rust, designed for embedded systems using the Embassy async ecosystem and embedded-hal
1.0.0 traits.
Core Features
- Asynchronous: Built on
async/await
and designed for the Embassy ecosystem. no_std
by default: Suitable for bare-metal and resource-constrained devices.- Hardware Agnostic: Uses
embedded-hal-async
traits to support various communication transports (TCP, UART, SPI, etc.). - Memory Efficient: Leverages
heapless
to avoid dynamic memory allocation. - MQTT v3.1.1 and v5 Support: Protocol version can be selected via feature flags.
- QoS 0 & 1: Support for "at most once" and "at least once" message delivery.
Getting Started
To use this library, you need a transport that implements the MqttTransport
trait. This trait is an abstraction over the underlying communication channel (e.g., a TCP socket).
Here is a conceptual example of how to use the client with an embassy-net
TCP socket:
use ;
use TcpSocket;
use Duration;
// Assume `socket` is an already connected `TcpSocket`
async
Project Structure
The library is organized into a few key modules:
src/client.rs
: Contains the mainMqttClient
and its async state machine.src/packet.rs
: Handles the encoding and decoding of MQTT control packets.src/transport.rs
: Defines theMqttTransport
trait for hardware abstraction.src/error.rs
: Provides unified error types for the client.
Feature Flags
The following feature flags are available:
v5
: Enables MQTT v5 support.std
: Enablesstd
library support for running examples and tests on a host machine.defmt
: Enables logging via thedefmt
framework.transport-smoltcp
: Enables integration withembassy-net
for a full TCP/IP stack.nom
: Enables thenom
parser for decoding MQTT packets.
Running Examples
The crate includes several examples in the examples/
directory.
Desktop Mock
This example uses std::net::TcpStream
to connect to a local MQTT broker on your host machine.
Prerequisites: An MQTT broker (like Mosquitto) running on localhost:1883
.
Run command:
Embedded Examples
The esp8266_uart.rs
and smoltcp_ethernet.rs
examples are skeletons that demonstrate how the library would be integrated into a real embedded project. They require a specific HAL and driver setup to be fully functional.