Prerequisites:

 

Overview:

Sparkplug is an open source project developed by Cirrus Link Solutions which shows how devices or projects can be enabled to communicate with MQTT Engine and Ignition.  This example shows how to listen to Sparkplug B messages passing through an MQTT Server by subscribing to all Sparkplug B messages.

 

Sparkplug Listener:

This tutorial assumes:

  • Ignition is running and in active trial mode or using a purchased license.
  • MQTT Distributor is installed and running, using the default configuration, and in active trial mode or using a purchased license.
  • MQTT Engine is installed and running, using the default configuration, and in active trial mode or using a purchased license.
  • At least one Sparkplug B client is connected to the MQTT Server and publishing data.  If you don't have one, you can use an emulated version by following one of these tutorials:

With the Sparkplug code downloaded onto your development machine, change into the directory and build the code.  In order for this to work you must have a Java Development Environment and Maven installed.  Also, this example assumes the MQTT Server running is MQTT Distributor running with it's default configuration.  If you are using a different MQTT Server, edit the following file to reflect your MQTT Server configuration:

  • sparkplug_b/tools/java_sparkplug_b_listener/src/main/java//com/cirruslink/example/SparkplugListener.java

 

The most likely candidates for change are the serverUrl, username, and password.  For simplicity this example does not use or support TLS over MQTT without modifications.

With the above steps completed, run the following commands to build the application:

cd sparkplug_b/tools/java_sparkplug_b_listener
mvn clean install


At this point, the application can be run and it will begin showing messages.  To run, use this command:

java -jar target/sparkplug_b_listener-1.1.2-SNAPSHOT.jar


