#iot #azure

azure_iot_sdk

Client library for connection devices to Azure IoT Hub

5 releases (breaking)

new 0.5.0 Oct 18, 2020
0.4.0 Jun 27, 2020
0.3.0 Apr 25, 2020
0.2.0 Apr 19, 2020
0.1.0 Apr 18, 2020

#11 in #azure

32 downloads per month

MIT license

54KB
936 lines

Azure IoT SDK for Rust

Self developed library to interact with Azure IoT Hub using MQTT protocol

CI docs Crate cratedown cratelastdown

Running examples

Copy the sample config file

cp examples/config.sample.toml examples/config.toml

Edit values in examples/config.toml with your iot hub host, device and primary key

Usage

#[macro_use]
extern crate log;

use azure_iot_sdk::{client::IoTHubClient, message::Message};

use serde::Deserialize;

#[derive(Debug, Deserialize)]
struct DeviceConfig {
    hostname: String,
    device_id: String,
    shared_access_key: String,
}

impl DeviceConfig {
    fn from_env() -> Result<Self, config::ConfigError> {
        let mut cfg = config::Config::default();
        cfg.merge(config::File::with_name("examples/config"))?;
        cfg.try_into()
    }
}

#[tokio::main]
async fn main() {
    env_logger::from_env(env_logger::Env::default().default_filter_or("info")).init();

    let config = DeviceConfig::from_env().unwrap();

    let mut client =
        IoTHubClient::with_device_key(config.hostname, config.device_id, config.shared_access_key)
            .await;

    info!("Initialized client");

    let mut recv = client.get_receiver().await;
    let receive_loop = async {
        while let Some(msg) = recv.recv().await {
            match msg {
                MessageType::C2DMessage(msg) => info!("Received message {:?}", msg),
                _ => {}
            }
        }
    };

    let msg = Message::new(b"Hello, world!".to_vec());
    let sender = client.send_message(msg);

    futures::join!(receive_loop, sender);
}

Dependencies

~6.5–9MB
~185K SLoC