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

  1. Set server domain name

Command:

AT+MQTT=1,192.168.31.43

Response:

OK
  1. Set server port

Command:

AT+MQTT=2,1883

Response:

OK
  1. Set connection mode

Command:

AT+MQTT=3,1

Response:

OK
  1. Set client ID

Command:

AT+MQTT=4,1234

Response:

OK
  1. Set MQTT username

Command:

AT+MQTT=5,admin

Response:

OK
  1. Set MQTT password

Command:

AT+MQTT=6,public

Response:

OK
  1. Connect to MQTT server

Command:

AT+MQTT

Response:

OK

+EVENT:MQTT_CONNECT

Note:

  • The MQTT domain name or IP address you enter may differ from the commands above.

  1. Subscribe to topic

For example, subscribe to topic “testtopic0”:

Command:

AT+MQTTSUB=testtopic0,0

Response:

OK
  1. 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,HelloWorld

Response

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,8

Response

>

Enter the data to send

1122334455667788

Response

OK