The MQTT protocol defines a lightweight and simple messaging protocol designed for limited devices and networks with low bandwidth, unreliable networks or high latency. The MQTT client uses publish and subscribe methods to communicate over a TCP connection with the MQTT message broker.
Info |
---|
MQTT AT commands are implemented according to MQTT version 3.1.1. |
Note |
---|
Secure MQTT is not supported |
1. Example: Unencrypted, unauthenticated
Code Block | ||
---|---|---|
| ||
# Translates a domain name into an IP address using an existing DNS. > AT+UDNSRN=0,"broker.emqx.io" < +UDNSRN: "3.82.39.163", "44.195.202.69" < OK # Enable URCs > AT+CEREG=2 < OK # Read network registration status > AT+CEREG? < +CEREG: 2,2 < OK # Read network registration status. Wait of +CEREG: 2,5... > AT+CEREG? < +CEREG: 2,5,"AB80","819965",9 < OK # Check the module connected to the network > AT+CGATT? < +CGATT: 1 < OK # Check to see if the PDP context has been activated. > AT+CGACT? < +CGACT: 1,1 < OK # Read IP address from module > AT+CGDCONT? < +CGDCONT: 1,"IP","vgesace.nb.iot","10.254.46.241",0,0,0,0,0,0 < OK # Set MQTT unique client ID. In the example IMEI from device > AT+UMQTT=0,"350857128129037" < +UMQTT: 0,1 < OK # Set MQTT local TCP port number > AT+UMQTT=1,1883 < +UMQTT: 1,1 < OK # Set remote MQTT server's name > AT+UMQTT=2,"broker.emqx.io" < +UMQTT: 2,1 < OK # Set remote MQTT server's IP address > AT+UMQTT=3,"44.195.202.69",1883 < +UMQTT: 3,1 < OK # Connect to the broker > AT+UMQTTC=1 < +UMQTTC: 1,1 < OK < +UUMQTTC: 1,0 """ Repeat the command "AT+UMQTTC=1" if the URC "+UUMQTTC: 1,0" does not arrive. """ # Subscribe to MQTT topic filter > AT+UMQTTC=4,0,"/exelonix/test" < +UMQTTC: 4,1 < OK < +UUMQTTC: 4,1,0,"/exelonix/test" # Publish MQTT message to topic name > AT+UMQTTC=2,0,0,"/exelonix/test","MQTT message" < +UMQTTC: 2,1 < OK # Wait of asynchronous notification (URC) of the reception of an MQTT message < +UUMQTTCM: 6,1 # Read MQTT messages received > AT+UMQTTC=6 < +UMQTTC: 6,1 < OK < +UUMQTTCM: 6,1 < Topic:/exelonix/test < Msg:MQTT message # Disconnect from the broker > AT+UMQTTC=0 < +UMQTTC: 0,1 < OK |
2. Example: Unencrypted, authenticated
Code Block |
---|
# Translates a domain name into an IP address using an existing DNS. > AT+UDNSRN=0,"test.mosquitto.org" < +UDNSRN: "91.121.93.94" < OK # Enable URCs > AT+CEREG=2 < OK # Read network registration status > AT+CEREG? < +CEREG: 2,2 < OK # Read network registration status. Wait of +CEREG: 2,5... > AT+CEREG? < +CEREG: 2,5,"A9F0","819901",7 < OK # Check the module connected to the network > AT+CGATT? < +CGATT: 1 < OK # Check to see if the PDP context has been activated. > AT+CGACT? < +CGACT: 1,1 < OK # Read IP address from module > AT+CGDCONT? < +CGDCONT: 1,"IP","vgesace.nb.iot","10.254.46.27",0,0,0,0,0,0 < OK # Set MQTT unique client ID. In the example IMEI from device AT+UMQTT=0,"350857128129037" +UMQTT: 0,1 OK # Set MQTT local TCP port number AT+UMQTT=1,1884 +UMQTT: 1,1 OK # Set remote MQTT server's name AT+UMQTT=2,"test.mosquitto.org" +UMQTT: 2,1 OK # Set remote MQTT server's IP address AT+UMQTT=3,"91.121.93.94",1884 +UMQTT: 3,1 OK # Set User name and password AT+UMQTT=4,"rw","readwrite" +UMQTT: 4,1 OK # Connect to the broker AT+UMQTTC=1 +UMQTTC: 1,1 OK +UUMQTTC: 1,0 """ Repeat the command "AT+UMQTTC=1" if the URC "+UUMQTTC: 1,0" does not arrive. """ # Subscribe to MQTT topic filter AT+UMQTTC=4,0,"/exelonix/test" +UMQTTC: 4,1 OK +UUMQTTC: 4,1,0,"/exelonix/test" # Publish MQTT message to topic name AT+UMQTTC=2,0,0,"/exelonix/test","MQTT message" +UMQTTC: 2,1 OK # Wait of asynchronous notification (URC) of the reception of an MQTT message +UUMQTTCM: 6,1 # Read MQTT messages received AT+UMQTTC=6 +UMQTTC: 6,1 OK +UUMQTTCM: 6,1 Topic:/exelonix/test Msg:MQTT message # Disconnect from the broker > AT+UMQTTC=0 < +UMQTTC: 0,1 < OK |