Skip to main content

MQTT Sparkplug B v1.0 Wrapper

Project description

Python Sparkplug B Wrapper

This python module implements an easy way to create Sparkplug B entities on Python, abstracting one level up the already existing python Eclipse Tahu Sparkplug B v1.0 core modules.

The mqtt-spb-wrapper python module provides the following high level objects to handle generic Sparkplug B entities in an easy way:

  • MqttSpbEntityEdgeNode - End of Network (EoN) entity
    • This entity can publish NDATA, NBIRTH, NDEATH messages and subscribe to its own commands NCMD as well as to the STATUS messages from the SCADA application.
  • MqttSpbEntityDevice - End of Network Device (EoND) entity
    • This entity that can publish DDATA, DBIRTH, DDEATH messages and subscribe to its own commands DCMD as well as to the STATUS messages from the SCADA application.
  • MqttSpbEntityApplication - Application entity
    • This entity can publish NDATA, NBIRTH, NDEATH messages and subscribe to its own commands NCMD, to the STATUS messages from the SCADA application as well as to all other messages from the Sparkplug B Domain ID.
  • MqttSpbEntityScada - SCADA entity
    • This entity can publish NDATA, NBIRTH, NDEATH messages as well as to send commands to all EoN and EoND (NCMD, DCMD), and subscribe to all other messages from the Sparkplug B Domain ID.

Other helper classes:

  • MqttSpbPayload
    • Class to decode the Sparkplug B binary payloads ( Google protobuf format )
  • MqttSpbTopic
    • Class to parse, decode and handle MQTT Sparkplug B topics.

Examples

The repository includes a folder with some basic examples for the different type of entities, see the folder /examples in this repository for more examples.

Basic EoN Device

The following code shows how to create an EoND entity that transmit some simple data.

import time
from mqtt_spb_wrapper import *

_DEBUG = True  # Enable debug messages

print("--- Sparkplug B example - End of Node Device - Simple")


def callback_command(payload):
    print("DEVICE received CMD: %s" % (payload))


def callback_message(topic, payload):
    print("Received MESSAGE: %s - %s" % (topic, payload))


# Create the spB entity object
group_name = "Group-001"
edge_node_name = "Gateway-001"
device_name = "SimpleEoND-01"

device = MqttSpbEntityDevice(group_name, edge_node_name, device_name, _DEBUG)

device.on_message = callback_message  # Received messages
device.on_command = callback_command  # Callback for received commands

# Set the device Attributes, Data and Commands that will be sent on the DBIRTH message --------------------------

# Attributes
device.attribures.set_value("description", "Simple EoN Device node")
device.attribures.set_value("type", "Simulated-EoND-device")
device.attribures.set_value("version", "0.01")

# Data / Telemetry
device.data.set_value("value", 0)

# Commands
device.commands.set_value("rebirth", False)

# Connect to the broker --------------------------------------------
_connected = False
while not _connected:
    print("Trying to connect to broker...")
    _connected = device.connect("localhost8", 1883)
    if not _connected:
        print("  Error, could not connect. Trying again in a few seconds ...")
        time.sleep(3)

# Send birth message
device.publish_birth()


# Send some telemetry values ---------------------------------------
value = 0  # Simple counter
for i in range(5):
    # Update the data value
    device.data.set_value("value", value)

    # Send data values
    print("Sending data - value : %d" % value)
    device.publish_data()

    # Increase counter
    value += 1

    # Sleep some time
    time.sleep(5)

# Disconnect device -------------------------------------------------
# After disconnection the MQTT broker will send the entity DEATH message.
print("Disconnecting device")
device.disconnect()

print("Application finished !")

Basic Listener Application

The following code shows how to create an application entity to listen to all Sparkplug B messages on a given group.

import time
from mqtt_spb_wrapper import *

_DEBUG = True  # Enable debug messages

print("--- Sparkplug B example - Application Entity Listener")


def callback_app_message(topic, payload):
    print("APP received MESSAGE: %s - %s" % (topic, payload))


