Skip to content

MQTT相关命令

AT+MQTTTLS 设置MQTT的TLS

CommandPossible response(s)
+MQTTTLS=<tls>,<type>,<cert file content>,<cert file size>OK
ERROR
+MQTTTLS?+MQTTTLS:(0-1),<type>,<cert file content>,<cert file size>
+MQTTTLS=?+MQTTTLS:<tls>,<type>,<cert file content>,<cert file size>

参数
<tls>:tls加密使能标志

  • 0:tcp
  • 1:tls

<type>:证书类型

  • 0:根证书名
  • 1:客户端证书名
  • 2:客户端私钥名

<cert file content>:(预存的)证书内容,最多1536字节
<cert file size >:(预存的)指示证书数据长度,不超过1536

举例

发→◇AT+MQTTTLS=?
收←◆+MQTTTLS:(0-1)
收←◆OK
发→◇AT+MQTTTLS=1,0,"-----BEGIN CERTIFICATE-----......",1181 收←◆OK
发→◇AT+MQTTTLS? 收←◆+MQTTTLS:1 收←◆OK

AT+MQTTCONFIG设置MQTT相关参数

CommandPossible response(s)
+MQTTCONFIG=<url>,<port>,<client id>,<cleansession>,<keepalive>,<username>,<password>OK
ERROR
+HTTPCONFIG?+MQTTTLS:<url>,<port>,<client id>,(0-1),<keepalive>,<username>,<passworkd>
+MQTTCONFIG=?+MQTTCONFIG:<url>,<port>,<client id>,(0-1),<keepalive>,<username>,<password>

参数
<url>:服务器IP地址或者域名
<port>:服务器端口号
<client id>:客户端标识
<clean session>:新建会话选项

  • 0:保持原会话
  • 1:新建会话

<keepalive>:发送数据最大间隔时间 <username>:用户名 <password>:密码

举例

发→◇AT+MQTTCONFIG=?
收←◆+MQTTCONFIG:<url>,<port>.<clientid>,(0-1),<keepalive>,<username>,<password>
收←◆OK
发→◇AT+MQTTCONFIG=183.230.40.16,8883,MQTT002,1,60,431671,version=2018 -10-31&res=products%2F431671%2Fdevices%2FMQTT002&et=1635216369&method= md5&sign=UJyDU%2FI3%2FVGCJFjUHhBAXw%3D%3D 收←◆OK
发→◇AT+MQTTCONFIG? 收←◆+MQTTCONFIG:183.230.40.16,8883,MQTT002,1,60,431671,version=2018 -10-31&res=products%2F431671%2Fdevices%2FMQTT002&et=1635216369&method= md5&sign=UJyDU%2FI3%2FVGCJFjUHhBAXw%3D%3D 收←◆OK

AT+MQTTCONNECT 客户端连接服务器

CommandPossible response(s)
+MQTTCONNECT+MQTTCONNECT:0
OK
ERROR

MQTT CONNECT连接超时默认是5S。

举例

发→◇AT+MQTTCONNECT
收←◆+MQTTCONNECT:0
收←◆OK

AT+MQTTPUB 发布消息

CommandPossible response(s)
+MQTTPUB=?+MQTTPUB:<topic>,(0-2),(0-1);<message>
+MQTTPUB=<topic>,<qos>,<retain><message>OK
ERROR

参数
<topic>:发布主题
<qos>:消息质量

  • 0:最多一次
  • 1:最少一次
  • 2:只做一次

<message>:用户消息

举例

发→◇AT+MQTTPUB=?
收←◆+MQTTPUB:<topic>,(0-2),(0-1);<message>
收←◆OK
发→◇AT+MQTTPUB="$sys/431671/MQTT002/dp/post/json",1,0,"{\"id\":123,\"dp\":{\"temperatrue\":[{\"v\":35,}],\"power\":[{\"v\":61,}]}}" 收←◆OK

AT+MQTTSUB 订阅主题

CommandPossible response(s)
+MQTTSUB=<topic>,<qos>OK
ERROR
+MQTTSUB?+MQTTSUB:<topic>,(0-2)
OK

参数
<topic>:订阅主题
<qos>:消息质量

  • 0:最多一次
  • 1:最少一次
  • 2:只做一次

举例

发→◇AT+MQTTSUB="topic1",1
收←◆OK

AT+MQTTUNSUB 取消订阅主题

CommandPossible response(s)
+MQTTUNSUB=<topic>OK
ERROR
+MQTTCONFIG?+MQTTUNSUB:<topic>
OK

参数
<topic>:订阅主题

举例

发→◇AT+MQTTUNSUB="topic1"
收←◆OK

AT+MQTTSTATE 查询连接状态

CommandPossible response(s)
+MQTTSTATE+MQTTSTATE:<status>
OK
ERROR

参数
<status>:状态

  • 0:连接服务器失败
  • 1:连接服务器成功

举例

发→◇AT+MQTTSTATE
收←◆+MQTTSTATE:1
收←◆OK

AT+MQTTDISCONNECT 客户端断开连接

CommandPossible response(s)
+MQTTDISCONNECTOK
ERROR

举例

发→◇AT+MQTTDISCONNECT
收←◆OK

MQTT错误码

code mean
0 MQTT operation completed successfully.
1 MQTT operation queued, awaiting result.
2 Initialization failed.
3 At least one parameter is invalid.
4 MQTT operation failed because of memory allocation failure.
5 MQTT operation failed because the network was unusable.
6 MQTT operation could not be scheduled, i.e. enqueued for sending.
7 MQTT response packet received from the network is malformed.
8 A blocking MQTT operation timed out.
9 A CONNECT or at least one subscription was refused by the server.
10 A QoS 1 PUBLISH received no response and [the retry limit]
11 An API function was called before @ref mqtt_function_init
Back to top