1. 筹备工作

1.1 注册阿里云账号

应用集体淘宝账号或手机号,开明阿里云账号,并通过__实名认证(能够用支付宝认证)__

1.2 收费开明IoT物联网套件

产品官网 https://www.aliyun.com/product/iot

1.3 软件环境

Nodejs装置 https://nodejs.org/en/download/
编辑器 sublimeText/nodepad++/vscode
MQTT lib https://www.npmjs.com/package/mqtt

2. 开发步骤

2.1 云端开发

1) 创立高级版产品

2) 性能定义,产品物模型增加属性

增加产品属性定义


物模型对应属性上报topic

/sys/替换为productKey/替换为deviceName/thing/event/property/post

物模型对应的属性上报payload

{    id: 123452452,    params: {        temperature: 26.2,        humidity: 60.4    },    method: "thing.event.property.post"}

3) 设施治理>注册设施,取得身份三元组

2.2 设施端开发

咱们以nodejs程序来模仿设施,建设连贯,上报数据。

1.创立文件夹 aliyun-iot-demo-nodejs2.进入文件夹,创立package.json文件,增加内容3.执行npm install命令,装置sdk4.创立thermometer.js文件,增加内容5.执行node thermometer.js命令

1) package.json增加阿里云IoT套件sdk依赖

{  "name": "aliyun-iot",  "dependencies": {    "mqtt": "2.18.8"  },  "author": "wongxming",  "license": "MIT"}

2) 下载安装SDK

在aliyun-iot-demo-nodejs文件夹下,执行命令

$ npm install

3) 应用程序目录构造

4) 模仿设施thermometer.js代码

/**"dependencies": { "mqtt": "2.18.8" }*/const crypto = require('crypto');const mqtt = require('mqtt');//设施身份三元组+区域const deviceConfig = require("./iot-device-config.json");const options = {    productKey: deviceConfig.productKey,    deviceName: deviceConfig.deviceName,    timestamp: Date.now(),    clientId: Math.random().toString(36).substr(2)}options.password = signHmacSha1(options, deviceConfig.deviceSecret);options.clientId = `${options.clientId}|securemode=3,signmethod=hmacsha1,timestamp=${options.timestamp}|`;options.username = `${options.deviceName}&${options.productKey}`;const url = `tcp://${deviceConfig.productKey}.iot-as-mqtt.${deviceConfig.regionId}.aliyuncs.com:1883`;//建设连贯const client = mqtt.connect(url,options);//属性上报的Topicconst topic = `/sys/${deviceConfig.productKey}/${deviceConfig.deviceName}/thing/event/property/post`;setInterval(function() {    //公布数据到topic    client.publish(topic, getPostData());}, 5 * 1000);function getPostData(){    const payloadJson = {        id: Date.now(),        params: {            temperature: Math.floor((Math.random() * 20) + 10),            humidity: Math.floor((Math.random() * 40) + 60)        },        method: "thing.event.property.post"    }    console.log("===postData topic=" + topic)    console.log(payloadJson)    return JSON.stringify(payloadJson);}/*  生成基于HmacSha1的password  参考文档:https://help.aliyun.com/document_detail/73742.html?#h2-url-1*/function signHmacSha1(options, deviceSecret) {    let keys = Object.keys(options).sort();    // 按字典序排序    keys = keys.sort();    const list = [];    keys.map((key) => {        list.push(`${key}${options[key]}`);    });    const contentStr = list.join('');    return crypto.createHmac('sha1', deviceSecret).update(contentStr).digest('hex');}

设施配置文件

{    "productKey": "替换productKey",    "deviceName": "替换deviceName",    "deviceSecret": "替换deviceSecret",    "regionId": "cn-shanghai"}

3. 启动运行

3.1 设施启动

$ node thermometer.js

3.2 云端查看设施运行状态

物联网平台产品介绍详情:https://www.aliyun.com/product/iot/iot_instc_public_cn

              阿里云物联网平台客户交换群