If you use a self-developed MQTT tool to connect a device with IoT Platform, you must specify the following MQTT parameters for authentication: username, passwd, and mqttClientId. This article describes how to obtain the values of these parameters.

Overview

You can use one of the following methods to obtain the values of the MQTT parameters.

Method Description
Use the IoT Platform console The supported signature algorithm is hmacsha256.

By default, the ClientId parameter of the device is a string in the ${ProductKey} + '.' + ${DeviceName} format. You cannot modify this parameter.

Use the Node.js script The supported signature algorithm is hmacsha256.

By default, the ClientId parameter of the device is a string in the ${ProductKey} + '.' + ${DeviceName} format. You can modify this parameter.

Use the IoT Platform console

  1. Log on to the IoT Platform console.
  2. On the Overview page, find the instance that you want to manage and click the instance name to go to the Instance Details page.
    Important Enterprise Edition instances are available in the China (Shanghai) and Japan (Tokyo) regions. If the Enterprise Edition instances are unavailable in the region that you select, skip this step.
    Overview
  3. In the left-side navigation pane, choose Devices > Devices. The Devices page appears.
  4. In the device list, find the device and click View in the Actions column. The Device Details page appears.
  5. On the Device Information tab, click Here next to MQTT Connection Parameters to obtain the MQTT parameters.
    Important The clientId parameter indicates the mqttClientId parameter and is different from the ClientId parameter of the device. For more information about MQTT connection parameters, see Establish MQTT connections.
    MQTT connection parameters

Use the Node.js script

  1. Download signTool.zip and decompress the package. Modify the following parameters in the mqttSignTool.js file and then save the file.
    Parameter Example Description
    productKey a1Ee*** The device certificate that is saved after you add the device. For more information, see Create a device.

    You can view the device certificate on the Device Details page in the console.

    deviceName 8Co5***
    deviceSecret bcabcf***
    timestamp 2524608000000 The timestamp. Unit: milliseconds.
    briefId 12345 Optional. The client ID of the device.

    The default value provided in the package is a string in the ${ProductKey} + '.' + ${DeviceName} format. You can modify this parameter as needed, such as var briefId = '12345'.

    Important You can specify a client ID that is up to 64 characters in length. To efficiently identify a device, we recommend that you use the MAC address or serial number (SN) of the device as the client ID.
  2. Download and install Node.js on the Windows or Linux operating system. In this example, the Ubuntu 16.04 64-bit Linux operating system is used. Therefore, download the compiled node-v16.5.0-linux-x64.tar.gz package.
    1. Log on to the Linux virtual machine (VM).
    2. Run the following commands to download and decompress the package:
      wget https://nodejs.org/dist/v16.5.0/node-v16.5.0-linux-x64.tar.gz
      tar xf node-v16.5.0-linux-x64.tar.gz
      cd node-v16.5.0-linux-x64
      ./bin/node -v
      If the package is decompressed, the following version number appears:
      v16.5.0
    3. The node-v16.5.0-linux-x64/bin directory contains the node and npm files. Run the following commands to configure global environment variables:
      ln -s /root/node-v16.5.0-linux-x64/bin/npm   /usr/local/bin/
      ln -s /root/node-v16.5.0-linux-x64/bin/node   /usr/local/bin/
  3. Run the following commands to exit the node-v16.5.0-linux-x64 directory and create the signTool project file to store the JavaScript file and signature file:
    cd ..
    mkdir signTool
    cd signTool/
  4. Upload the mqttSignTool.js file that is modified in Step 1 to the signTool directory in the Linux VM development environment.
  5. Go to the signTool file and run the following commands to download and view the signature file:
    npm install crypto-js
    ls
    The following figure shows the downloaded signature file. file
  6. Run the following command to run the mqttSignTool.js signature file:
    node mqttSignTool.js
    The following figure shows the obtained MQTT parameters that are required for authentication.
    Important The clientId parameter indicates the mqttClientId parameter and is different from the ClientId parameter of the device. For more information about MQTT connection parameters, see Establish MQTT connections.
    Result