#aws #iot #device #sdk

aws-iot-device-sdk-rust

An easy to use SDK for connecting to AWS IoT Core

4 releases

✓ Uses Rust 2018 edition

0.0.4 Jan 14, 2020
0.0.3 Dec 26, 2019
0.0.2 Dec 25, 2019
0.0.1 Dec 25, 2019

MIT license

12KB
119 lines

Documentation crates.io

aws-iot-device-sdk-rust

The AWS IoT Device SDK for Rust allows developers to write Rust to use their devices to access the AWS IoT platform through MQTT. This is my first crate, and project, in Rust, and as I am still learning it will hopefully get a lot better with time. With the client you can publish and subscribe to topics and add callbacks that are associated with topics. The shadow manager updates, gets, publishes and deletes the device shadow.

It has been through the Works on My Machine Certification Program, and it Works on My Machine™.


lib.rs:

aws-iot-core-sdk-rust aims to be a well-functioning and easy to use AWS IoT device SDK. At its core it uses the pure Rust MQTT client Rumqtt, as well as Serde for (de)serializing JSON. The name is chosen to match its C, C++, Python and JS counterparts.

  • Use this to easily connect your IoT devices to AWS IoT Core.
  • Publish and subscribe to any topic you want.
  • Register callback functions that will be called to handle incoming messages on any topic.

The crate re-exports Mqtt311s Quality of Service enum. These are used when subscribing and publish. The variants are:

  • AtMostOnce (0)
  • AtLeastOnce (1)
  • ExactlyOnce (2)

Publish and subscribe

use aws_iot_device_sdk_rust::client;

fn main() {
    let mut iot_core_client = client::AWSIoTClient::new(
        "myClientId",
        "root-CA.crt",
        "device.cert.pem",
        "device.private.key",
        "myendpoint.iot.eu-west-1.amazonaws.com"
        ).unwrap();

    iot_core_client.start_listening();
    iot_core_client.subscribe("thing/light/status", QoS::AtLeastOnce);
    iot_core_client.publish("thing/light/status", "on");
}

Add callback

use aws_iot_device_sdk_rust::client;

fn my_callback() {
    println!("Someone or something published to thing/light/status!");
}
fn main() {
    let mut iot_core_client = client::AWSIoTClient::new(
        "myClientId",
        "root-CA.crt",
        "device.cert.pem",
        "device.private.key",
        "myendpoint.iot.eu-west-1.amazonaws.com"
        ).unwrap();

    iot_core_client.start_listening();
    iot_core_client.add_callback("thing/light/status", my_callback).unwrap();
}

Get shadow updates

use aws_iot_device_sdk_rust::{client, shadow};

fn print_shadow_updates(shadow: String) {
    println!("{:?}", shadow);
}

fn main() {
    let mut iot_core_client = client::AWSIoTClient::new(
        "myClientId",
        "root-CA.crt",
        "device.cert.pem",
        "device.private.key",
        "myendpoint.iot.eu-west-1.amazonaws.com"
        ).unwrap();

    let mut shadow_manager = shadow::AWSShadowManager::new(&mut iot_core_client,
        String::from("MyThing"));
    shadow_manager.add_listen_on_delta_callback(print_shadow_updates);
}

Dependencies

~16MB
~329K SLoC