MQTT Client DAT

From TouchDesigner 099 Wiki

Summary

The MQTT Client DAT receives and sends data from/to MQTT devices via MQTT servers (broker). TouchDesigner can act as a client and another computer needs to act as a MQTT Server. Once a client establishes a connection with a server, it can do two things:

  1. Send a message to the server to express interest in any data that has a specific "topic" string. This is called "subscribing". Then the MQTT Client DAT will receive all messages that the server gets with that topic.
  2. Inform the server that it will send messages to the server with a certain topic string, and then send messages with that topic. The messages then get forward to any client that has expressed interest in that topic.

See also MQTT, TCP/IP DAT.

PythonIcon.png mqttclientDAT_Class

Parameters - Connect Page

Active active - Enable the connection.

Network Address netaddress - The IP address to connect to.

Specify ID specifyid - Allows naming the client with parameter User Client ID, otherwise automatically and uniquely generated for each connection.

User Client ID usercid - Client name when Specify ID enabled.

Keep Alive Interval keepalive - Specifies in seconds, the maximum time to expect without communication. If no data is sent during this time, a lightweight ping message is sent to the server instead. Can be set to 0 to avoid pings.

Max In Flight maxinflight - Controls how many messages can be in-flight simultaneously

Clean Session cleansession - If Specify ID is selected, the server will preserve any state information associated with the connection of that ID, such as subscriptions, delivery attempts, etc.

Reconnect reconnect - Will attempt to reconnect to the MQTT broker.

Parameters - Received Data Page

Callbacks DAT callbacks - The Callbacks DAT contains functions that are called when connections are made, lost or published data arrives. See mqttclientDAT_Class for usage.

Execute From executeloc - Determines the location the script is run from.

  • Current Node current - The script is executed from the current node location (for example, where 'cc' points to).
  • Callbacks DAT script - The script is executed from the location of the DAT specified in the Callbacks DAT parameter.
  • Specified Operator op - The script is executed from the operator specified in the From Operator parameter below.

From Operator fromop - The operator whose state change will trigger the DAT to execute its script when Execute From is set to Specified Operator. This operator is also the path that the script will be executed from if the Execute From parameter is set to Specified Operator.

Clamp Output clamp - The DAT is limited to 100 messages by default but with Clamp Output, this can be set to anything including unlimited.

Maximum Lines maxlines - Limits the number of messages, older messages are removed from the list first.

Clear Output clear - Deletes all lines except the heading. To clear with a python script op("opname").par.clear.pulse()

Bytes Column bytes - Outputs the raw bytes of the message in a separate column.

Example

A publicly available MQTT test server/broker is accessible at http://test.mosquitto.org/. It provides a topic under "temp/random" resembling a temperature gauge which you can subscribe but also publish to.

Create a MQTT Client DAT and specify as Network Address test.mosquitto.org:1883 which is the unencrypted MQTT port.

Open up the Textport or use a separate Text DAT and reference the MQTT Client DAT in a variable. After you can use the mqttclientDAT_Class Methods for subscribing and publishing to the gauge. When publishing to it you can see it change in real-time under http://test.mosquitto.org/gauge/.

# reference the MQTT Client DAT
a = op('/project1/mqttclient1')
 
# subscribe to the temp/random topic
a.subscribe('temp/random')
 
# send a single byte, value 23 to the gauge and watch it change on http://test.mosquitto.org/gauge/
a.publish('temp/random',b'23')

Subscribing to a topic that doesn't exist is the same as creating the topic. This makes it very easy to start publishing data on any MQTT Broker.

Another public MQTT Broker can be found at HiveMQ. You can publish and subscribe to topics under broker.hivemq.com, port 1883.

There is a rich amount of resources available on the Internet, a good general introduction in 12 parts can for example be found at HiveMQ