我用emqx在本地搭建了一个MQTT服务器,搭建过程可以查看官网搭建方式
01 连接Wifi
连接WiFi请看上篇帖子
02 配置MQTT
命令: AT+MQTT=<key>,<data>
参数2:<data>
设置的参数
参数1:<key>
MQTT参数。
1:设置连接的域名或 IP
2:设置服务器端口号
3:设置连接方式(暂时默认 1:使用tcp连接)
4:设置客户端ID
5:设置用户名(最大长度63字节)
6:设置登录密码(最大长度63字节)
7:设置遗嘱消息
格式为
AT+MQTT=7,<LWT_topic>,<LWT_qos>,<LWT_Retained>,<LWTpayload>
LWT_topic:遗嘱主题(不需要遗嘱这里设置为空)
LWT_qos:遗嘱 QOS(0/1/2)
LWT_Retained:遗嘱 retained(0/1)
LWTpayload:遗嘱消息内容
示例
AT+MQTT=1,192.168.1.9 // 设置MQTT服务器URL
[AI_HalWriteFlash()-328]flash save ok
[AI_FAtMqtt()-183]AI_HalWriteFlash success
OK
[MEM] After do cmd, available heap 120224
AT+MQTT=2,1883 // 设置MQTT服务器端口
[AI_HalWriteFlash()-328]flash save ok
[AI_FAtMqtt()-183]AI_HalWriteFlash success
OK
[MEM] After do cmd, available heap 120224
AT+MQTT=3,1 // 设置连接方式 TCP
[AI_FAtMqtt()-204]don't need save flash
OK
[MEM] After do cmd, available heap 120224
AT+MQTT=4,bw16 // 设置客户端ID
[AI_HalWriteFlash()-328]flash save ok
[AI_FAtMqtt()-183]AI_HalWriteFlash success
OK
[MEM] After do cmd, available heap 120224
AT+MQTT=5, // 设置用户名 我这里为空
[AI_FAtMqtt()-77]cmd format error
+MQTT:3
ERROR
[MEM] After do cmd, available heap 120224
AT+MQTT=6, // 设置密码 我这里为空
[AI_FAtMqtt()-77]cmd format error
+MQTT:3
ERROR
[MEM] After do cmd, available heap 120224
AT+MQTT? // 查询 MQTT 连接和配置情况
+MQTT:0,192.168.1.9,1883,1,bw16,,,,0,0,
OK
[MEM] After do cmd, available heap 120224
AT+MQTT // 连接MQTT
[AI_MqttPrvMQTTTask()-2351]not set LWT
[AI_MqttPrvMQTTTask()-2357]MQTT connect config
host:192.168.1.9:1883
username:
password:
clientID:bw16
use tcp
OK
[MEM] After do cmd, available heap 103680
[AI_MqttDataHandle()-1507]start MQTT connect
host:192.168.1.9:1883
username:
password:
clientID:bw16
willFlag:0
use tcp
[AI_MqttNetworkConnect()-532]addr=192.168.1.9
[AI_MqttSetStatus()-1487]Set mqtt status to MQTT_CONNECT
[AI_MqttDataHandle()-1544]send mqtt connect cmd success
[AI_MqttDataHandle()-1555]Read packet type is CONNACK
[AI_MqttDataHandle()-1576]recive MQTT Connected ack
// 连接成功
此时,后台会有对应客户端连接
03 订阅消息
命令: AT+MQTTSUB=<topic>,<qos>
参数1:<topic>
要订阅的主题。(最多订阅四个主题)
参数2:<qos>
qos 等级(0,1,2)。
AT+MQTTSUB=topic,0
[AI_MqttSetStatus()-1487]Set mqtt status to MQTT_SUBTOPIC
[AI_MqttAppSubscribeTopic()-2225]send topic topic cmd success,wait ack
[AI_MqttDataHandle()-1555]Read packet type is SUBACK
[AI_MqttDataHandle()-1625]wait subscribe ack success,grantedQoS=0
[AI_MqttSetStatus()-1487]Set mqtt status to MQTT_RUNNING
[AI_MqttAppSubscribeTopic()-2240]recive Subscribe ack,subscribe success
OK
[MEM] After do cmd, available heap 103136
后台显示
当订阅消息后,该主题有消息后,就会通过串口显示
其他客户端发送消息
BW16收到主题
[AI_MqttMessageArrived()-2140]Message arrived
+EVENT:MQTT_SUB,topic,18,{ "msg": "hello" }
04 发布消息
命令: AT+MQTTPUB=<topic>,<qos>,<Retained>,<payload>
参数1:<topic>
要发布的主题
参数2:<qos>
qos 等级(0,1,2)。
参数3:<Retained>
0:普通消息 1:保留消息
参数4:<payload>
数据内容(最大长度为1024个字节)
AT+MQTTPUB=topic,0,0,hello bw16 // 发送消息
[AI_MqttAppPublishMessage()-2097]Publish Topic topic:0
[AI_MqttAppPublishMessage()-2132]Qos0 not need Ack
OK
[MEM] After do cmd, available heap 103136
[AI_MqttDataHandle()-1555]Read packet type is PUBLISH
[AI_MqttMessageArrived()-2140]Message arrived
+EVENT:MQTT_SUB,topic,11,hello bw16 // 接收消息
客户端显示
05 取消订阅
命令: AT+MQTTUNSUB=<topic>
参数1:<topic>
要取消订阅的主题
AT+MQTTUNSUB=topic
[AI_MqttDataHandle()-1555]Read packet type is UNSUBACK
[AI_MqttDataHandle()-1697]topic:topic set AI_MQTT_RECIVE_UNSUBACK
[AI_MqttAppUnsubscribeTopic()-2308]recive UnSubscribe ack,UnSubscribe success
OK
[MEM] After do cmd, available heap 103200
取消订阅成功