Her are topic and payload examples showing the B: Example Python Client connecting, publishing it's NBIRTH, DBIRTH, and a DDATA message:

  • NBIRTH

    spBv1.0/Sparkplug B Devices/NBIRTH/Python Edge Node 1
    {
        "timestamp" : 1485304768576,
        "metrics" : [ {
            "name" : "bdSeq",
            "timestamp" : 1485304768576,
            "dataType" : "Int64",
            "value" : 1,
            "transient" : false,
            "null" : false,
            "historical" : false
        }, {
         "name" : "Node Control/Next Server",
            "alias" : 0,
            "timestamp" : 1485304768576,
            "dataType" : "Boolean",
            "value" : false,
            "transient" : false,
            "null" : false,
            "historical" : false
        }, {
            "name" : "Node Control/Rebirth",
            "alias" : 1,
            "timestamp" : 1485304768576,
            "dataType" : "Boolean",
            "value" : false,
            "transient" : false,
            "null" : false,
            "historical" : false
        }, {
            "name" : "Node Control/Reboot",
            "alias" : 2,
            "timestamp" : 1485304768576,
            "dataType" : "Boolean",
            "value" : false,
            "transient" : false,
            "null" : false,
            "historical" : false
        }, {
            "name" : "Node Metric0",
            "alias" : 4,
            "timestamp" : 1485304768576,
            "dataType" : "String",
            "value" : "hello node",
            "transient" : false,
            "null" : false,
            "historical" : false
        }, {
            "name" : "Node Metric1",
            "alias" : 5,
            "timestamp" : 1485304768576,
            "dataType" : "Boolean",
            "value" : true,
            "transient" : false,
            "null" : false,
            "historical" : false
        }, {
            "name" : "DataSet",
            "alias" : 3,
            "timestamp" : 1485304768576,
            "dataType" : "DataSet",
            "value" : {
                "numOfColumns" : 3,
                "columnNames" : [ "Int8s", "Int16s", "Int32s" ],
                "types" : [ "Int8", "Int16", "Int32" ],
                "rows" : [ {
                    "values" : [ {
                        "type" : "Int8",
                        "value" : 0
                    }, {
                        "type" : "Int16",
                        "value" : 1
                    }, {
                        "type" : "Int32",
                        "value" : 2
                    } ]
                }, {
                    "values" : [ {
                        "type" : "Int8",
                        "value" : 3
                    }, {
                        "type" : "Int16",
                        "value" : 4
                    }, {
                        "type" : "Int32",
                        "value" : 5
                    } ]
                } ]
            },
            "transient" : false,
            "null" : false,
            "historical" : false
        }, {
            "name" : "Node Metric2",
            "alias" : 6,
            "timestamp" : 1485304768576,
            "dataType" : "Int16",
            "propertySet" : {
                "propertyMap" : {
                    "engUnit" : {
                        "type" : "String",
                        "value" : "MyCustomUnits",
                        "null" : false
                    }
                },
                "names" : [ "engUnit" ],
                "values" : [ {
                    "type" : "String",
                    "value" : "MyCustomUnits",
                    "null" : false
                } ]
            },
            "value" : 13,
            "transient" : false,
            "null" : false,
            "historical" : false
        }, {
            "name" : "_types_/Custom_Motor",
            "timestamp" : 1485304768576,
            "dataType" : "Template",
            "value" : {
                "version" : "",
                "templateRef" : "",
                "metrics" : [ {
                    "name" : "RPMs",
                    "timestamp" : 1485304768576,
                    "dataType" : "Int32",
                    "value" : 0,
                    "transient" : false,
                    "null" : false,
                    "historical" : false
                }, {
                    "name" : "AMPs",
                    "timestamp" : 1485304768576,
                    "dataType" : "Int32",
                    "value" : 0,
                    "transient" : false,
                    "null" : false,
                    "historical" : false
                } ],
                "parameters" : [ {
                    "name" : "Index",
                    "type" : "String",
                    "value" : "0"
                } ],
                "definition" : true
            },
            "transient" : false,
            "null" : false,
            "historical" : false
        } ],
        "seq" : 0,
        "metricCount" : 9
    }
  • DBIRTH

    spBv1.0/Sparkplug B Devices/DBIRTH/Python Edge Node 1/Emulated Device
    {
        "timestamp" : 1485304768577,
        "metrics" : [ {
            "name" : "input/Device Metric0",
            "alias" : 7,
            "timestamp" : 1485304768577,
            "dataType" : "String",
            "value" : "hello device",
            "transient" : false,
            "null" : false,
            "historical" : false
        }, {
            "name" : "input/Device Metric1",
            "alias" : 8,
            "timestamp" : 1485304768577,
            "dataType" : "Boolean",
            "value" : true,
            "transient" : false,
            "null" : false,
            "historical" : false
        }, {
            "name" : "output/Device Metric2",
            "alias" : 9,
            "timestamp" : 1485304768577,
            "dataType" : "Int16",
            "value" : 16,
            "transient" : false,
            "null" : false,
            "historical" : false
        }, {
            "name" : "output/Device Metric3",
            "alias" : 10,
            "timestamp" : 1485304768577,
            "dataType" : "Boolean",
            "value" : true,
            "transient" : false,
            "null" : false,
            "historical" : false
        }, {
            "name" : "My_Custom_Motor",
            "alias" : 11,
            "timestamp" : 1485304768577,
            "dataType" : "Template",
            "value" : {
                "version" : "",
                "templateRef" : "Custom_Motor",
                "metrics" : [ {
                    "name" : "RPMs",
                    "timestamp" : 1485304768577,
                    "dataType" : "Int32",
                    "value" : 123,
                    "transient" : false,
                    "null" : false,
                    "historical" : false
                }, {
                    "name" : "AMPs",
                    "timestamp" : 1485304768577,
                    "dataType" : "Int32",
                    "value" : 456,
                    "transient" : false,
                    "null" : false,
                    "historical" : false
                } ],
                "parameters" : [ {
                    "name" : "Index",
                    "type" : "String",
                    "value" : "1"
                } ],
                "definition" : false
            },
            "transient" : false,
            "null" : false,
            "historical" : false
        } ],
        "seq" : 1,
        "metricCount" : 5
    }
  • DDATA

    spBv1.0/Sparkplug B Devices/DDATA/Python Edge Node 1/Emulated Device
     {
        "timestamp" : 1485304768577,
        "metrics" : [ {
            "name" : "",
            "alias" : 7,
            "timestamp" : 1485304768577,
            "dataType" : "String",
            "value" : "ibwmzplziqsz",
            "transient" : false,
            "null" : false,
            "historical" : false
        }, {
            "name" : "",
            "alias" : 8,
            "timestamp" : 1485304768578,
            "dataType" : "Boolean",
            "propertySet" : {
                "propertyMap" : {
                    "Quality" : {
                        "type" : "Int32",
                        "value" : 500,
                        "null" : false
                    }
                },
                "names" : [ "Quality" ],
                "values" : [ {
                    "type" : "Int32",
                    "value" : 500,
                    "null" : false
                } ]
            },
            "value" : true,
            "transient" : false,
            "null" : false,
            "historical" : false
        } ],
        "seq" : 2,
        "metricCount" : 2
    }

 

  • No labels