MQTT AT Command Set

This chapter introduces the MQTT AT command set. Adaptations may vary across different modules. For details, see Firmware Versions and Feature Differences.

AT+MQTT configuration and connection

Configure Command

Command:

//配置参数
AT+MQTT=<key>,<data>

Response:

OK

Parameters

  • <key>: MQTT parameter.
    • 1: Set connection domain name or IP

    • 2: Set server port number

    • 3: Set connection method
      • Reserved; currently defaults to 1: use TCP connection

    • 4: Set client ID

    • 5: Set username
      • Maximum length: 63 bytes

    • 6: Set login password
      • Maximum length: 63 bytes

    • 7: Set Last Will and Testament (LWT) message; format: AT+MQTT=7,<LWT_topic>,<LWT_qos>,<LWT_Retained>,<LWTpayload>
      • LWT_topic: LWT topic (set to “” if LWT is not needed)

      • LWT_qos: LWT QoS (0/1/2)

      • LWT_Retained: LWT retained (0/1)

      • LWTpayload: LWT message content

  • <data>: Parameter value to set.

Description

  • Configure MQTT connection parameters.

Execute Command

Command:

//启用连接
AT+MQTT

Response:

OK

Example

//设置域名
AT+MQTT=1,192.168.202.10
OK

//设置端口号
AT+MQTT=2,1883
OK

//设置连接方式
AT+MQTT=3,1
OK

//设置用户 ID
AT+MQTT=4,client_id
OK

//设置 MQTT 用户名
AT+MQTT=5,admin
OK

//设置 MQTT 密码
AT+MQTT=6,public
OK

//设置遗嘱主题 LWTTOPIC,qos0,开启retained,负载消息为 123456;注意:取消遗嘱消息则设置为 AT+MQTT=7,"",0,0,""
AT+MQTT=7,"LWTTOPIC",0,1,"123456"
OK

//查询 MQTT 连接和配置情况
AT+MQTT?
+MQTT:0,192.168.202.10,1883,1,client_id,admin,public
OK

//连接 MQTT
AT+MQTT
OK

//MQTT 连接成功
+EVENT:MQTT_CONNECT

Query Command

Command:

AT+MQTT?

Response:

+MQTT:<MQTT_status>,<Host_name>,<Port>,<scheme>,<client_id>,<username>,<password>,<LWT_topic>,<LWT_qos>,<LWT_Retained>,<LWTpayload>
OK

Parameters

  • <MQTT_status>: MQTT connection status.
    • 0: Initial state

    • 1: Connecting

    • 2: Subscribing to messages

    • 3: Connected

  • <Host_name>: Server domain name.

  • <Port>: Server port number.

  • <scheme>: Connection method
    • 1: TCP connection

  • <client_id>: MQTT client ID.

  • <username>: MQTT username.

  • <password>: MQTT password.

  • <LWT_topic>: LWT topic.

  • <LWT_qos>: LWT QoS.

  • <LWT_Retained>: LWT retained.

  • <LWTpayload>: LWT message content.

Description

  • Note: Configure MQTT parameters before connecting. If an MQTT task is already running, executing again will reconnect (when changing servers, it is recommended to remove all subscriptions before reconnecting).

  • Note: Connection is asynchronous. OK only indicates the MQTT task has started. Query connection status with AT+MQTT? or wait for the URC “+EVENT:MQTT_CONNECT

Example

AT+MQTT? //查询 MQTT 连接和配置情况
+MQTT:0,192.168.202.10,1883,1,client_id,admin,public,LWTTOPIC,0,1,123456
OK

AT+MQTTPUB publish MQTT message

Execute Command

Command:

AT+MQTTPUB=<topic>,<qos>,<Retained>,<payload>

Response:

OK

Parameters

  • <topic>: Topic to publish to.

  • <qos>: QoS level (0, 1, 2).

  • <Retained>:
    • 0: Normal message

    • 1: Retained message

  • <payload>: Data content (maximum length: 1024 bytes)

Description

  • Publish an MQTT message.

Example

AT+MQTTPUB=testtopic,1,0,456
OK

AT+MQTTPUBRAW publish MQTT message of specified length

Execute Command

Command:

AT+MQTTPUBRAW=<topic>,<qos>,<Retained>,<length>

Response:

OK

Parameters

  • <topic>: Topic to publish to.

  • <qos>: QoS level (0, 1, 2).

  • <Retained>:
    • 0: Normal message

    • 1: Retained message

  • <length>: Data length

Description

  • Publish an MQTT message of specified length.

Example

AT+MQTTPUBRAW=testtopic,1,0,10 //向 testtopic 发送 10 字节的数据
> //收到这个字符之后开始输入要发送的数据

OK //当收到 10 字节数据后就会发送数据(可以是任意数据),发送完成会显示 OK

AT+MQTTSUB subscribe to MQTT messages

Execute Command

Command:

AT+MQTTSUB=<topic>,<qos>

Response:

OK

Parameters

  • <topic>: Topic to subscribe to. (Up to four topics)

  • <qos>: QoS level (0, 1, 2).

Description

  • Configure MQTT subscription topics.

Example

AT+MQTTSUB=testtopic0,0
OK

Query Command

Command:

AT+MQTTSUB?

Response:

<status>,<Topic>
...
OK

Parameters

  • <status>: Subscription status.
    • 0: Initial state

    • 1: Subscribing (first subscription)

    • 2: Subscribing (resubscribing after reconnect)

    • 3: Subscribed

  • <Topic>: Subscribed topic.

Description

  • Query configured MQTT subscription topics.

Example

AT+MQTTSUB?
3,testtopic0
3,testtopic1
OK

AT+MQTTUNSUB unsubscribe from MQTT messages

Execute Command

Command:

AT+MQTTUNSUB=<topic>

Response:

OK

Parameters

  • <topic>: Topic to unsubscribe from.

Description

  • Unsubscribe from MQTT messages.

Example

AT+MQTTSUB=testtopic0

OK