Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
482 views
in Technique[技术] by (71.8m points)

azure - How can a Rust application send a cloud-to-device message to the IoTHub intended to reach a device?

I am building an MQTT-based Rust application that sends a message to the IoTHub so that this is forwarded to a target device which listens to cloud-to-device messages.

The target device "myDev" is subscribed to the topic devices/myDev/messages/devicebound/# and it can receive messages from the IoTHub interface ("Message to Device", on the Azure Portal).

The examples I have found to achieve this in other programming languages use the Azure IoT SDK, which has a client service. See for instance the example for Java: https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-java-java-c2d#send-a-cloud-to-device-message

The problem is that there is really no official Azure IoT SDK for Rust. I could only find this project https://github.com/damienpontifex/azure-iot-sdk-rs which I cannot use because it has a dependency with Tokio > 1.x and I have to stick for now with Tokio 0.x.

However, I can imagine that sending a cloud-to-device message to IoTHub must be somehow doable. Required Information is probably: device_id, body and the properties, which are the fields available in the interface.

But I haven't found further details anywhere. Any help is appreciated.

question from:https://stackoverflow.com/questions/65942688/how-can-a-rust-application-send-a-cloud-to-device-message-to-the-iothub-intended

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Indeed, there is no official SDK - but there are some other implementations you can have a look at it, like this one with the following example:

use tokio::time;
use azure_iot_sdk::{IoTHubClient, DeviceKeyTokenSource, Message};

#[tokio::main]
async fn main() -> azure_iot_sdk::Result<()> {
    let iothub_hostname = "iothubname.azure-devices.net";
    let device_id = "MyDeviceId";
    let token_source = DeviceKeyTokenSource::new(
        iothub_hostname,
        device_id,
        "TheAccessKey",
    ).unwrap();

    let mut client =
        IoTHubClient::new(iothub_hostname, device_id.into(), token_source).await?;

    let mut interval = time::interval(time::Duration::from_secs(1));
    let mut count: u32 = 0;

    loop {
        interval.tick().await;

        let msg = Message::builder()
            .set_body(format!("Message #{}", count).as_bytes().to_vec())
            .set_message_id(format!("{}-t", count))
            .build();

        client.send_message(msg).await?;

        count += 1;
    }

    Ok(())
}

Alternatively, If Rust is not a hard requirement, at least end-to-end, maybe you can have an independent service that deals with the operations with IoT Hub, and have it called from your Rust code as a normal REST service.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...