Last Code Revision Date: 02-Sep-2025
This project connects a single Modbus TCP device and publishes its data to an MQTT broker. It is designed for industrial automation and IoT integration.
config.json
git clone <your-repo-url>
cd "Uni-Directional Modbus TCP-IP to MQTT Publisher using JavaScript for Single Device"
npm install
Edit config.json
to match your device and MQTT broker settings. Example:
{
"device": {
"name": "ModbusDevice01",
"location": "Factory Floor",
"description": "Single Modbus TCP device publishing to MQTT"
},
"modbus": {
"ip": "127.0.0.1",
"port": 502,
"slaveID": 1,
"interval": 1000,
"reconnect": { "maxRetries": 5, "retryInterval": 2000 },
"registers": [
{ "type": "holding", "startAddress": 0, "endAddress": 2, "tags": ["HRTag0", "HRTag1", "HRTag2"], "enabled": true }
]
},
"mqtt": {
"broker": { "ip": "broker.hivemq.com", "port": 1883, "username": "", "password": "", "secure": false, "caFile": "" },
"baseTopic": "modbus/data",
"reconnect": { "maxRetries": 5, "retryInterval": 2000 },
"payloadFormat": "json"
}
}
Each register group in config.json
should specify:
type
: “holding”, “input”, “coil”, or “discrete”startAddress
and endAddress
: Modbus addresses to polltags
: Array of tag names for each registerenabled
: Set to true
to poll this groupExample:
"registers": [
{ "type": "holding", "startAddress": 0, "endAddress": 2, "tags": ["HRTag0", "HRTag1", "HRTag2"], "enabled": true }
]
Published to topic: modbus/data/Holding Register
{
"HRTag0": 123,
"HRTag1": 456,
"HRTag2": 789,
"timestamp": "2025-09-02T12:34:56.789Z",
"device": {
"name": "ModbusDevice01",
"location": "Factory Floor",
"description": "Single Modbus TCP device publishing to MQTT"
}
}
Published to topic: modbus/data/holding/HRTag0
123
Published to topic: modbus/data/holding/HRTag1
456
node index.js
secure
to true
in config.json
.5.66$