OPC UA to SQLite Data Logger using JavaScript

OPC UA to SQLite Data Logger

This project reads tag values from an OPC UA server and logs them into a SQLite database at a configurable interval. Tag details are loaded from a CSV file, and all configuration is managed via a JSON config file.

Features

  • Batch reading of OPC UA tags for performance
  • Batch logging to SQLite database
  • Change-based logging: only logs to database when tag value changes
  • Automatic OPC UA reconnection: handles connection drops at startup and runtime
  • Configurable OPC UA endpoint, credentials, polling interval, database/table name, and CSV file
  • Debug logging for troubleshooting
  • Graceful shutdown: closes OPC UA session/client and SQLite DB cleanly on Ctrl+C or SIGTERM

Files

  • server.js — Main script for reading OPC UA data and logging to SQLite
  • config.json — Configuration file for endpoint, credentials, polling interval, database/table, and CSV file
  • tags.csv — CSV file listing tag names and nodeIds to read
  • opcua_data.db — SQLite database file (default name, configurable)

Configuration

Edit config.json to set your parameters:

{
  "opcuaEndpoint": "opc.tcp://localhost:4840",
  "opcuaUsername": "", // optional
  "opcuaPassword": "", // optional
  "sqliteDbName": "opcua_data.db",
  "sqliteTableName": "tag_data",
  "pollIntervalMs": 1000,
  "tagsCsvFile": "tags.csv"
}

Tag CSV Format

Example tags.csv:

name,nodeId
Constant,ns=3;i=1001
Counter,ns=3;i=1002
Random,ns=3;i=1003

Usage

  1. Install dependencies:
    npm install
    
  2. Start the logger:
    node server.js
    
  3. Inspect the database using a SQLite client (e.g., DB Browser for SQLite).

Notes

  • Only changed tag values are logged to the database.
  • Automatic reconnection logic ensures the logger recovers from OPC UA connection drops at startup and during runtime.
  • Debug output is printed to the console for each read and database insert.
  • Graceful shutdown: Press Ctrl+C or send SIGTERM to close all connections and DB cleanly.
  • Make sure your OPC UA server is running and accessible.
  • You can change the polling interval, database/table name, and CSV file in config.json.

34.03$

SKU 93 Categories ,

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