aws-iot-device-sdk-rust
A Rust SDK for connecting IoT devices to AWS IoT Core via MQTT 3.1.1. Built on top of rumqttc with TLS powered by rustls.
Features
- Async and sync clients for connecting to AWS IoT Core
- Mutual TLS authentication using X.509 certificates
- Publish and subscribe to MQTT topics
- Broadcast incoming messages to multiple receivers
- Configurable MQTT options (keep-alive, packet size, last will, etc.)
Installation
Add the following to your Cargo.toml:
[]
= "0.7"
By default the async client is enabled. To use the sync client instead:
[]
= { = "0.7", = false, = ["sync"] }
You can also enable both:
[]
= { = "0.7", = ["sync"] }
Prerequisites
To connect to AWS IoT Core you need:
- An AWS IoT thing registered in your account
- A root CA certificate (e.g.
AmazonRootCA1.pem) - A device certificate (
*.crt) - A private key (
*.pem) - Your AWS IoT endpoint (found in the AWS IoT Core console under Settings)
Usage
Async client
Create an AWSIoTAsyncClient, spawn an event loop listener on a separate task, and use receivers to process incoming messages. Multiple receivers can be created to fan out messages to different parts of your application.
use ;
use Error;
async
Sync client
The sync client works the same way but uses std::thread instead of tokio. Enable it with the sync feature.
use ;
use Error;
MQTT options
You can customize MQTT connection options by passing MQTTOptionsOverrides to AWSIoTSettings:
use MQTTOptionsOverrides;
use Duration;
let mut overrides = default;
overrides.keep_alive = Some;
overrides.clean_session = Some;
overrides.conn_timeout = Some;
let aws_settings = new;
Using the underlying rumqttc client directly
If you need more control, you can retrieve the underlying rumqttc client and work with it directly:
let = new.await?;
let client = iot_core_client.get_client.await;
// Use `client` and `eventloop` with the rumqttc API directly
Error handling
All fallible operations return Result<_, AWSIoTError>. The error type wraps the underlying rumqttc and I/O errors:
| Variant | Description |
|---|---|
AWSConnectionError |
MQTT connection failure (wraps rumqttc::ConnectionError) |
MQTTClientError |
Client operation failure such as publish/subscribe (wraps rumqttc::ClientError) |
IoError |
File I/O error when reading certificates or keys |
MutexError |
Mutex poisoning (sync client only) |
License
This project is licensed under the MIT License.