How-to
Run Python and Node-RED beside PLC code on an edge PC
Short answer
Keep the IEC 61131-3 program in the controller runtime and run Python and Node-RED as separate Docker containers on the same edge PC. Exchange data over one MQTT interface with a fixed publish period, and declare data stale after three missed periods. One container per workload, and nothing from Python or Node-RED inside the control project.
On this page
- Keep the control project and the containers apart
- Prepare the container host
- Run Node-RED in its own container
- Package the Python model as a second container
- Define the MQTT interface between runtime and containers
- Worked example: 12 tags at 1 Hz with a 3 s timeout
- Test each layer, then the whole chain
- When the controller has no container host
- Questions asked
Run the IEC 61131-3 program in the controller runtime, and run Python and Node-RED as two separate Docker containers on the same edge PC. The three workloads never share a project: the control program publishes tags to an MQTT broker on a fixed period, the containers subscribe, and everything the containers produce returns through variables the control program validates first. Set the stale-data timeout at three times the publish period and test it by stopping each container. One container per workload, nothing merged into the control project — that split is the whole decision.
Keep the control project and the containers apart #
IEC 61131-3 defines the programming languages and the cyclic task model the controller executes; it has no concept of a Python package or a flow engine. That line sets the architecture: control and data acquisition stay in the runtime, and everything that needs Python or Node-RED runs beside it in containers.
| Workload | Where it runs | What it owns |
|---|---|---|
| IEC 61131-3 control and data acquisition | Controller runtime | Process outputs, tag acquisition, validation of every incoming value |
| Python analytics model | Docker container | Predictions computed from published tags |
| Node-RED flows | Docker container | JSON and MQTT handling, dashboards, third-party services |
| MQTT broker | Own container or the existing plant broker | Message delivery between the three |
One container per workload buys the two properties this deployment depends on: the model's dependencies never touch the control runtime, and a model update replaces one container instead of forcing a control-project download.
Prepare the container host #
- Confirm the controller firmware provides a container host, and record the CPU architecture from the system documentation.
- Match the architecture to the image before pulling anything. Node-RED publishes Docker images for amd64, arm32v6, arm32v7, arm64v8 and s390x, so a 64-bit ARM controller and an x64 industrial PC pull the same multi-arch image.
- Set each container's restart policy to
alwaysso the stack returns after a power cycle without a login. - Give every container a persistent volume for its own data — flows, model files, logs — so replacing a container keeps the configuration.
The one-service-per-container pattern carries to storage as well, and Deploying Edge Data Store in Docker on a controller PC applies it to a historian container on the same host.
Run Node-RED in its own container #
- Pull the Node-RED image for the host architecture.
- Run it with the flow-editor port published to the plant network and the persistent volume mounted, so deployed flows survive a container replacement.
- When the flows need nodes the prebuilt image does not carry, build a custom image from a Dockerfile that installs them, and deploy that image instead.
- Point the MQTT nodes at the broker and topics defined below, then deploy the flow.
If a flow has to run directly on the operating system rather than in a container, Install Node-RED natively on a Linux edge computer covers that path. Containers remain the better fit here because the flows then update independently of the control program.
Package the Python model as a second container #
- Write a Dockerfile that installs the model's dependencies at pinned versions, copies the application, and starts it.
- Build the image and run it beside Node-RED with the same restart policy and a persistent volume for the model files.
- Have the application subscribe to the tag topic, compute on its own cadence, and publish results to the command topic.
The container boundary is what makes the analytics stack safe to change on a running line: the runtime never imports anything the model needs, and the model never links against the runtime.
Define the MQTT interface between runtime and containers #
One topic per direction. Illustrative names: edge/line1/tags from the runtime to the containers, edge/line1/model from the model back. Publish tags with QoS 1 (at-least-once delivery) and set the retain flag on the tag message, so a restarted consumer receives the last values the moment it subscribes.
Each payload is JSON with a Unix-milliseconds timestamp and a tag map that names the unit in the identifier:
{"ts": 1718150400123, "tags": {"cmp1_suction_press_kpa": 4120, "cmp1_flow_nm3h": 310.5}}
Ownership and failure behavior, committed:
- The runtime owns the process. Containers publish only to the command topic, and the control program range-checks and rate-limits every received value before it can affect an output — a model result never commands an actuator directly, and stale or missing data leaves the affected function in its defined safe state, not the last commanded one.
- Declare data stale at three times the publish period, and have the consumer drop any payload whose embedded timestamp is older than that.
When the tags are reachable over OPC UA rather than through the runtime's own client, Bridging OPC UA PLC data to MQTT with an edge PC shows the gateway pattern that feeds the same broker.
Worked example: 12 tags at 1 Hz with a 3 s timeout #
Example inputs: 12 measurement tags, published as one JSON message every 1 s, payload 600 bytes.
- Throughput: 600 B/s × 8 bit/B = 4800 bit/s = 4.8 kbit/s. On a 100 Mbit/s link that is 4800 ÷ 100,000,000 = 0.0048 %, so the interface is nowhere near the constraint.
- Stale timeout: 3 × 1 s = 3 s. At a 1 s period that is three missed messages, which separates one lost publish from a dead path.
- Model cadence: compute every 5 s from the latest retained message, and discard any payload older than 5 s by its timestamp, so a redelivered old message cannot be mistaken for live data.
Decision at these numbers: publish at 1 s, time out at 3 s, compute at 5 s. If acquisition later moves to 100 ms, the timeout moves with the rule: 3 × 0.1 s = 0.3 s.
Test each layer, then the whole chain #
- Both containers stopped: the control program runs and acquires its tags normally.
- Node-RED alone: the flow deploys, subscribes, and shows live values.
- The model container alone: a recorded input produces the expected output.
- All three running: a tag change reaches the model and the prediction returns through the command topic with a fresh timestamp.
- Stop the model container mid-run: the runtime declares data stale at the timeout and holds the safe state.
- Power-cycle the host: the restart policy brings the stack back, and the retained message lets the model catch up.
- Log CPU and memory for 24 h with everything running; memory that climbs without settling is a leak to fix before commissioning.
When the controller has no container host #
If the controller firmware offers no container host, the split survives unchanged: leave the runtime on the controller and run the containers on a fanless mini PC mounted in the same cabinet, reaching the controller over the local network through the same MQTT interface.
Questions asked
Does the Python model have to be merged into the IEC 61131-3 project?
No. Keeping the model in a container means a model update swaps one image and restarts one container, while the control program and its download stay untouched. Merging the model into the project would force a control download for every model change and pull the machine-learning stack into the runtime's validation scope.
Which CPU architectures do the Node-RED container images cover?
The published images cover amd64, arm32v6, arm32v7, arm64v8 and s390x. Read the controller's architecture from its system documentation before pulling; the multi-arch image then resolves to the right variant on both a 64-bit ARM controller and an x64 industrial PC.
What happens to the containers after a power cycle?
With the restart policy set to always, the container host starts the broker, Node-RED and the model again without anyone logging in. Because the tag message is retained, each consumer immediately receives the last values on subscribe instead of waiting for the next publish.
Can the stack start on a plant network with no internet access?
Yes, if you pull or build the images during commissioning while connected, because the containers then start from local images. The runtime, broker, Node-RED and the model talk only to each other over the local network, so an offline plant never blocks a restart.