赞
踩
阿里云物联网平台是一个集成了设备管理、数据安全通信和消息订阅等能力的一体化平台。向下支持连接海量设备,采集设备数据上云;向上提供云端API,服务端可通过调用云端API将指令下发至设备端,实现远程控制。
MQTT.fx是一款基于Eclipse Paho使用Java语言编写的MQTT客户端,支持Windows、Mac和Linux操作系统,可用于验证设备是否可与物联网平台正常连接,并通过Topic订阅和发布消息。本文以Windows系统下MQTT.fx为例,介绍设备使用MQTT协议模拟接入物联网平台。
下载并安装MQTT.fx软件。MQTT.fx软件安装和使用说明,请参见MQTT.fx。
参考:如何计算MQTT签名参数 获取mqtt的连接签名参数
注意 其中clientId是mqttClientId值,请勿与设备的clientId混淆。MQTT连接参数的详细说明,请参见MQTT-TCP连接通信。
添加到MQTT.fx
注意时间time一定要真实实时,否则报错6332
topic:/sys/a1kRPyuOfpd/temp_02/thing/event/property/post
{
"id": "1235",
"version": "1.0",
"sys":{
"ack":0
},
"params": {
"CurrentHumidity": {
"value": 12.2,
"time": 1524448722000
},
"CurrentTemperature": {
"value": 31.6,
"time": 1524448722000
}
},
"method": "thing.event.property.post"
}
/sys/a1jwSeD1EDU/mensuo1/thing/event/LowElectricityAlarm/post
{
"id": "1235",
"version": "1.0",
"sys":{
"ack":0
},
"params":{
},
"method": "thing.event.LowElectricityAlarm.post"
}
了解参数
/sys/a1jwSeD1EDU/mensuo1/thing/event/DoorOpenNotification/post
{
"id": "1235",
"version": "1.0",
"sys":{
"ack":0
},
"params":{
"KeyID":"1111",
"LockType":2
},
"method": "thing.event.DoorOpenNotification.post"
}
https://iot.console.aliyun.com/lk/monitor/log?pk=a1kRPyuOfpd&pn=%E6%B8%A9%E5%BA%A6%E4%BC%A0%E6%84%9F%E5%99%A82&dn=temp_02
配置指南:
登录物联网平台控制台,将鼠标移至账号头像上,然后单击AccessKey管理,获取AccessKey ID和AccessKey Secret。
https://ram.console.aliyun.com/manage/ak?spm=5176.11485173.top-nav.dak.379559afZJzv8X
登录物联网平台控制台,在对应实例的规则引擎 > 服务端订阅 > 消费组列表查看您的消费组ID。
实例ID。您可在物联网平台控制台的实例概览页面,查看当前实例的ID。
若有ID值,必须传入该ID值。
若无实例概览页面或ID值,传入空值,即iotInstanceId = “”。
表示客户端ID,需您自定义,长度不可超过64个字符。建议使用您的AMQP客户端所在服务器UUID、MAC地址、IP等唯一标识。
AMQP客户端接入并启动成功后,登录物联网平台控制台,在对应实例的规则引擎 > 服务端订阅 > 消费组列表页签,单击消费组对应的查看,消费组详情页面将显示该参数,方便您识别区分不同的客户端。
${uid}.iot-amqp.${YourRegionId}.aliyuncs.com:${port}
### 最终配置
```java
private final static Logger logger = LoggerFactory.getLogger(AmqpClient.class);
private static String accessKey = "LTAI5tMHxxxxxxx13Z8D3nY";
private static String accessSecret = "AdAd746FxxxxxxA5Ydd";
private static String consumerGroupId = "DEFAULT_GROUP";
//iotInstanceId:实例ID。若是2021年07月30日之前(不含当日)开通的公共实例,请填空字符串。
private static String iotInstanceId = "";
//控制台服务端订阅中消费组状态页客户端ID一栏将显示clientId参数。
//建议使用机器UUID、MAC地址、IP等唯一标识等作为clientId。便于您区分识别不同的客户端。
private static String clientId = "123456789987654321";
//${YourHost}为接入域名,请参见AMQP客户端接入说明文档。
private static String host = "13246xxx0940.iot-amqp.cn-shanghai.aliyuncs.com";
## 用MQTT.fx模拟上报温湿度
可以看到日志
### 属性上报消息
```bash
15:44:57.163 [pool-1-thread-1] INFO com.aliyun.iotx.demo.AmqpClient - receive message,
topic = /a1kRPyuOfpd/temp_02/thing/event/property/post,
messageId = 1585538019430364673,
content = {"deviceType":"CurrentTemperature","iotId":"sLomjhEuCDrTX2KQTU5x000000","requestId":"1235","checkFailedData":{},"productKey":"a1kRPyuOfpd","gmtCreate":1666856696337,"deviceName":"temp_02","items":{"CurrentHumidity":{"value":12.2,"time":1524448722000},"CurrentTemperature":{"value":31.6,"time":1524448722000}}}
16:04:58.253 [pool-1-thread-2] INFO com.aliyun.iotx.demo.AmqpClient - receive message,
topic = /as/mqtt/status/a1kRPyuOfpd/temp_02,
messageId = 1585543057267769344,
content = {"lastTime":"2022-10-27 16:04:57.473","iotId":"sLomjhEuCDrTX2KQTU5x000000","utcLastTime":"2022-10-27T08:04:57.473Z","clientIp":"121.31.14.28","utcTime":"2022-10-27T08:04:57.473Z","offlineReasonCode":200,"time":"2022-10-27 16:04:57.473","productKey":"a1kRPyuOfpd","deviceName":"temp_02","status":"offline"}
16:05:30.841 [pool-1-thread-3] INFO com.aliyun.iotx.demo.AmqpClient - receive message,
topic = /as/mqtt/status/a1kRPyuOfpd/temp_02,
messageId = 1585543193989292544,
content = {"lastTime":"2022-10-27 16:05:30.065","iotId":"sLomjhEuCDrTX2KQTU5x000000","utcLastTime":"2022-10-27T08:05:30.065Z","clientIp":"121.31.14.28","utcTime":"2022-10-27T08:05:30.065Z","time":"2022-10-27 16:05:30.065","productKey":"a1kRPyuOfpd","deviceName":"temp_02","status":"online"}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。