def callback_app_command(payload):
    print("APP received CMD: %s" % payload)


domain_name = "Domain-001"
app_entity_name = "ListenApp01"

app = MqttSpbEntityApplication(domain_name, app_entity_name, debug_info=_DEBUG)

# Set callbacks
app.on_message = callback_app_message
app.on_command = callback_app_command

# Set the device Attributes, Data and Commands that will be sent on the DBIRTH message --------------------------

# Attributes
app.attribures.set_value("description", "Test application")

# Commands
app.commands.set_value("rebirth", False)

# Connect to the broker----------------------------------------------------------------
_connected = False
while not _connected:
    print("Trying to connect to broker...")
    _connected = app.connect("localhost8", 1883)
    if not _connected:
        print("  Error, could not connect. Trying again in a few seconds ...")
        time.sleep(3)

# Send birth message
app.publish_birth()


# Loop forever, messages and commands will be handeled by the callbacks
while True:
    time.sleep(1000)

Eclipse Sparkplug B v1.0

Sparkplug is a specification for MQTT enabled devices and applications to send and receive messages in a stateful way. While MQTT is stateful by nature it doesn't ensure that all data on a receiving MQTT application is current or valid. Sparkplug provides a mechanism for ensuring that remote device or application data is current and valid. The main Sparkplug B features include:

  • Complex data types using templates
  • Datasets
  • Richer metrics with the ability to add property metadata for each metric
  • Metric alias support to maintain rich metric naming while keeping bandwidth usage to a minimum
  • Historical data
  • File data

Sparkplug B Specification: [https://www.eclipse.org/tahu/spec/Sparkplug%20Topic%20Namespace%20and%20State%20ManagementV2.2-with%20appendix%20B%20format%20-%20Eclipse.pdf](https://www.eclipse.org/tahu/spec/Sparkplug Topic Namespace and State ManagementV2.2-with appendix B format - Eclipse.pdf)

Eclipse Tahu spB v1.0 implementation

Eclipse Tahu provide client libraries and reference implementations in various languages and for various devices to show how the device/remote application must connect and disconnect from the MQTT server using the Sparkplug specification explained below. This includes device lifecycle messages such as the required birth and last will & testament messages that must be sent to ensure the device lifecycle state and data integrity.

The mqtt-spb-wrapper python module uses the open source Sparkplug core function from Eclipse Tahu repository, located at: https://github.com/eclipse/tahu (The current release used is v0.5.15 ).

For more information visit : https://github.com/eclipse/tahu

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mqtt_spb_wrapper-1.0.17.tar.gz (25.1 kB view details)

Uploaded Source

Built Distribution

mqtt_spb_wrapper-1.0.17-py3-none-any.whl (23.3 kB view details)

Uploaded Python 3

File details

Details for the file mqtt_spb_wrapper-1.0.17.tar.gz.

File metadata

  • Download URL: mqtt_spb_wrapper-1.0.17.tar.gz
  • Upload date:
  • Size: 25.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.6

File hashes

Hashes for mqtt_spb_wrapper-1.0.17.tar.gz
Algorithm Hash digest
SHA256 fb130b634f7bee158053b48c34d87d0f094af4b2c9b212f1c761a3c03dd4bda7
MD5 040895cc6576dc70702dd7bde69a8e1f
BLAKE2b-256 f0f0d6eb07c12f9989e40518944f73bf2afbabdb7b2e77f2a3f4e02d61ebda25

See more details on using hashes here.

File details

Details for the file mqtt_spb_wrapper-1.0.17-py3-none-any.whl.

File metadata

File hashes

Hashes for mqtt_spb_wrapper-1.0.17-py3-none-any.whl
Algorithm Hash digest
SHA256 9f2e65a09bdd7a8e3bce6a39a00f718d2ff9de8c6180b9f27d27aeff18ff0d63
MD5 11580ec9ff8cffdd87b8572144e5ce37
BLAKE2b-256 06bae8e650fde6f3f4f57f54a624f9d09f59e529211f16244b0ef002c4f9f284

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page