huaweicloud-iot-device-sdk
Version:
93 lines (63 loc) • 1.95 kB
Markdown
//img.shields.io/npm/v/huaweicloud-iot-device-sdk.svg)](https://npmjs.org/package/huaweicloud-iot-device-sdk)
[](https://npmjs.org/package/huaweicloud-iot-device-sdk)
[ ](https://www.aliyun.com/product/iot) MQTT client for Node.js
You can install it as dependency with npm.
```sh
$
$ npm install huaweicloud-iot-device-sdk -S
```
Huawei IoT mqtt client with authrozied by deviceId & deviceSecret.
```js
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")
})
```
```js
client.subscribe(topic)
```
```js
client.publish(topic, 'Hello mqtt')
client.publish(topic, 'Hello mqtt', { qos: 1 })
```
```js
client.on('message', function(topic, message) {
console.log(topic+"," + message.toString())
})
```
<img src='https://raw.githubusercontent.com/wongxming/dtalkNodejs/master/wongxming.jpg' width="240" height="240" />
[![npm-version](https: