Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

MQTT Engine provides a mechanism for publishing MQTT Messages from an Ignition script.  This can be useful for general messaging outside of Sparkplug.  For example, one may want to interface to another system that uses MQTT such as when using Custom Namespaces at MQTT Engine. This method allows arbitrary MQTT messages to be published based on events that exist in Ignition.

Note

MQTT Engine must have a client connection to a broker to use the publish mechanism. This is configured here

If the MQTT Engine client connection is configured to use Primary Host ID, the client will include an MQTT Will message in its connect message with a topic in the format "spBv1.0/STATE/primary_host_id" and a payload {"online" : false, "timestamp" : 1668114759262}.
When the MQTT Engine client goes off-line, the broker will publish the MQTT Will message on behalf of MQTT Engine for MQTT clients subscribing on this topic.



MQTT Message Publishing via Ignition Script:

...

  • serverName: The server name as shown in the MQTT Engine > Servers configuration
    • Note: This is the Name and not the URL configuration parameter
    • Version 4.0.23 and newer support either serverName or serverSetName
  • mqttTopic: The MQTT Topic to publish on
  • payload: The MQTT payload
  • qos: The MQTT quality of service to publish on
  • retain: Whether or not to set the retain flag in the MQTT message
Note

If your payload contains escape characters such as backslash you will need to use the 'r' modifier on your payload string to ensure these are included

Example JSON payload:


payload = r{"metadata" : {},"plan" : {"requiredState" : {"value_control" :"{\"pump_1\":{\"4\":\"off\",\"3\":\"off\",\"2\":\"off\",\"1\":\"off\"},},},}}"},"statereturn" : {}},"version" : 12}


When complete, the script should look something like this.  Now click OK.

...

payload=str('{"name":"John", "age":30, "car":"BMW"}').encode()

payload='{"myTestTag":"' + str(system.tag.read("[default]myTestTag").value) + '"}'


Additional Resources