MQTT AT Example ============================================================= This document focuses on running on module devices: :doc:`../command-set/MQTT_AT_Commands` Detailed examples of commands. .. contents:: :local: :depth: 1 .. 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: :doc:`../instruction/other/firmware_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 1. Setting the server domain name Command .. code-block:: none AT+MQTT=1,192.168.31.43 Response .. code-block:: none OK 2.Setting the server port Command: .. code-block:: none AT+MQTT=2,1883 Response: .. code-block:: none OK 3. Set connection mode Command: .. code-block:: none AT+MQTT=3,1 Response: .. code-block:: none OK 4. Set user ID Command: .. code-block:: none AT+MQTT=4,1234 Response: .. code-block:: none OK 5. Set the MQTT user name Command: .. code-block:: none AT+MQTT=5,admin Response: .. code-block:: none OK 6. Set the MQTT password Command: .. code-block:: none AT+MQTT=6,public Response: .. code-block:: none OK 7.Connect to the MQTT server Command: .. code-block:: none AT+MQTT Response: .. code-block:: none OK +EVENT:MQTT_CONNECT Description - The MQTT domain name or IP address you enter may be different from the one in the command above. 8. Belief-l For example, subscribe to a topic for “testtopic0”: Command: .. code-block:: none AT+MQTTSUB=testtopic0,0 Response: .. code-block:: none OK 9. 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: .. code-block:: none AT+MQTTPUB=testtopic1,1,0,HelloWorld Response: .. code-block:: none 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: .. code-block:: none AT+MQTTPUBRAW=testtopic1,1,0,8 Response: .. code-block:: none > Enter the data to be sent .. code-block:: none 1122334455667788 Response: .. code-block:: none OK