IMD SolutionComputing LibraryShop main store

How-to

Run a JSON SCADA MQTT driver instance and extract payload values

Edge computing and IIoT Published 6 min read

Short answer

The broker sees no connection because the Windows service starts the default MQTT driver instance while the connection is configured as instance 2. Run the driver in the foreground as node.exe index.js 2, rebuild the service definition with that same argument, and map values with one topic path per JSON property or with the topicsScripted method; no wildcard expands every property.

On this page
  1. Confirm the fault is at the connection stage
  2. Run the driver with the instance parameter
  3. Recreate the Windows service with the same argument
  4. Extract payload values one property at a time
  5. Switch to topicsScripted when one payload carries many values
  6. Counting topic entries for the nine-property power payload
  7. Questions asked

The JSON SCADA MQTT Sparkplug-B driver never reaches the broker because the Windows service launches the default driver instance while the connection is configured as instance 2. Stop the service, run the driver in the foreground with the instance argument on the command line, confirm the broker logs the connection, then rebuild the service definition with that same argument. After values arrive, map them into tags one JSON property at a time with a topic entry such as topicname/$.ActivePower, or switch the instance to the topicsScripted method when one payload carries dozens of values. No wildcard expands every property of a payload; the supported choices are explicit entries or a script.

Confirm the fault is at the connection stage #

The chain from driver to tag runs in four stages: the Windows service starts the Node.js process, the process loads the driver instance given on its command line, the client connects to the configured broker endpoint, and the broker logs the connection before any payload parsing or tag creation happens.

A broker that logs no connection from the MQTT Sparkplug-B driver has stopped that chain at the third stage or earlier, so JSONPath expressions and topic entries cannot be the cause — no payload has arrived yet. A working Telegraf client on the same host, reaching the same Mosquitto listener, proves the broker listens and is reachable; it does not prove the native driver process is running, that it selected the instance you configured, or that it uses the same endpoint and credentials. Read the driver's configured broker address, port and credentials and compare them with the client that works. A client that stays offline until you point it at the broker IP address instead of a host name is the same stage of fault, described in MQTT sink stays offline until you use the broker IP address.

Run the driver with the instance parameter #

The driver takes its instance number from a command-line argument. With no argument the process starts instance 1; a connection configured as instance 2 needs that number on the command line. The configured connection and the argument have to name the same instance, otherwise the process runs a driver that no connection in the configuration refers to.

Command Driver instance selected
node.exe index.js 1
node.exe index.js 1 1
node.exe index.js 2 2

For a foreground diagnostic run on Windows:

  1. Stop the MQTT driver service first, so the foreground process is the only copy holding that instance. On a host that is collecting plant data, stop it in a window where a pause in acquisition is acceptable.

  2. Open a command prompt and change to the driver folder:

    cd \json-scada\src\mqtt-sparkplug
    
  3. Start the driver with the Node.js runtime shipped with the platform, instance argument on the end:

    c:\json-scada\platform-windows\nodejs-runtime\node.exe index.js 2
    
  4. Read the console output for startup and connection messages that the service does not surface.

  5. Check the broker log for the connection from the driver client.

Omitting the trailing 2 selects instance 1. Do not rebuild the service or touch topic entries until the broker records this connection.

Recreate the Windows service with the same argument #

The default mqttsparkplugclient service is equivalent to running the driver with no argument, or with an explicit 1, so it always selects instance 1. It does not pick up instance 2 because a command prompt once used it, and service startup is a separate check from manual startup: a working interactive run proves the driver, not the service definition.

  1. Confirm the foreground run from the previous section connects, with the instance argument included.
  2. Recreate the service definition so its executable and argument list match the working command exactly. The project keeps its Windows service definitions in the create_services.bat script in the platform-windows folder, and its install documentation describes converting JSON SCADA processes into Windows services with the NSSM tool; follow those patterns rather than hand-editing an existing definition.
  3. Start the service.
  4. Verify the running process actually carries the argument, then check the broker log for a connection arriving after the service start. A connection that appears after service startup, not after a manual run, is the proof that the rebuilt definition works.

Extract payload values one property at a time #

With the broker logging the driver client, map payload values through the topic entries in the connection configuration. A topic entry on its own produces one tag named after the topic and no value, because the entry never names a key to read. To extract a value, pair the subscribed topic name with a JSONPath that names one payload property, in the form topicname/$.ActivePower.

For a payload published as:

{"ActivePower":20.47765,"ReactivePower":12.163717,"PowerFactor":0.8618866,"Frequency":50.18043,"VoltageLine":390.2411,"VoltagePhase":225.6683,"Current":35.47994,"ActiveEnergy":868352.0,"ReactiveEnergy":160743.6}
Payload property Topic entry Extracted value
ActivePower topicname/$.ActivePower 20.47765
Frequency topicname/$.Frequency 50.18043
VoltageLine topicname/$.VoltageLine 390.2411

