What you’ll build
A simulated industrial sensor system with:- Temperature sensor with sine wave physics
- Pressure gauge with critical alerts
- Real-time MQTT telemetry publishing
- Modbus TCP server for PLC integration
- Dynamic state changes and thresholds
twin:
name: industrial-temp-sensor
physics:
- variable: temperature
strategy: sine
params:
min: 45.0
max: 85.0
- variable: overheat_alert
strategy: script
params:
code: |
if temperature > 80.0 {
return true;
} else {
return false;
}
transports:
- type: mqtt
params:
broker_url: localhost
port: 1883
topic_prefix: factory/line1/temp
- type: http
params:
port: 8090
factory/line1/temptwin:
name: pressure-gauge
physics:
- variable: pressure_psi
strategy: sine
params:
min: 100.0
max: 150.0
- variable: valve_status
strategy: script
params:
code: |
if pressure_psi > 140.0 {
return "critical";
} else {
return "normal";
}
transports:
- type: mqtt
params:
broker_url: localhost
port: 1883
topic_prefix: pipeline/pressure
- type: modbus
params:
port: 5021
# macOS
brew install mosquitto
brew services start mosquitto
# Ubuntu/Debian
sudo apt-get install mosquitto
sudo systemctl start mosquitto
# Docker
docker run -d -p 1883:1883 eclipse-mosquitto
✓ Starting digital twin: industrial-temp-sensor
✓ Physics engine initialized
✓ MQTT transport connected to localhost:1883
✓ HTTP server listening on 0.0.0.0:8090
→ Publishing telemetry every 1s
{
"device": "industrial-temp-sensor",
"timestamp": "2024-01-15T14:30:45Z",
"temperature": 67.5,
"overheat_alert": false
}
{
"device": "industrial-temp-sensor",
"timestamp": "2024-01-15T14:31:12Z",
"temperature": 82.3,
"overheat_alert": true
}
{
"device": "pressure-gauge",
"timestamp": "2024-01-15T14:30:45Z",
"pressure_psi": 125.8,
"valve_status": "normal"
}
{
"device": "pressure-gauge",
"timestamp": "2024-01-15T14:31:08Z",
"pressure_psi": 145.2,
"valve_status": "critical"
}
{
"name": "industrial-temp-sensor",
"uptime_seconds": 347,
"variables": {
"temperature": 72.4,
"overheat_alert": false
},
"last_update": "2024-01-15T14:30:45Z"
}
from pymodbus.client import ModbusTcpClient
client = ModbusTcpClient('localhost', port=5021)
client.connect()
# Read pressure_psi (register 0)
result = client.read_holding_registers(0, 1)
print(f"Pressure: {result.registers[0]} PSI")
# Read valve_status (register 1)
result = client.read_holding_registers(1, 1)
status = "critical" if result.registers[0] == 1 else "normal"
print(f"Valve status: {status}")
client.close()
Key features demonstrated
Physics simulation strategies
Sine wave - Smooth oscillation between min and max:You can reference other variables in scripts. The temperature variable is automatically available.
Actor model architecture
Each twin runs as an independent lightweight process:- Isolated state management
- Concurrent execution
- No shared memory
- Crash isolation
Multi-protocol support
MQTT - Publish/subscribe messaging:Advanced physics strategies
Noise simulation
Add random variation to sensor readings:Linear change
Simulate gradual increases or decreases:Step function
Switch between discrete states:Complex scripting
Implement custom formulas:Real-world use cases
Factory automation testing
Simulate production line sensors without physical equipment:- Temperature monitors
- Conveyor belt speed sensors
- Quality control cameras
- Vibration detectors
Smart home development
Test home automation systems:- Thermostats
- Motion sensors
- Smart locks
- Energy meters
Automotive diagnostics
Mock vehicle sensors:- Engine temperature
- Fuel level
- Tire pressure
- OBD-II data
Agriculture monitoring
Simulate farm sensors:- Soil moisture
- Weather stations
- Irrigation systems
- Greenhouse climate control
Integration examples
Node-RED flow
Connect sensors to Node-RED:- Add MQTT input node
- Set server to
localhost:1883 - Subscribe to
factory/# - Add function node to process data
- Output to dashboard or database
Grafana dashboard
Visualize sensor data:- Install Grafana and InfluxDB
- Use Telegraf to subscribe to MQTT topics
- Store data in InfluxDB
- Create Grafana dashboard with time series panels
- Set up alerts for critical thresholds
PLC integration
Connect to industrial controllers:- Configure PLC to read Modbus TCP
- Point to
localhost:5021 - Map registers to PLC variables
- Use in ladder logic or structured text
- Test control algorithms without hardware
Next steps
- Create multiple sensor fleets with different configurations
- Add network latency simulation for realistic behavior
- Implement custom transport protocols
- Build a complete factory floor simulation
- Integrate with SCADA systems for testing