Sparkplug B MQTT Simulation

sparkplug b mqtt simulation
Ranjith kumar DSM November 13, 2018

Sparkplug provides an open and freely available specification for how Edge of Network (EoN) gateways or native MQTT enabled end devices and MQTT Applications communicate bi-directionally within an MQTT Infrastructure. One of the unique aspects of MQTT is that it was originally designed for real time SCADA systems to help reduce data latency over bandwidth limited and often unreliable network infrastructure. The intent of the Sparkplug specification is to take full advantage of MQTT’s native Continuous Session Awareness capability as it applies to real time SCADA/IIoT solutions.

Specifications of Sparkplug

 Currently there are two Sparkplug defined encoding schemes that this specification supports.

  • The first one is the Sparkplug A encoding scheme based on the very popular Kura open source Google Protocol Buffer definition.
  • The second one is the Sparkplug B encoding scheme that provides a richer data model developed with the feedback of many system integrators and end user customers using MQTT.

Sparkplug MQTT Topics & Messages

This blog guide you to simulate the Sparkplug B encoding in Bevywise IoT Simulator. Before simulating you must know about the Sparkplug Topic Namespace Elements and Sparkplug MQTT Message Types. 

Sparkplug Topic Namespace Elements:

All Clients which use the Sparkplug spec will use this default UTF-8 Format Topic namespace.

namespace/group_id/message_type/edge_node_id/[device_id]

  • namespace: It is the root element that will define both the structure of the remaining namespace elements as well as the encoding used for the associated payload data. Format for namespace is UTF-8 string constant should be used. The current Sparkplug specification defines two namespaces:
      • Sparkplug payload definition A
        • namespace element should be “spAv1.0”
      • Sparkplug payload definition B
        • namespace element should be “spBv1.0”
  • group_id: This provides unique logical group ID of MQTT EoN nodes. For group_id the format should be  UTF-8 alphanumeric string, except for the reserved characters of ‘+’ (plus), ‘/’ (forward slash), and ‘#’ (number sign). Examples of where the [group_id] might be used include Cement industry where MQTT EoN nodes on different department have the different [group_id].
  • message_type Element: It provide the indication as to, how to handle the MQTT payload of the message. The following message_type elements are defined for the Sparkplug Topic Namespace:
  • NBIRTH – Birth certificate for MQTT EoN nodes.
  • NDEATH – Death certificate for MQTT EoN nodes.
  • DBIRTH – Birth certificate for Devices.
  • DDEATH  – Death certificate for Devices.
  • NDATA    – Node data message.
  • DDATA     – Device data message.
  • NCMD – Node command message.
  • DCMD      – Device command message.
  • STATE – Critical application state message.
  • edge_node_id: Unquie identification for the MQTT EoN nodes within the infrastructure. For edge_node_id the format should be UTF-8 alphanumeric string, except for the reserved characters of ‘+’ (plus), ‘/’ (forward slash), and ‘#’ (number sign). The edge_node_id travels with every message published, so it should be as short as possible
  • device_id: Unquie ID which used to identify the device attached to the MQTT EoN node. device_id is an optional element since some messages will be either originating or destined to the edge_node_id, in that place device_id would not be required. The format of the device_id is a valid UTF-8 alphanumeric String except for the reserved characters of ‘+’ (plus), ‘/’ (forward slash), and ‘#’ (number sign).The device_id travels with every message published, so it should be as short as possible.

Simulate Sparkplug MQTT Device & Network

  • Bevywise IoT Simulator is a simulation tool which help users to test and develop the MQTT/IoT Applications. For installation and setup of Bevywise IoT Simulator, please refer our help doc and video tutorial.
  • After starting the Simulator, create a new network.
  • In new network, create three MQTTEoN nodes by via device creation : spBv1.0, Node2, Node3.
Dashboard of the IoT Simulator

Sparkplug MQTT Message Types

Sparkplug defines the Topic Namespace for set of MQTT messages that are used to manage connection state as well as bidirectional metric information exchange that would apply to many typical real-time SCADA/IIoT, monitoring, and data collection system use cases. The defined message types include:

  • NBIRTH – Birth certificate for MQTT EoN nodes.
  • NDEATH – Death certificate for MQTT EoN nodes.
  • DBIRTH – Birth certificate for Devices.
  • DDEATH  – Death certificate for Devices.
  • NDATA    – Node data message.
  • DDATA     – Device data message.
  • NCMD – Node command message.
  • DCMD      – Device command message.
  • STATE – Critical application state message.

Advantage of using these defined messages:

Using these defined messages the SCADA/IIOT Application can:

  • Discover all metadata and monitor state of any EoN/Device connected to the MQTT infrastructure.
  • Discover all metrics which include all diagnostics, properties, metadata, and current state values.
  • Issue write/command messages to any EoN/Device metric.

