Skip to content

About

my practice embedded coding practice, practicing python and learning embedded C

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

Industrial Automation Digital Twin using Python, Modbus TCP & Node-RED

Project Overview

This project implements a Digital Twin of an industrial process using a Python-based Modbus TCP server and Node-RED.

The Python application acts as a virtual PLC / industrial process simulator, generating continuously changing sensor values such as:

  • Temperature
  • Pressure
  • Valve Status

The simulated data is exposed through Modbus TCP Holding Registers, which are then read by Node-RED. Node-RED is used for data processing, monitoring, visualization, and alarm generation through a dashboard.

System Architecture

┌──────────────────────────┐
│     Python Simulator     │
│                          │
│  Temperature             │
│  Pressure                │
│  Valve Status            │
└────────────┬─────────────┘
             │
             │ Modbus TCP
             │ Port 5020
             ▼
┌──────────────────────────┐
│       Node-RED           │
│                          │
│  Modbus Read             │
│  Data Processing         │
│  Alarm Logic             │
│  Dashboard               │
└────────────┬─────────────┘
             │
             ▼
┌──────────────────────────┐
│    Digital Twin HMI      │
│                          │
│ Temperature              │
│ Pressure                 │
│ Valve Status             │
│ Trends / Alarms           │
└──────────────────────────┘

Objectives

The main objectives of this project are:

  1. Simulate an industrial process without requiring physical PLC hardware.
  2. Implement a Modbus TCP server using Python.
  3. Generate dynamic sensor values to represent an industrial process.
  4. Use Node-RED to communicate with the virtual PLC.
  5. Visualize real-time process parameters through a dashboard.
  6. Demonstrate the basic architecture of an industrial Digital Twin.
  7. Provide a foundation for adding alarms, control logic, data logging, and predictive maintenance.

Industrial Scenario

The project represents a simplified industrial process containing a temperature sensor, pressure sensor, and control valve.

The Python program acts as a virtual PLC connected to a simulated process.

Every two seconds, the simulator generates new temperature and pressure values:

Temperature: 20–90 °C
Pressure:    90–160 PSI

These values are written into Modbus Holding Registers.

Node-RED periodically reads these registers and presents the information through a graphical dashboard.

Technologies Used

Technology Purpose
Python Industrial process simulation
PyModbus Modbus TCP server
Modbus TCP Industrial communication protocol
Node-RED Data processing and visualization
Node-RED Dashboard HMI / Digital Twin interface
Threading Continuous sensor simulation

Modbus Register Mapping

The simulator uses Holding Registers (HR) for process data.

Register Parameter Unit Example Range
HR0 Temperature °C 20–90
HR1 Pressure PSI 90–160
HR2 Valve Status — 0 / 1

Valve Status

The valve can be represented using:

0 → Closed
1 → Open

Note: The original Python code comments indicate 0 as "Valve Open". The README uses the conventional mapping 0 = Closed and 1 = Open. Ensure that the Python and Node-RED implementations use the same mapping.

Python Modbus TCP Simulator

How It Works

The Python program performs three major functions.

1. Create Modbus Registers

The following holding registers are initialized:

hr=ModbusSequentialDataBlock(
    0,
    [25, 100, 0]
)

This represents:

HR0 = 25 °C
HR1 = 100 PSI
HR2 = Valve Status

2. Simulate Sensors

A background thread continuously generates new values:

temp = random.randint(20, 90)
press = random.randint(90, 160)

The values are then written to the Modbus registers:

store.setValues(3, 0, [temp])
store.setValues(3, 1, [press])

The simulator updates the process every 2 seconds.

3. Start Modbus TCP Server

The Modbus server listens on:

IP Address: 127.0.0.1
Port:       5020

Node-RED can therefore communicate with the virtual PLC using:

127.0.0.1:5020

Installation

Prerequisites

Install the following software:

  • Python 3.x
  • Node.js
  • Node-RED
  • Web browser
  • PyModbus

Clone the Repository

git clone https://github.com/<your-username>/<repository-name>.git
cd <repository-name>

Install Python Dependency

pip install pymodbus

Depending on the PyModbus version being used, the API and imports may differ. This project uses the API style shown in the simulator code.

Running the Python Simulator

Run:

python simulator.py

You should see:

Industrial PLC Simulator started on localhost:5020...

The terminal will then periodically display values similar to:

PLC Update -> Temp: 47°C, Pressure: 132 PSI
PLC Update -> Temp: 63°C, Pressure: 101 PSI
PLC Update -> Temp: 29°C, Pressure: 148 PSI

This confirms that the virtual process is generating dynamic values.

Node-RED Configuration

Start Node-RED:

node-red

Open the Node-RED editor in your browser:

http://localhost:1880

Modbus TCP Connection

Configure the Node-RED Modbus client to connect to:

Host: 127.0.0.1
Port: 5020

Use the appropriate Modbus Read nodes to read the Holding Registers.

Example:

Modbus Read
     │
     ▼
HR0 → Temperature
HR1 → Pressure
HR2 → Valve Status

The exact address configuration may depend on the Node-RED Modbus package and whether it uses zero-based or one-based addressing.

Suggested Node-RED Flow

