Skip to main content

MQTT Sparkplug B v1.0 Wrapper

Project description

MQTT-SPB-WRAPPER

This python module implements a wrapper around the python Eclipse Tahu Sparkplug B v1.0 core modules, to easily create and handle MQTT Sparkplug B entities in an easy way.

The mqtt-spb-wrapper python module includes the following object classes to handle generic Sparkplug B entities in an easy way:

  • MqttSpbEntityEdgeNode - End of Network (EoN) entity that 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 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 that 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 that 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.

  • MqttSpbTopic - Object to easily 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.13.tar.gz (23.7 kB view details)

Uploaded Source

Built Distribution

mqtt_spb_wrapper-1.0.13-py3-none-any.whl (22.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mqtt_spb_wrapper-1.0.13.tar.gz
  • Upload date:
  • Size: 23.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.2

File hashes

Hashes for mqtt_spb_wrapper-1.0.13.tar.gz
Algorithm Hash digest
SHA256 91ded0c6007415e4007a9eff80804ae0e767eddb272dfc3f7dd7374bba6cfd41
MD5 d10dc2b37f8ed42334c87a6259cc0040
BLAKE2b-256 536612ac04c675b220d3ef557d9088f500466e3e80a49391aaf240c202214417

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mqtt_spb_wrapper-1.0.13-py3-none-any.whl
Algorithm Hash digest
SHA256 005ca9e1a0568afd99ce4aec5d5ed50ae324c133baf0086729102ff902b58809
MD5 02e2a9ffd3c2286b12818810cb97af7f
BLAKE2b-256 4a25cc1d1eadd77670d71993aad826adef30192bc60ad7935c42c1a87c2d129d

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