You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 17 Current »

Prerequisites:

  • Knowledge of Ignition and Module installation process: Cirrus Link Module Installation
  • Install the following MQTT Modules
    • MQTT Distributor
      • v4.0.X if using Ignition 8.0.x
    • MQTT Engine
      • v4.0.X if using Ignition 8.0.x


Overview:

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.  This method allows arbitrary MQTT messages to be published based on events that exist in Ignition.

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



MQTT Message Publishing via Ignition Script:

Start by opening an Ignition Designer window and double clicking 'Scripting-> Gateway Event Scripts' from the Project Browser as shown below:

Now select 'Tag Change' click the '+' icon at the bottom of the window shown below and create a new Tag Change Script called 'Publish Script'.

Set the name of the script and then select a tag path. In this case we're using an MQTT Engine tag from an emulated device.

Now select the 'Script' tab near the top.  In it, type the following line:

system.cirruslink.engine.publish("Chariot SCADA", "a/b/c", str("hello world").encode(), 0, 0)

Note, the form of the publish method is as follows.  You may need to change the method arguments based on your configuration:

system.cirruslink.engine.publish(String serverName, String mqttTopic, byte[] payload, int qos, boolean retain)

Where:

  • serverName: The server name as shown in the MQTT Engine > Servers configuration
    • Note: This is the Name and not the URL configuration parameter
  • 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

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" : {"requiredState" : {"value_control" :"{\"pump_1\":{\"2\":\"off\",\"1\":\"off\"},},},}}"},"return" : {}}}


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

Finally, save and publish the project.  At this point every time this tags value, quality, or timestamp changes it will result in a MQTT message being published on topic 'a/b/c' with a payload of 'hello world'.


Additional JSON examples:

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

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


Additional Resources

  • No labels