关于物联网:设备使用-HTTPS-协议接入-IoT-物联网平台设备接入类

91次阅读

共计 3156 个字符,预计需要花费 8 分钟才能阅读完成。

设施基于 HTTPS 协定接入 IoT 平台

设施基于 HTTPS 协定接入 IoT 平台仅华东 2 反对官网文档 ​​​https://help.aliyun.com/document_detail/58034.html​​

1. 设施身份认证: 通过三元组获取 token

HTTPS 服务器地址为 ​​https://iot-as-http.cn-shanghai.aliyuncs.com/>​​​​

​​​

​​认证申请示例:

POST /auth HTTP/1.1
Host: iot-as-http.cn-shanghai.aliyuncs.com
Content-Type: application/json
body: {
    "version": "default",
    "clientId": "mylight1000002",
    "signmethod": "hmacsha1",
    "sign": "4870141D4067227128CBB4377906C3731CAC221C",
    "productKey": "ZG1EvTEa7NN",
    "deviceName": "NlwaSPXsCpTQuh8FxBGH",
    "timestamp": "1501668289957"
}

返回示例:

{
  "code": 0,// 业务状态码
  "message": "success",// 业务信息
  "info": {"token":  "6944e5bfb92e4d4ea3918d1eda3942f6"}
}

2. 设施数据上报

HTTPS 服务器地址为 ​​https://iot-as-http.cn-shanghai.aliyuncs.com/topic/​​${topic}

申请示例:

POST /topic/a1GFjLP3xxC/device123/pub
Host: iot-as-http.cn-shanghai.aliyuncs.com
password:${token}
Content-Type: application/octet-stream
body: ${your_data}

返回示例

{
  "code": 0,// 业务状态码
  "message": "success",// 业务信息
  "info": {"messageId": 892687627916247040}
}

3. 实战案例 Nodejs 版

3.1 创立高级版产品

3.2 性能定义

增加产品属性定义

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

3.4 设施模仿代码

var rp = require('request-promise');
const crypto = require('crypto');

const deviceConfig = {
    productKey: "替换 productKey",
    deviceName: "替换 deviceName",
    deviceSecret: "替换 deviceSecret"
}

const topic = `/sys/${deviceConfig.productKey}/${deviceConfig.deviceName}/thing/event/property/post`;

//1. 获取身份 token
rp(getAuthOptions(deviceConfig))
    .then(function(parsedBody) {console.log('Auth Info :'+JSON.stringify(parsedBody))
        //2. 公布物模型数据
        pubData(topic, parsedBody.info.token, getPostData())
    })
    .catch(function(err) {console.log('Auth err :'+JSON.stringify(err))
    });

// 生成 Auth 认证的参数
function getAuthOptions(deviceConfig) {

    const params = {
        productKey: deviceConfig.productKey,
        deviceName: deviceConfig.deviceName,
        timestamp: Date.now(),
        clientId: Math.random().toString(36).substr(2),
    }

    //1. 生成 clientId,username,password
    var password = signHmacSha1(params, deviceConfig.deviceSecret);

    var options = {
        method: 'POST',
        uri: 'https://iot-as-http.cn-shanghai.aliyuncs.com/auth',
        body: {
            "version": "default",
            "clientId": params.clientId,
            "signmethod": "hmacsha1",
            "sign": password,
            "productKey": deviceConfig.productKey,
            "deviceName": deviceConfig.deviceName,
            "timestamp": params.timestamp
        },
        json: true
    };

    return options;
}

//publish Data to IoT
function pubData(topic, token, data) {

    const options = {
        method: 'POST',
        uri: 'https://iot-as-http.cn-shanghai.aliyuncs.com/topic' + topic,
        body: data,
        headers: {
            password: token,
            'Content-Type': 'application/octet-stream'
        }
    }

    rp(options)
        .then(function(parsedBody) {console.log('publish success :' + parsedBody)
        })
        .catch(function(err) {console.log('publish err' + JSON.stringify(err))
        });

}
// 模仿物模型数据
function getPostData() {
    var payloadJson = {id: Date.now(),
        params: {humidity: Math.floor((Math.random() * 20) + 60),
            temperature: Math.floor((Math.random() * 20) + 10)
        },
        method: "thing.event.property.post"
    }

    console.log("===postData\n topic=" + topic)
    console.log(payloadJson)

    return JSON.stringify(payloadJson);
}
//HmacSha1 sign
function signHmacSha1(params, deviceSecret) {let keys = Object.keys(params).sort();
    // 按字典序排序
    keys = keys.sort();
    const list = [];
    keys.map((key) => {list.push(`${key}${params[key]}`);
    });
    const contentStr = list.join('');
    return crypto.createHmac('sha1', deviceSecret).update(contentStr).digest('hex');
}

3.5 运行成果

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

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

正文完
 0