示例代码
/* iot - 阿里云IoT连接平台连接组件示例,产品功能定义:
* [功能类型] ------ [功能名称] ------ [标志符] ------ [数据类型]
* 属性 主灯开关 LightSwitch bool (0-关闭 1-开启)
* 服务 开关翻转 ToggleLightSwitch -
* 事件 故障上报 Error 参数标志符:ErrorCode
* */
var iot = require('iot');
var productKey = '<product-key>';
var deviceName = '<device-name>';
var deviceSecret = '<device-secret>';
var lightSwitch = 0;
var device = iot.device({
productKey: productKey,
deviceName: deviceName,
deviceSecret: deviceSecret,
region: 'cn-shanghai',
success: function () {
console.log('connect success');
onConnect();
},
fail: function () {
console.log('connect failed');
}
});
function onConnect() {
setInterval(function () {
/** post properties */
lightSwitch = 1 - lightSwitch;
device.postProps({
payload: {
LightSwitch: lightSwitch
},
success: function () {
console.log('postProps success');
},
fail: function () {
console.log('postProps failed');
}
});
/** post events */
device.postEvent({
id: 'Error',
params: {
ErrorCode: 0
},
success: function () {
console.log('postEvent success');
},
fail: function () {
console.log('postEvent failed');
}
});
}, 3000);
}
device.on('connect', function () {
console.log('(re)connected');
});
/* 网络断开事件 */
device.on('disconnect', function () {
console.log('disconnect ');
});
/* 关闭连接事件 */
device.on('close', function () {
console.log('iot client just closed');
});
/* 发生错误事件 */
device.on('error', function (err) {
console.log('error ' + err);
});
/* 云端设置属性事件 */
device.on('props', function (payload) {
console.log('cloud req data is ', payload);
console.log('LightSwitch ', payload.LightSwitch ? 'ON' : 'OFF');
});
/* 云端下发服务事件 */
device.on('service', function (id, payload) {
console.log('received cloud serviceid is ' + id);
console.log('received cloud req_data is ' + payload);
});
var iot = require('iot');
var productKey = '<product-key>';
var deviceName = '<device-name>';
var deviceSecret = '<device-secret>';
/* 设备签名 */
iot.sign({
productKey: productKey,
deviceName: deviceName,
deviceSecret: deviceSecret,
region: 'cn-shanghai',
success: function (params) {
console.log('sign success');
console.log(params);
},
fail: function () {
console.log('sign failed');
}
});
device(Object option)
创建 iot 实例,同时会开始尝试连接阿里云IoT平台。iot实例默认开启TLS加密。
入参
属性 | 类型 | 必填 | 描述 |
productKey | String | 是 | 设备的prodctKey |
deviceName | String | 是 | 设备的deviceName |
deviceSecret | String | 是 | 设备的deviceSecret |
region | String | 否 | 阿里云region,默认值: |
success | Function | 否 | 连接成功时的回调函数 |
fail | Function | 否 | 连接失败的回调函数 |
返回
实例。
sign(Object option)
进行设备签名。本接口相对独立,当 iot - 阿里云IoT平台连接组件 无法满足要求时,可以使用本接口进行设备认证,然后使用 mqtt 接入阿里云物联网平台。设备签名请参考 设备接入Link SDK - 设备签名。
入参
属性 | 类型 | 必填 | 描述 |
productKey | String | 是 | 设备的prodctKey |
deviceName | String | 是 | 设备的deviceName |
deviceSecret | String | 是 | 设备的deviceSecret |
region | String | 否 | 阿里云region,默认值: |
success | Function | 否 | 认证成功的回调函数 |
fail | Function | 否 | 认证失败的回调函数 |
success回调函数参数
Object类型,其属性有:
属性 | 类型 | 描述 |
host | String | 可以连接的MQTT服务器域名地址, 根据入参 |
port | Number | 可以连接的MQTT服务器端口号, 根据标准MQTT协议一般是 |
username | String | 连接MQTT服务器时将要使用的用户名 |
password | String | 连接MQTT服务器时将要使用的密码, 和用户名一一对应 |
clientid | String | 连接MQTT服务器时标识设备的自定义ID, 服务器对此不做校验, 鉴权通过 |
fail回调函数参数
属性 | 类型 | 描述 |
error | String | 错误信息 |
iot 实例
事件
当iot client 连接到iot平台时,触发connect事件。断开重连时也会触发该事件。
当连接断开时,触发'disconnect'事件。
当iot client 关闭时,触发close事件。
当iot云端下发属性设置时,触发props事件。
当iot云端调用设备service时,触发service事件。
当设备跟iot平台通信过程中遇到错误时,触发error事件。
方法
上报属性。
上报事件。
关闭iot实例。
注册iot事件回调函数。
事件:'connect'
当iot client 连接到iot平台时,触发'connect'事件。断开重连时也会触发该事件。
事件:'disconnect'
当连接断开时,触发'disconnect'事件。三元组错误、三元组重复上线、服务端断开连接、主动断开连接、网络发生异常等都会触发该事件。该事件触发时,会自动尝试重连。
事件:'close'
当iot client 关闭时,触发'close'事件。
事件:'props'
当iot云端下发属性设置时,触发'props'事件。
回调函数参数有:
属性 | 类型 | 描述 |
payload | Object | 服务端设置设备属性的内容 |
事件:'service'
当iot云端调用设备service时,触发'service'事件。
回调函数参数有:
属性 | 类型 | 描述 |
id | String | 服务标志符,详细请参考什么是物的模型 |
payload | Object | 服务端设置设备属性的内容 |
事件:'error'
当设备跟iot平台通信过程中遇到错误时,触发'error'事件。'error'事件的回调函数参数有:
属性 | 类型 | 描述 |
error | String | 错误信息 |
iot.postProps(Object options)
上报设备属性。
入参
Object类型,其属性有:
属性 | 类型 | 必填 | 描述 |
payload | Object | 是 | 设备定义的属性参数。详细请参考什么是物的模型 |
success | Function | 否 | 上报设备属性成功的回调函数 |
fail | Function | 否 | 上报设备属性失败的回调函数 |
iot.postEvent(Object options)
上报设备的事件。
入参
Object类型,其属性有:
属性 | 类型 | 必填 | 描述 |
id | String | 是 | 事件标志符,详细请参考什么是物的模型 |
params | Object | 是 | 要上报的事件参数 |
success | Function | 否 | 上报设备事件成功的回调函数,其函数参数参见下面说明 |
fail | Function | 否 | 上报设备事件失败的回调函数 |
success回调函数参数:
属性 | 类型 | 描述 |
res | String | 服务端reply的消息内容 |
iot.close()
关闭IoT实例。
iot.on(String event, Function callback)
监听事件。
入参
属性 | 类型 | 必填 | 描述 |
event | String | 是 | 注册的事件名称 |
callback | Function | 是 | 监听事件的回调函数 |
在文档使用中是否遇到以下问题
更多建议
匿名提交