Uni-Directional Modbus TCP/IP to MQTT Converter using JavaScript for Single Modbus Device

Last Code Revision Date: 02-Sep-2025

Uni-Directional Modbus TCP-IP to MQTT Publisher (Single Device)

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.

Features

  • Polls Modbus registers and publishes to MQTT
  • Supports secure MQTT (TLS/SSL)
  • Configurable via config.json
  • Device metadata included in payloads
  • Reconnect logic for Modbus and MQTT
  • Flexible payload format (JSON or raw)

Prerequisites

  • Node.js (v14 or newer recommended)
  • Access to a Modbus TCP device
  • MQTT broker (local or cloud)

Installation

  1. Clone this repository:
    git clone <your-repo-url>
    cd "Uni-Directional Modbus TCP-IP to MQTT Publisher using JavaScript for Single Device"
    
  2. Install dependencies:
    npm install
    

Configuration

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"
  }
}

Modbus Register Mapping

Each register group in config.json should specify:

  • type: “holding”, “input”, “coil”, or “discrete”
  • startAddress and endAddress: Modbus addresses to poll
  • tags: Array of tag names for each register
  • enabled: Set to true to poll this group

Example:

"registers": [
  { "type": "holding", "startAddress": 0, "endAddress": 2, "tags": ["HRTag0", "HRTag1", "HRTag2"], "enabled": true }
]

Example MQTT Payloads

JSON Format

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"
  }
}

Raw Format

Published to topic: modbus/data/holding/HRTag0

123

Published to topic: modbus/data/holding/HRTag1

456

Usage

  1. Start the application:
    node index.js
    
  2. The app will connect to your Modbus device and MQTT broker, then begin publishing data.

Troubleshooting

  • Check console logs for connection errors.
  • Ensure your Modbus device and MQTT broker are reachable from your network.
  • For secure MQTT, provide a valid CA certificate and set secure to true in config.json.

5.66$

SKU 61 Categories ,

Your Automation Partner. Streamline workflows, and drive productivity. Explore our innovative solutions.