MQTT AT Examples
This document provides detailed examples of running MQTT AT Command Set commands on module devices.
Important
The examples in this document assume the device is already connected to Wi-Fi.
These examples may vary depending on the module. Refer to Firmware Versions and Feature Differences for command support on your module.
MQTT Connection over TCP
This example connects to MQTT server IP 192.168.31.43, port 1883, client_id 1234, username admin, password public
Set server domain name
Command:
AT+MQTT=1,192.168.31.43Response:
OK
Set server port
Command:
AT+MQTT=2,1883Response:
OK
Set connection mode
Command:
AT+MQTT=3,1Response:
OK
Set client ID
Command:
AT+MQTT=4,1234Response:
OK
Set MQTT username
Command:
AT+MQTT=5,adminResponse:
OK
Set MQTT password
Command:
AT+MQTT=6,publicResponse:
OK
Connect to MQTT server
Command:
AT+MQTTResponse:
OK +EVENT:MQTT_CONNECT
Note:
The MQTT domain name or IP address you enter may differ from the commands above.
Subscribe to topic
For example, subscribe to topic “testtopic0”:
Command:
AT+MQTTSUB=testtopic0,0Response:
OK
Publish to topic
For small string messages, use AT+MQTTPUB. For example, publish to topic testtopic1 with message HelloWorld:
Command
AT+MQTTPUB=testtopic1,1,0,HelloWorldResponse
OK
For large messages, use AT+MQTTPUBRAW, which supports both string and hex data. For example, publish to topic testtopic1 with message 1122334455667788:
Command
AT+MQTTPUBRAW=testtopic1,1,0,8Response
>Enter the data to send
1122334455667788Response
OK