使用MQTT连接阿里云的物联网云平台(2)

使用MQTT连接阿里云IOT平台(2)

上一篇讲到在阿里云的物联网平台建立物理模型(即数据点),以及如何使用虚拟设备查看设备上线时上报的JSON格式和云平台下发的JSON格式,这点对于后面项目开发来说很重要。

本篇将讲述如何使用MQTT.fx连接阿里云。

首先介绍MQTT.fx对于开发者来说是个什么样的东西,MQTT.fx能够有效的模拟设备连接云平台,相对于在云平台的虚拟设备,它还需要用户提供IP地址,用户名,密码等(可以通过证书或密钥方式),另外任何支持MQTT协议的云平台都可以使用MQTT.fx,对于不同平台,根据平台的开发文档有所不同。

MQTT.fx可以通过百度获取,这里就不进行提供了。

接下来上主菜了!

登录阿里云的物联网平台,找到自己想连接的设备,点击进去查看
在这里插入图片描述
如何按箭头所指获取三元
在这里插入图片描述
接下来需要使用到一个小工具阿里云参数小工具

在这里插入图片描述
可以获取到
域名:一般都是以(华东2(上海)),ProductKey + “.iot-as-mqtt.cn-shanghai.aliyuncs.com”
端口:固定1883
ClientID:deviceName + “|securemode=3,signmethod=hmacmd5|”
username:deviceName & ProductKey
password: 这个是hmacmd5对deviceSecret加密后得出的数据

接下来使用MQTT.fx了
在这里插入图片描述
在这里插入图片描述
然后点击OK后 Connect的在这里插入图片描述
按图所示,连接成功后,填写发布的主题和订阅的主题

根据上一篇所说的
发布的主题为

/sys/{ProductKey}/{DeviceName}/thing/event/property/post

订阅的主题为

/sys/{ProductKey}/{DeviceName}/thing/service/property/set

设备上报的JSON格式

{"id":1626879369379,
"params":{"PowerSwitch":1},
"version":"1.0","method":"thing.event.property.post"
}

云平台下发设置的JSON格式

{"method":"thing.service.property.set",
"id":"1656973952",
"params":{"PowerSwitch":0},"version":"1.0.0"}

这里就不在MQTT.fx演示了,各位小伙伴自己实践一下可以加深映像哦!
没看过上一篇的可以在这点击,理解起来更容易

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面是一个简单的VUE mqtt连接阿里云物联网平台的程序: 1. 首先,在你的VUE项目中安装mqtt依赖: ``` npm install mqtt --save ``` 2. 在你的组件中引入mqtt: ```javascript import mqtt from 'mqtt' ``` 3. 在你的组件中定义mqtt连接的参数: ```javascript const options = { clientId: 'your_client_id', username: 'your_username', password: 'your_password', clean: true } const mqttUrl = 'mqtt://your_mqtt_url:1883' ``` 4. 在你的组件中创建mqtt连接: ```javascript let client = mqtt.connect(mqttUrl, options) ``` 5. 在你的组件中定义mqtt连接的事件处理函数: ```javascript client.on('connect', function () { console.log('connected') }) client.on('message', function (topic, message) { console.log(topic, message.toString()) }) client.on('error', function (error) { console.log(error) }) ``` 6. 在你的组件中订阅mqtt主题: ```javascript client.subscribe('your_topic') ``` 7. 在你的组件中发布mqtt消息: ```javascript client.publish('your_topic', 'your_message') ``` 完整代码示例: ```javascript <template> <div> <p>MQTT</p> </div> </template> <script> import mqtt from 'mqtt' export default { name: 'mqtt', data() { return { client: null } }, created() { const options = { clientId: 'your_client_id', username: 'your_username', password: 'your_password', clean: true } const mqttUrl = 'mqtt://your_mqtt_url:1883' let client = mqtt.connect(mqttUrl, options) client.on('connect', function () { console.log('connected') }) client.on('message', function (topic, message) { console.log(topic, message.toString()) }) client.on('error', function (error) { console.log(error) }) client.subscribe('your_topic') client.publish('your_topic', 'your_message') this.client = client }, beforeDestroy() { this.client.end() } } </script> ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值