MQTT AT Example¶
This document focuses on running on module devices: MQTT AT command set Detailed examples of commands.
Important
The examples described in this document are based on the premise that the device is connected to Wi-Fi.
This example has different adaptations for different modules. For specific differences, please go to the following steps to
check the support status of the module instruction: Firmware iterations and differences.
Tcp-based MQTT connection¶
In this example, the mqtt server to be connected has ip address :192.168.31.43, port: 1883, client_id: 1234, user name: admin, and password: public
Setting the server domain name
Command
AT+MQTT=1,192.168.31.43Response
OK
2.Setting the server port
Command:
AT+MQTT=2,1883 Response:OK
Set connection mode
Command:
AT+MQTT=3,1 Response:OK
Set user ID
Command:
AT+MQTT=4,1234Response:
OK
Set the MQTT user name
Command:
AT+MQTT=5,adminResponse:
OK
Set the MQTT password
Command:
AT+MQTT=6,public Response:OK
7.Connect to the MQTT server
Command:
AT+MQTTResponse:
OK +EVENT:MQTT_CONNECT
Description
The MQTT domain name or IP address you enter may be different from the one in the command above.
Belief-l
For example, subscribe to a topic for “testtopic0”:
Command:
AT+MQTTSUB=testtopic0,0Response:
OK
Publish Topic
To publish a message with a small amount of data and a string message type, use the directive AT+MQTTPUB, For example, the topic of the published message is testtopic1, and the published message is HelloWorld:
Command:
AT+MQTTPUB=testtopic1,1,0,HelloWorldResponse:
OK
AT+MQTTPUBRAW can be used when a large amount of data is released.The AT+MQTTPUBRAW command does not limit the message type, You can send either a string or hex data.For example, if the topic of the release message is testtopic1 and 1122334455667788:
Command:
AT+MQTTPUBRAW=testtopic1,1,0,8
Response:
>Enter the data to be sent
1122334455667788
Response:
OK