Creating Sparkplug MQTT Message types in IoT Simulator

  1. MQTT EoN NBIRTH and NDeath Certificate: The first MQTT message that an EoN node MUST publish upon the successful establishment of an MQTT Session is an EoN BIRTH Certificate.
  • Creating NBIRTH: [namespace/group_id/NBIRTH/edge_node_id]
    • Click spBv1.0 and
    • Create a event with “Whole day” configuration with every 5 sec time interval.
    • In that give the topic namespace as

                                 spBv1.0/kiln/NBIRTH/1

  • Select QoS as 1 and retain as 1
  • Give message as “NBIRTH” and save it
NBIRTH simulation for Sparkplug
  • Creating NDEATH: [namespace/group_id/NDEATH/edge_node_id]
    • Click spBv1.0 and
    • Create a event with “On Disconnect” configuration.
    • In that give the topic as

                                spBv1.0/kiln/NDEATH/1

    • Select QoS as 1 and retain as 1
    • Give message as “NDEATH” and save it.
NDEATH simulation for Sparkplug
  1. MQTT EoN Node Data (NDATA)
  • Once an MQTT EoN node is online with a proper NBIRTH, the NDATA will published.
  • This enables the advantages of the native Continuous Session Awareness of MQTT to monitor the STATE of all connected MQTT EoN node and to rely on Report by Exception (RBE) messages for metric state changes over the MQTT session connection.
  • The payload of NDATA messages will contain any RBE or time based metric EoN node values that need to be reported to any subscribing MQTT clients.
  • Creating NDATA:[namespace/group_id/NDATA/edge_node_id]
    • Click spBv1.0 and
    • Click the + icon and select Behaviour
    • In that, give command topic as “NBIRTH topic namespace” and command “NBIRTH”.
    • Next give the NDATA topic namespace in Event and give Event Data as current time and click save.

                         spBv1.0/Kiln/NDATA/1

    • Now NDATA will published to subscriber once they subscribe the NDATA topic namespace.
MQTT simulator NDATA Sparkplug
  1. Device Birth Certificate[DBRITH]
  • The MQTT EoN node is responsible for the management of all attached physical and/or logical devices. Once the EoN node has published its NBIRTH, the customers application ensure that EoN Node is ONLINE
  • But each physical and/or logical device connected to this node will still need to provide this DBIRTH before consumer applications create/update the metric structure (if this is the first time this device has been seen) and set any associated metrics in the application to a “GOOD” state.
  • Creating the DBRITH:[namespace/group_id/DBIRTH/edge_node_id]
    • Click spBv1.0 and
    • Click the + icon and select Behaviour
    • In that, give command topic as “NBIRTH topic namespace” and command “NBIRTH”.
    • Next give the DBIRTH topic namespace in Event and give Event Data as DBIRTH and click save.

                                   spBv1.0/Kiln/DBIRTH/1

    • Now DBIRTH will published to subscriber once they subscribe the DBIRTH topic namespace.
iot simualtor device birth sparkplug
  1. Device Data Messages (DDATA):
  • Once an MQTT EoN node and associated devices are all online with proper Birth Certificates it is in a mode of quiescent Report by Exception (RBE) reporting of any metric that changes.
  • It take advantage to monitor the STATE of all connected devices and can rely on Report by Exception (RBE) messages for any metric value change over the MQTT session connection.
  • Creating DDATA: [namespace/group_id/DDATA/edge_node_id/device_id]
    • Click spBv1.0 and
    • Click event with “Specific Duration” for 20 min duration with 10 sec interval.
    • In that give the topic as

                                spBv1.0/kiln/DDATA/1/LSM213

    • Select QoS as 1 and retain as 1
    • Give Random message as “ON|OFF” and click save.
Iot Simulator specific duration events
  1. Device Death Certificate[DDEATH]
  • If the device becomes unavailable for any reason (no response, CRC error, etc.) it is the responsibility of the EoN node to publish a DDEATH on behalf of the end device.
  • Once the DDEATH certificate published, any MQTT client subscribed to this device should set the data quality of all metrics to “STALE”.
  • Creating DDEATH:

                  [namespace/group_id/DDEATH/edge_node_id/device_id]

  • Click spBv1.0 and
  • Click the + icon and select Behaviour
  • In that, give command topic as “DDATA topic namespace” and command “OFF”.
  • Next give the DBIRTH topic namespace in Event and give Event Data as DDEATH and click save.

spBv1.0/Kiln/DDEATH/1

  • Now DDEATH will published to subscriber once they subscribe the DBIRTH topic namespace based on the DDATA.
