azure_iot_sdk

Crates.ioazure_iot_sdk
lib.rsazure_iot_sdk
version0.8.0
sourcesrc
created_at2020-04-18 03:03:58.962107
updated_at2021-05-11 00:46:49.965404
descriptionClient library for connection devices to Azure IoT Hub
homepage
repositoryhttps://github.com/damienpontifex/azure-iot-sdk-rs
max_upload_size
id231383
size116,847
Damien Pontifex (damienpontifex)

documentation

https://docs.rs/azure_iot_sdk

README

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 DeviceConfig {
        hostname,
        device_id,
        shared_access_key,
    } = DeviceConfig::from_env().unwrap();

    let token_source = DeviceKeyTokenSource::new(
        &hostname,
        &device_id,
        &shared_access_key,
    )
    .unwrap();

    let mut client = IoTHubClient::new(&hostname, device_id, token_source).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);

    tokio::join!(receive_loop, sender);
}
Commit count: 75

cargo fmt