Two checks make each entry work: the topic string must match the published topic exactly, and the property name after $. must match the JSON key's spelling exactly, including case. An expression that would expand every root property in one entry, such as MyTopicName/$.*~, is not supported; extraction works one explicit property path at a time or through the scripted method below. Where the values sit inside nested JSON objects rather than at the root, the same one-property-per-entry logic applies, and SCADA MQTT points blank with nested JSON payloads walks through that payload shape.

Switch to topicsScripted when one payload carries many values #

The topicsScripted method is the driver's documented route for complex payloads: the instance subscribes to the topics and a script extracts the payload data into values and tag data, replacing a long list of per-property entries with one subscription and one script.

  1. Set the topic subscription or subscriptions on the instance so the payloads that matter reach the driver.
  2. Write the extraction script against the payload's actual JSON structure, using the publisher's exact property names; the driver's documentation in the mqtt-sparkplug source folder includes an example script for extracting data from complex payloads.
  3. Test the script on a received payload and compare every extracted value with what the publisher sent.
  4. Keep the script and the payload aligned from then on: a property that is renamed, omitted or moved must be reflected in the script, or that tag stops updating.

Use explicit topic entries for a small set of stable values and topicsScripted once the count grows into dozens; the worked example below puts numbers on that choice.

Counting topic entries for the nine-property power payload #

Inputs:

  • The published payload: one JSON object with nine root properties, from ActivePower through ReactiveEnergy, published by the meter to the broker — the payload shown in the extraction section above.
  • Seven working entries: this case configured seven topicname/$.property strings in the driver instance and received values for them.
  • Thirty variables: the case's planned expansion, the size at which maintaining individual entries was judged unreasonable.

Rule: per-property extraction costs one topic entry per extracted value, while the topicsScripted method costs one topic subscription plus one script no matter how many properties the payload carries.

Arithmetic:

  • Today's payload: 9 properties × 1 entry per property = 9 entries if every value is wanted; 7 exist, so 9 − 7 = 2 more entries would cover the payload.
  • The expansion: 30 variables × 1 entry per variable = 30 entries to create and maintain, against 1 subscription + 1 script = 2 configuration items for the scripted method.

Result: nine values cost nine explicit entries today; the thirty-variable set costs thirty entries under per-property mapping, or two items under the scripted method.

Decision: keep the seven working entries while this payload is all the driver reads. Move the instance to topicsScripted for the thirty-variable expansion — at thirty variables and over, one entry per value stops being maintainable.

Questions asked

Can two driver types both use instance 1 at the same time?

Yes. JSON SCADA counts driver instances separately for each driver type, so an MQTT instance 1 and an IEC 104 instance 1 can run side by side. The instance number only has to be unique within one driver type. That is also why the MQTT driver needs its own explicit argument: nothing in another driver's numbering tells the MQTT process which instance to start.

Why does a topic entry create a tag but no value?

Without a property path, the driver creates one tag named after the topic and leaves it without a payload value, because the entry never names a JSON key to read. Adding a path that ends in a property name, in the form topicname/$.ActivePower, tells the driver to read exactly that key. Check the spelling against the payload, because a key that is not spelled exactly extracts nothing.

Does the foreground run keep collecting data after the window closes?

No. A command-prompt run stops when the session ends or the host reboots, so it is a diagnostic and commissioning step only. Continuous collection needs the Windows service, rebuilt with the same instance argument that worked in the foreground. Verify the rebuilt service with the broker log, because the service definition carries its own argument list and does not inherit what was typed manually.

Does the driver convert the extracted values to engineering units?

No. The driver stores the number exactly as the publisher sent it and applies no unit conversion. If the publisher sends power in kilowatts, the tag reads kilowatts; scale and label either at the publisher or inside a topicsScripted extraction script. Confirm one reading against a known figure from the device before trusting the mapping in alarms or records.

Keep reading

  • How-to

    How Geo SCADA publishes values to MQTT

    Geo SCADA Expert publishes outbound values as control writes to MQTT Text driver points: the topic lives in each point's Control settings and the control value becomes the payload, while the point's readback is irrelevant. Sparkplug payloads need the Sparkplug B driver. Data arriving from Node-RED proves only the inbound path.

    Edge computing and IIoT

  • Explainer

    Shrinking a Sparkplug JSON payload on a narrow link

    Casting the JSON to bytes changes the Sparkplug datatype tag, not the size: String and Bytes metrics both ride one protobuf length-delimited field with the same length prefix. The example's ~700-byte object minifies to ~400 bytes and shrinks further as typed metrics with aliases; with a 1460-byte IPv4 segment limit, the gain is bytes on the link, not packets.

    Edge computing and IIoT

  • Troubleshooting

    Why a quoted MQTT JSON value fails an analog point

    MQTT forwards your payload bytes unchanged, so a quoted "868.9640" is a JSON string and an analog point rejects it. Either publish a real JSON number at the source, or convert the text in a receiver that parses it, as Geo SCADA 2023's Text JSON driver did in the reported case. Confirm with one controlled test that changes only the quotation marks.

    Edge computing and IIoT

Managing recipes on Ignition Edge without a database