Sparkplug device death MQTT messages
  1.  SCADA/IIoT Host Birth and Death Certificates
  • The SCADA/IIoT Host Node is any MQTT Client application that subscribes to and publishes messages.
  • In an infrastructure, mulitple MQTT Servers provide redundancy and scalability, for that MQTT EoN nodes need to be aware of the “state” of the primary SCADA/IIoT Host application(s).
  • The “state” can be acheived by the unique set of Birth/Death Certificates that the SCADA/IIoT Host MQTT Client MUST publish when a new MQTT session is established.
  • Topic namespace for SCADA/IIOT Host : STATE/scada_host_id
  • It uses an aspect of the MQTT transport called a “RETAINED” publish to maintain the current state of the Primary Host MQTT Client session state to all available MQTT Servers.
  • The format of the scada_host_id can be valid String with the exception of the reserved characters of ‘+’ (plus), ‘/’ (forward slash), and ‘#’ (number sign).
  • Creating SCADA/IIoT Birth Certificate Payload (STATE):
    • Click the SCADA/IIOT and
    • Create events with “On Connect” configuration.
    • In that give topic as SCADA/IIOT Host topic namespace[STATE/scada_host_id]: STATE/SCADABIRTH
    • Set WILL Retain flag as “1” and QoS as “1”
    • Select message type as “Text” and variant as “Constant”.
    • Give Payload or message as “ONLINE“.
IoT Simualtor SCADA Birth command
  • SCADA/IIoT Host Death Certificate Payload (STATE):
  • When the SCADA/IIoT Host MQTT client establishes an MQTT session to the MQTT Server(s), the Death Certificate will be part of the Will Topic and Will Payload registered in the MQTT CONNECT transaction.
  • The Will Topic should be: STATE/scada_host_id
    • Creating SCADA/IIoT DEATHCertificate Payload (STATE):
      • Click the SCADA/IIOT and
      • Click the switch button straight to WILL
      • In that give topic as : STATE/SCADADEATH
      • Give message or payload as “OFFLINE”
      • Set WILL Retain flag as “1” and QoS as “1”
IoT Simulator Will for Sparkplug
  1. MQTT EoN Node Command (NCMD) 
  • The NCMD command topic provides the Topic Namespace used to send commands to any connected EoN nodes.
  • This means sending an updated metric value to an associated metric included in the NBIRTH metric list.
  • Topic namespace: namespace/group_id/NCMD/edge_node_id
  • Creating MQTT EoN Node Command (NCMD)
    • Click spBv1.0 and
    • Create event withInstant command
    • In that give topic as

                           spBv1.0/Kiln/NCMD/1

  • Set QoS to “1” and Retain to “1”
  • Select message type as JSON and click + button.
  • In that add NBIRTH metric like:

ONLINE_STATE and ONLINE TIME.

  • Give the Key as ONLINE_STATE, select “constant”, give value as TRUE and click ADD
  • Next click + button and
  • Give Key as ONLINE_TIME, select “System variable” and select “$Current_time from list
  • next click ADD and save the JSON.
  IoT Simualtor Device NCMD Command
  • Once the NBIRTH occurred, publish the NCMD by clicking the action icon
  • on the left side, now the NCMD will send commands to connected EoN nodes.
  • Next the subscribe the NCMD topic in SCADA/IIOT to get the Update metric from spBv1.0.
  1. Device Command (DCMD)
  • The DCMD topic provides the Topic Namespace used to publish metrics to any connected device.
  • This means sending a new metric value to an associated metric included in the DBIRTH metric list.
  • Topic namespace: namespace/group_id/DCMD/edge_node_id/device_id
  • Creating Device Command (DCMD)
    • Click spBv1.0 and
    • Create event with “Instant command
    • In that give topic as

                             spBv1.0/Kiln/DCMD/1/LSM213

    • Set QoS to “1” and Retain to “1”
    • Select message type as JSON and click + button.
    • In that add NBIRTH metric like:
    • ONLINE_STATE and ONLINE TIME.
    • Give the Key as ONLINE_STATE, select “constant”, give value as TRUE and click ADD
    • Next click + button and
    • Give Key as ONLINE_TIME, select “System variable” and select “$Current_time from list
    • next click ADD and save the JSON.
IoT Simualtor Device DCMD Command
  • Once the DBIRTH occurred, publish the DCMD by clicking the action icon
  • on the left side, now the NCMD will send commands to connected EoN nodes.
  • Next the subscribe the DCMD topic in SCADA/IIOT to get the Update metric from spBv1.0.

These are the Sparkplug set of MQTT Message Types that are used to manage connection state. Next connect the Bevywise IoT Simulator to built-in MQTTBroker or connect to any other MQTTBroker to start the Spatkplug Simulation. Likewise create Sparkplug message type for other Nodes.

You can get started by downloading the IoT Simulator now.

Download IoT Simulator

Feel free to write to support for any questions.