MQTT_SUBSCRIBE  topic$, QoS_number (only available on Wx PLC firmware >= F94.2)
Purpose Subscribe to the topic$ with the connected MQTT Broker using a specific QoS_number.
MQTT_SUBSCRIBE "test/topic", 1
Comments: Subscribe to the topic "test/topic" with the MQTT broker with Qos = 1.

  1. Use MQTT_STATUS(1) function to check if the subscribe command is successful. MQTT_STATUS(1) returns a number '4' when it successfully subscribed to the topic and a number '0' if it failed to subscribe to the topic.
  2. You can run this command multiple times to subscribe to multiple topics or a wild-card topic"
  3. When the MQTT broker receives a message from a publisher with the matching topic, it will relay the data to all the devices that has subscribed to the same topic.
  4. When the PLC receives the message from the MQTT broker, it will interrupt the PLC program execution and automatically invoke the callback custom function defined by the MQTT_CONNECT command to handle the received message. The call back function can check the received message topic using the MQTT_TOPIC$() function and the received message data using the MQTT_DATA$() function.
  5. To stop receiving messages from the MQTT broker, the PLC can unsubcribe to the topic$ that it has previously subscribed to by running the MQTT_UNSUBSCRIBE command.
  6. If you enable persistent connection (by running MQTT_CONFIG 1,"1" command" before running the MQTT_CONNECT command) when the PLC connects to the MQTT broker, the broker will remember the topic that the PLC has previously subscribed to. So after the PLC has been disconnected and reconnected to the broker it will automatically subscribe to the same topics that it has previously subscribed to.
  7. When persistent connection is enabled and the PLC is disconnected for a period of time, the broker will also queue the messages matching those topics that the PLC has subscribed to and will deliver all of the messages to the PLC consecutively once the PLC reconnects to the broker.

backbutton.gif (507 bytes)  Basic to TBASIC Reference Manual