||Subscribe to the topic$
with the connected MQTT Broker using a specific
MQTT_SUBSCRIBE "test/topic", 1
Subscribe to the topic "test/topic" with the MQTT broker with Qos = 1.
- Use MQTT_STATUS(1) function to check if the subscribe command
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.
- You can run this command multiple times to subscribe to multiple topics or
a wild-card topic"
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.
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$()
- 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.
- 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.
- 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.
Basic to TBASIC Reference Manual