All Products
Search
Document Center

IoT Platform:Use the Paho MQTT library for Rust to connect a device to IoT Platform

Last Updated:Apr 25, 2023

This topic describes how to use the Paho Message Queuing Telemetry Transport (MQTT) library for Rust to connect a device to IoT Platform and enable messaging.

Usage notes

In this topic, the permissions of a common user are used to perform all operations. If you want to perform specific operations that require administrator permissions, run the sudo command.

Prerequisites

A product and a device are created in an instance in the IoT Platform console. A device certificate and an MQTT endpoint are obtained. The certificate information includes the ProductKey, DeviceName, and DeviceSecret. For more information, see the following topics:

Prepare a development environment

  • Ubuntu 18.04

  • Cargo 1.61.0 or later

  • If you use Linux, run the following command to install Cargo to manage dependencies.

    sudo apt-get install cargo cmake

Connect a device to IoT Platform

  1. Download the package that contains the sample code and decompress the package to the aiot-rust-demo directory. The following table describes the files that are decompressed to the directory.

    File

    Description

    src/mqtt-basic-demo/main.rs

    This file contains the logic code that is used to connect a device to IoT Platform and enable communications.

    Cargo.toml

    The description file of the project.

  2. In the src/mqtt-basic-demo/main.rs file, replace the values of the product_key, device_name, , and device_secret parameters with your device certificate information.

    let product_key = "gb80*****";
    let device_name = "mqtt_******o";
    let timestamp = "2524****000";
    let device_secret = b"f***********************b";
    let host = "tcp://iot-060axxxx.mqtt.iothub.aliyuncs.com:1883"
  3. Run the cargo build command to compile your project. An output that is similar to the following information is returned.

       Compiling hex-literal v0.3.4
       Compiling hmac-sha256 v0.1.7
       Compiling openssl-src v111.24.0+1.1.1s
       Compiling cmake v0.1.49
       Compiling slab v0.4.7
       Compiling openssl-sys v0.9.80
       Compiling paho-mqtt-sys v0.8.0 (https://github.com/eclipse/paho.mqtt.rust.git?branch=master#8e1f7342)
       Compiling concurrent-queue v2.1.0
       Compiling crossbeam-channel v0.5.6
       Compiling async-channel v1.8.0
       Compiling futures-macro v0.3.25
       Compiling thiserror-impl v1.0.38
       Compiling futures-executor v0.3.25
       Compiling futures v0.3.25
       Compiling paho-mqtt v0.12.0 (https://github.com/eclipse/paho.mqtt.rust.git?branch=master#8e1f7342)
       Compiling mqtt-example v0.1.0 
        Finished dev [unoptimized + debuginfo] target(s) in 20.83s
  4. Run the ./target/debug/mqtt_basic_demo command to execute the project. An output that is similar to the following information is returned.

    starting Alibaba iot demo...
    subscribed to topic:=/gb80sxxxx/mqtt_basic_demo/user/get
    published to topic:=/gb80sxxxx/mqtt_basic_demo/user/update
    start receiving...