The device uses the HTTPS protocol to access the IoT platform

1. Device identity authentication: get token through triplet
The HTTPS server address is https://iot-as-http.cn-shanghai.aliyuncs.com


Example authentication request:

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"
}
Return example:

{
"code": 0,//business status code
"message": "success",//business information
"info": {
"token": "6944e5bfb92e4d4ea3918d1eda3942f6"
}
}

2. Device data reporting
The HTTPS server address is https://iot-as-http.cn-shanghai.aliyuncs.com/topic/${topic}


Example request:

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

{
"code": 0,//business status code
"message": "success",//business information
"info": {
"messageId": 892687627916247040
}
}

3. Actual case Nodejs version

3.1 Create a premium product


3.2 Function Definition
Add product attribute definition

Property Name Identifier Data Type Range
Temperature temperature float -50~100
Humidity float 0~100


3.3 Device Management>Register Device, Get Identity Triple


3.4 Device Simulation Code
var rp = require('request-promise');
const crypto = require('crypto');

const deviceConfig = {
productKey: "Replace productKey",
deviceName: "Replace deviceName",
deviceSecret: "Replace deviceSecret"
}

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

//1. Get identity token
rp(getAuthOptions(deviceConfig))
.then(function(parsedBody) {
console.log('Auth Info :'+JSON.stringify(parsedBody))
//2. Post model data
pubData(topic, parsedBody.info.token, getPostData())
})
.catch(function(err) {
console.log('Auth err :'+JSON.stringify(err))
});

//Generate parameters for Auth authentication
function getAuthOptions(deviceConfig) {

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

//1. Generate 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))
});

}
//simulant model data
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 topic=" + topic)
console.log(payloadJson)

return JSON. stringify(payloadJson);
}
//HmacSha1 sign
function signHmacSha1(params, deviceSecret) {

let keys = Object.keys(params).sort();
// Sort lexicographically
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');
}

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us