A basic Digital Twin flow can be structured as:

                 ┌───────────────┐
                 │ Modbus Read   │
                 └───────┬───────┘
                         │
                         ▼
                 ┌───────────────┐
                 │ Function      │
                 │ Data Mapping  │
                 └───────┬───────┘
                         │
             ┌───────────┼───────────┐
             ▼           ▼           ▼
        Temperature   Pressure   Valve Status
             │           │           │
             ▼           ▼           ▼
        Gauge/Chart   Gauge/Chart   Indicator
             │           │           │
             └───────────┼───────────┘
                         ▼
                 ┌───────────────┐
                 │ Digital Twin  │
                 │ Dashboard     │
                 └───────────────┘

Digital Twin Dashboard

The Node-RED dashboard can contain the following components.

Temperature

Display the current temperature using:

  • Gauge
  • Numerical indicator
  • Time-series chart

Example:

Temperature
57 °C

Pressure

Display the current pressure:

Pressure
134 PSI

A time-series chart can be used to visualize pressure variation.

Valve Status

Use an indicator to display the current valve condition:

VALVE STATUS

OPEN

or:

VALVE STATUS

CLOSED

Alarm Logic

Node-RED can implement basic industrial alarm logic.

For example:

Temperature > 80 °C
        ↓
High Temperature Alarm

Similarly:

Pressure > 150 PSI
        ↓
High Pressure Alarm

Example Node-RED function logic:

if (temperature > 80) {
    msg.payload = "HIGH TEMPERATURE ALARM";
}

return msg;

Digital Twin Concept

The project demonstrates the basic concept of a Digital Twin by maintaining a digital representation of an industrial process.

Physical Process
      │
      │ Sensor Data
      ▼
    PLC / RTU
      │
      │ Modbus TCP
      ▼
Digital Representation
      │
      ▼
   Node-RED
      │
      ├── Visualization
      ├── Monitoring
      ├── Alarms
      └── Data Analysis

In this project, the physical process is replaced by a software-based process simulator.

Therefore:

Python Simulator
       ↓
Virtual PLC
       ↓
Modbus TCP
       ↓
Node-RED
       ↓
Digital Twin Dashboard

Recommended Project Structure

digital-twin-node-red/
│
├── simulator.py
├── README.md
│
├── node-red/
│   └── flows.json
│
├── screenshots/
│   ├── dashboard.png
│   └── node-red-flow.png
│
└── docs/
    └── architecture.png

Possible Improvements

The current project can be extended into a more realistic industrial Digital Twin.

1. Realistic Sensor Model

Instead of completely random values, implement:

  • Gradual temperature changes
  • Pressure dynamics
  • Process disturbances
  • Sensor noise
  • Equipment response time

Example:

Temperature
     │
90°C ┤             ╭───
     │          ╭──╯
60°C ┤      ╭───╯
     │   ╭──╯
30°C ┤───╯
     └────────────────── Time

2. Valve Control

Allow Node-RED to send commands back to Python:

Node-RED
   │
   │ Modbus Write
   ▼
Python Simulator
   │
   ▼
Virtual Valve

This would convert the project from a monitoring-only Digital Twin into a closed-loop simulation.

3. Data Logging

Store historical process data using:

  • SQLite
  • InfluxDB
  • PostgreSQL

This enables trend analysis and historical visualization.

4. Predictive Maintenance

Historical data can be analyzed to detect abnormal operating conditions.

Example:

Normal Pressure
      ↓
Increasing Temperature
      ↓
Abnormal Condition
      ↓
Maintenance Alert

5. Advanced Process Simulation

The random-value generator can eventually be replaced by a mathematical process model.

For example:

Tank
 ↓
Pump
 ↓
Valve
 ↓
Pipeline
 ↓
Process

This would make the Digital Twin more representative of a real industrial system.

Security Considerations

This project is intended for education, simulation, and laboratory experimentation.

The Modbus TCP server binds to:

127.0.0.1

which restricts communication to the local machine.

For real industrial deployment, additional considerations would be required, including:

  • Network segmentation
  • Authentication
  • Access control
  • Firewalls
  • Secure industrial protocols
  • PLC safety systems
  • Industrial cybersecurity practices

Learning Outcomes

After completing this project, you should understand:

  • How Modbus TCP communication works
  • How PLC registers can be simulated using Python
  • How sensor data can be generated programmatically
  • How Node-RED communicates with industrial devices
  • How industrial data can be visualized
  • How alarms can be implemented
  • How a basic Digital Twin architecture is structured
  • How a software simulator can replace physical industrial hardware during development

Author

Industrial Automation / Digital Twin Project

Future Scope

The project can be further developed into a complete Industrial Digital Twin platform by integrating:

Python Process Model
        │
        ▼
Virtual PLC
        │
        ▼
Modbus TCP
        │
        ▼
Node-RED
        │
 ┌──────┼─────────┐
 ▼      ▼         ▼
HMI   Database   Alarms
 │      │         │
 └──────┼─────────┘
        ▼
 Analytics / AI
        │
        ▼
Predictive Maintenance

This provides a foundation for demonstrating PLC communication, SCADA/HMI concepts, industrial networking, process monitoring, and Digital Twin technology.

About

my practice embedded coding practice, practicing python and learning embedded C

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors