Device-side development

Updated at:

After you complete the configurations in the IoT Platform console, you can develop your device-side services. This topic uses a Node.js script to simulate device behavior. The script establishes an MQTT connection, reports data, and accepts instructions.

Procedure

  1. Download and install Node.js on the Windows or Linux operating system. In this example, Windows 10 (64-bit) is used. Download the node-v14.15.1-x64.msi installation package.

  2. Open a Command Prompt window and run the following command to view the node version:

    node --version

    If the package is installed, the following version number appears:

    v14.15.1
  3. Create a JavaScript file, such as iot_device.js, on your local computer to store the Node.js sample code.

    The sample code is as follows:

    // Import the dependent MQTT library, or implement it yourself.
    const mqtt = require('aliyun-iot-mqtt');
    // Device identity.
    var options = {
        productKey: "Your_ProductKey",
        deviceName: "Your_DeviceName",
        deviceSecret: "Your_DeviceSecret",
        host: "iot-cn-***.mqtt.iothub.aliyuncs.com"
    };
    // 1. Establish a connection.
    const client = mqtt.getAliyunIotMqttClient(options);
    // 2. The device accepts command data from the cloud.
    client.on('message', function(topic, message) {
        console.log("topic " + topic)
        console.log("message " + message)
    })
    // 3. The device subscribes to the command topic.
    client.subscribe(`/${options.productKey}/${options.deviceName}/user/cmd/down`)
    // 4. Impersonate the device to report data (raw messages).
    setInterval(function() {
        client.publish(`/${options.productKey}/${options.deviceName}/user/data/up`, getPostData(),{qos:1});
    }, 1000);
    // Impersonate the original message format of the device.
    function getPostData() {
        let payload = "DE02,"+Math.floor((Math.random() * 20) + 10)
        +","+Math.floor((Math.random() * 20) + 10)
        +",011101010,am024,1d478f";
        console.log("payload=[ " + payload+" ]")
        return JSON.stringify(payload);
    }
  4. Configure the device connection parameters.

    Parameter

    Example

    Description

    productKey

    a16c*****

    The device certificate information that you saved after you added the device.

    You can view this information on the Device Details page in the IoT Platform console.

    deviceName

    deviceSN1

    deviceSecret

    b2e6e4f102458d84***

    host

    iot-cn-***.mqtt.iothub.aliyuncs.com

    The domain name for MQTT device connection. For more information, see Manage instance endpoints.

  5. Open a Command Prompt window. Run the cd command to navigate to the directory that contains the iot_device.js file. In that directory, run the npm command to download the MQTT library for the Alibaba Cloud IoT Platform.

    npm install aliyun-iot-mqtt -S

    After the installation is complete, the project directory contains the following content:

    • node_modules folder

    • iot_device.js file (2 KB)

    • package-lock.json file (26 KB)

What to do next

Server-side development