huaweicloud-iot-device-sdk
v0.0.2
Published
华为云IoT物联网平台户端
Downloads
12
Maintainers
Readme
huaweicloud-iot-device-sdk
Huawei IoT SDK MQTT client for Node.js
Installation
You can install it as dependency with npm.
$ # save into package.json dependencies with -S
$ npm install huaweicloud-iot-device-sdk -S
Usage
Huawei IoT mqtt client with authrozied by deviceId & deviceSecret.
GET Data
const path = require('path');
const huaweiIoT = require('huaweicloud-iot-device-sdk');
const options = {
deviceId: "your deviceId",
deviceSecret: "your deviceSecret",
caFilePath: path.join(__dirname, 'hw-iot-root.pem'),
host:'your iot host'
}
//建立连接
var client = huaweiIoT.getHuaweiIoTClient(options);
client.on('message', function(topic, message) {
console.log("topic " + topic)
console.log("message " + message)
})
client.subscribe(`/huawei/v1/devices/${options.deviceId}/command/json`)
var topic = `/huawei/v1/devices/${options.deviceId}/data/json`
var postJson = {
msgType: "deviceReq",
data: [
{
serviceId: "pop",
serviceData: {
stackId:4
}
}
]
}
client.publish(topic, JSON.stringify(postJson))
client.end(function (){
console.log("end")
})
Subscribe Topic
client.subscribe(topic)
Publish Message
client.publish(topic, 'Hello mqtt')
client.publish(topic, 'Hello mqtt', { qos: 1 })
Receive Message
client.on('message', function(topic, message) {
console.log(topic+"," + message.toString())
})