A robust Node.js backend service designed to ingest Modbus meter data via a REST API and store it in a PostgreSQL database. This service supports running as a Windows Service for continuous operation.
- REST API: Accepts Modbus meter data via HTTP POST requests.
- Database Integration: Stores parsed meter readings in a PostgreSQL database.
- Windows Service: Built-in support for installation as a background Windows Service (
ModbusMeterService). - Data Processing: automatically calculates power factor and handles data type conversions.
Before running this project, ensure you have the following installed:
- Node.js (v14 or higher recommended)
- PostgreSQL
-
Clone the repository (or extract the project files):
git clone <repository-url> cd IoT-nodejs-modebus-backend-service
-
Install dependencies:
npm install
-
Environment Variables: Create a
.envfile in the root directory if it doesn't exist, or configure your environment with the following variables. Defaults are provided in the code if not specified.PORT=5000 DB_USER=postgres DB_HOST=localhost DB_NAME=Building_Automation_Management_System DB_PASSWORD=your_password DB_PORT=5432
-
Database Setup: Ensure your PostgreSQL database exists and has the required schema. Run the following SQL to create the table:
CREATE SCHEMA IF NOT EXISTS bms; CREATE TABLE IF NOT EXISTS bms.modbus_meter_data ( id SERIAL PRIMARY KEY, meter_id VARCHAR(255), received_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, voltage_a NUMERIC, voltage_b NUMERIC, voltage_c NUMERIC, current_a NUMERIC, current_b NUMERIC, current_c NUMERIC, power_a NUMERIC, power_b NUMERIC, power_c NUMERIC, total_power NUMERIC, energy_import NUMERIC, energy_export NUMERIC, frequency NUMERIC, pf_a NUMERIC, pf_b NUMERIC, pf_c NUMERIC, pf_total NUMERIC, current_demand NUMERIC, max_demand NUMERIC, raw_payload JSONB );
To start the server in development mode with auto-reload:
npm run devThe server will start on http://localhost:5000 (or your configured PORT).
To install and start the application as a Windows Service:
- Open a terminal as Administrator.
- Run the installation script:
node service_install.js
- The service
ModbusMeterServicewill be installed and started automatically.
To uninstall the service (if needed in the future), you would typically use a similar script using svc.uninstall().
- URL:
/api/modbus/meter-data - Method:
POST - Content-Type:
application/json
The API expects a JSON payload with the following structure (keys map to specific database columns):
{
"meter": "Meter_001",
"Ua": 230.5,
"Ub": 231.0,
"Uc": 229.8,
"Ia": 10.5,
"Ib": 11.2,
"Ic": 10.8,
"Pa": 2400,
"Pb": 2500,
"Pc": 2450,
"Pt": 7350,
"PositiveEnergy": 15000.5,
"NegativeEnergy": 50.0,
"freq": 50.0,
"PF_A": 95,
"PF_B": 96,
"PF_C": 94,
"PF_T": 95,
"CDt": 7000,
"MDt": 8000
}- Success (200 OK):
{ "success": true }
server.js: Main entry point of the application.service_install.js: Script to install the app as a Windows Service.controllers/: Contains logic for handling requests (e.g.,modbusController.js).routes/: Defines API endpoints (e.g.,modbusRoutes.js).daemon/: Directory where service logs and executables are stored after installation.