How-to
Bidirectional Sparkplug B writes on a CODESYS controller
Short answer
Version V1.3.0.0 of the CODESYS SparkplugB library makes the controller a bidirectional edge node only after both directions are proven: telemetry from EoN to host through the broker, then NCMD and DCMD writes from the Primary SCADA node into a write handler that changes the process value. Broker receipt is delivery, not acceptance.
On this page
- What does the V1.3.0.0 library add on the controller?
- What must already work before you commission Sparkplug?
- How do you prove the telemetry path first?
- How do you attach and remove devices while the node is running?
- Where does a Primary SCADA write stop?
- Example: what do metric aliases save on a 20-device node?
- What test closes out bidirectional operation?
- Questions asked
From version V1.3.0.0, the IEC SparkplugB library turns a CODESYS controller into a bidirectional Sparkplug B edge node: it attaches and removes devices while running and accepts writes from the Primary SCADA node. Commission the two directions separately. Telemetry up first — NBIRTH, DBIRTH and DDATA arriving decoded at the host. Writes down second — NCMD and DCMD from the host, through the broker, into an application write handler that changes a real value. A command observed at the broker is delivery, not acceptance; the last hop, where a metric maps to a controller variable, is where write paths usually stop. Work the checks below in order and declare bidirectional operation only when a value written at the host reads back at the process.
What does the V1.3.0.0 library add on the controller? #
Two capabilities: attaching and removing devices at the EoN at runtime, in any number, and writing data to the EoN or its devices from the Primary SCADA node — the bidirectional half the library's earlier releases did not cover.
Runtime device handling is a managed inventory inside the library: an attached application device gets its DBIRTH published to the host, a removed device drops out of the node's device list, and neither step restarts the broker session. Physical hot-plug remains a property of your field hardware; the release covers the logical device at the EoN.
The write half adds the receive path into your application. The library delivers an incoming NCMD or DCMD metric and your program decides which variable it drives — nothing actuates until that mapping is wired and the application's own permissives allow the write.
The project's usage guides on the CODESYS Forge cover a third-party MQTT broker, a third-party Sparkplug host node, connecting the example project, and integrating the library into an existing project. Bring up the example project before touching production code.
What must already work before you commission Sparkplug? #
The controller, the broker and the Sparkplug host each need a working IP path to the broker before any Sparkplug logic deserves testing, because Sparkplug adds topic and payload rules on top of an MQTT session that must already connect and stay connected.
Check link status, addressing and routing from both clients to the broker listener, and configure the port and TLS mode the broker actually runs — not the values from a generic example. Keep the failure stages distinct: a transport failure ends before any MQTT exchange, an authentication failure rejects the client, and an authorization failure leaves it connected but unable to publish or subscribe where its role requires. A Sparkplug client refused at CONNECT is usually tripping over its will declaration — why a broker rejects your Sparkplug client over its STATE will walks that exact rejection.
The SparkplugB project rides on the CODESYS MQTT client stack, so if the underlying session is not running yet, set up the MQTT Client SL library on a CODESYS controller first and come back to Sparkplug. Decide where the broker lives at the same time; running an MQTT broker in the PLC or on a separate device sets out that trade-off.
If the EoN runs as a soft controller on plant-floor PC hardware rather than on a dedicated PLC, size the machine for TLS session load plus the scan and publishing workload, and deploy it as a fanless industrial mini PC in the cabinet.
How do you prove the telemetry path first? #
Start with a minimal node — one EoN, one device, a few birthed metrics — and do not attempt a write test until the host decodes the hierarchy and shows a controller value changing, because births define every metric a later write can name.
- Set the group ID, EoN ID and device IDs so the controller and host agree on identity before the first connect; the conventions that keep a host's device model consistent are in how to name Sparkplug B group, edge node and device IDs.
- Connect the node and let the library publish the NBIRTH and the device DBIRTH; confirm both appear at the broker.
- Check the host: node and device discovered, and every metric registered with the data type its birth declared.
- Change a harmless monitored value in the controller and confirm the same number reaches the host within one publish interval.
If publications reach the broker but the host shows nothing, the fault is identity, subscription scope or payload decoding — not the network — and it must be fixed here, because every write test downstream assumes this model is current.
How do you attach and remove devices while the node is running? #
Use the library's attach and remove operations on the EoN's managed device inventory: attaching publishes that device's DBIRTH, removing retires it from the node's list, and neither restarts the broker session or the other devices' data.
Prove the full cycle before production: with the baseline inventory visible at the host, attach a test device and confirm the host discovers it and receives current metrics; remove it and confirm the host marks it unavailable instead of showing a stale value as if it were live; reattach and confirm the definitions and values return without touching the EoN connection. Size the inventory honestly — every attached device holds metric definitions in controller memory and in the host's model, so attach what the application needs, not what the runtime could theoretically hold.
Where does a Primary SCADA write stop? #
A write crosses five hops — host publish, broker, the node's NCMD or DCMD subscription, the metric-to-variable mapping, and the write handler — and a broker log proves only the second, so trace until the process value itself moves.
Destination identity causes the first silent failure. An NCMD topic runs spBv1.0, then the group ID, then NCMD, then the edge node ID; a DCMD adds the device as a final level after the edge node ID. A device command must carry the device ID exactly as birthed, and the metric it names must exist in that device's DBIRTH with a matching type. When the deployment uses a primary host, Sparkplug expects the node to hold its side of the exchange until that host's STATE shows online — the closed-looking path that results is the situation behind why MQTT Transmission fails to validate the primary host online.
On the host side a command is an ordinary MQTT publish on those topics; the walkthrough on publishing Sparkplug NCMD and DCMD commands in Ignition 8.1 shows that half end to end. If data flows up while commands never come down, Sparkplug B sends data but receives no SCADA commands lists the usual causes before you localize the stop on the controller side.
| What you observe | Where the path stopped | The proof that moves you forward |
|---|---|---|
| Broker shows no NCMD or DCMD | Host publish configuration or permissions | Broker log records the command on the topic |
| Broker has it, node silent | Destination identity or subscription scope | Node logs receipt of its addressed topic |
| Node receives, value unchanged | Metric never birthed, type mismatch, unwired handler | Application accepts and applies the metric |
| Value changes, then reverts | Cyclic logic or a second writer | The owning logic holds the written value |
The receiver-side classic is a DCMD naming a metric the device never birthed, or a birth type that disagrees with the write; the documented case of a Sparkplug B NullPointerException on DCMD metrics is exactly that mismatch seen from the device end.
Example: what do metric aliases save on a 20-device node? #
Illustrative case: 20 devices at 40 metrics each — 800 birthed metrics — 24-byte names, 200 DDATA updates per second; a sequential two-byte alias in place of every name saves 4.4 kB/s, about 380 MB per day.
Sparkplug B lets a birth assign each metric an unsigned 64-bit alias; from then on the metric name travels only in the NBIRTH or DBIRTH and each DDATA carries the alias. Number the aliases sequentially from 1: values up to 127 varint-encode as a single byte, and beyond that each costs two bytes — so budget the conservative two. Assume the protobuf frame is otherwise identical with and without the alias, so its overhead cancels, and take the 24-byte name as an illustrative average such as Line3.Conveyor.Drive.Torque.
The saving per update is 24 bytes − 2 bytes = 22 bytes. At 200 updates per second the node sheds 200 × 22 B = 4,400 B/s, which is 4.4 kB/s or 35.2 kbit/s of link. Over a day: 4,400 B/s × 86,400 s = 380,160,000 B, about 380 MB counted as 1,000,000 bytes per MB.
The names still travel at every birth, so a rebirth — requested by the host or forced by a reconnect — sends the full names again once per metric. Decision: on a cellular or otherwise narrow plant-to-cloud link, alias every metric; on a switched plant LAN at this scale the saving is optional. The controller-side cost is small — assign the alias at birth and match it in the write handler — and the saving scales with your own name lengths and update rate, not with these illustrative inputs.
What test closes out bidirectional operation? #
Close with one end-to-end write: a known value sent from the host to a named metric, applied by the controller's write handler, visible at the process I/O and back in the host model, recorded with value and timestamp.
- Pick a writable shadow metric, or put the driven device in a safe state first — never commission DCMD writes on an output that moves machinery, and keep hard interlocks in the controller, because a Sparkplug write is not a safety function.
- Publish a DCMD to the device's exact topic from the host, or an NCMD for a node-level metric, and confirm the broker logs it.
- Confirm the node receives the topic and the library hands the metric to your write handler.
- Confirm the handler wrote the variable and the application permissive allowed it; record the value in the controller.
- Read the same value at the process and in the host display, and match the timestamps on both sides.
- Restart the broker, let both clients reconnect and republish their birth certificates, then repeat the write — declare bidirectional V1.3.0.0 operation proven only after the second pass.
Two passes, both showing the same value at process and host, are the acceptance record for bidirectional operation; anything less is still a one-way link.
Questions asked
What happens to a command sent while the edge node is offline?
MQTT delivers a publish to a connected subscriber only; if the node is offline when the host sends, delivery depends on broker session and QoS settings, so treat any command issued during an outage as lost. Re-issue it after both sides are back online, the node has republished its birth certificates, and the host session shows current. Build the operator procedure around resend-after-reconnect, not around queued commands.
How fast does a Sparkplug B write take effect in the controller?
Budget host publish time plus broker turn-around plus one application cycle of the controller. Across a plant LAN that is typically tens of milliseconds of network before the library receives the metric, then the write lands with the next application execution and the physical output follows at the I/O update. Treat the path as supervisory rather than deterministic — it is not a fieldbus replacement.
Which CODESYS runtime does the SparkplugB library need?
The library is developed and documented on its CODESYS Forge project page, which lists the tested runtime and the setup its example project uses; verify your controller's runtime version against that page before planning deployment. Soft controllers on industrial PCs usually offer the most room for device inventories and TLS load, while embedded controllers depend on each vendor's memory and communications budget for the same inventory.
Do stale values remain at the host after a runtime device removal?
The host marks the device offline once the node reports the removal, but many hosts keep the last received values on screen. Treat any value from a removed device as not current: nothing is publishing updates for it until you attach the device again, and the fresh DBIRTH is what re-opens the data path. Trends should show the gap rather than a frozen line.