msvargas / node-logix

Node.js package to read/write data from Allen Bradley Compact/Control Logix PLC's
MIT License
13 stars 3 forks source link

node-logix as Node-RED node #3

Closed EspenHalvorsen closed 5 years ago

EspenHalvorsen commented 5 years ago

Hi, Is there a way to get this as a node in Node-RED running on Hassio? I cannot find it in the pallette.

msvargas commented 5 years ago

Hi, i dont have experience in Node-RED, however @bubtheengineer using with Node-Red maybe he knows something about it Check #1

bubtheengineer commented 5 years ago

I wrote a small script in node.js that takes a tagname as a parameter and uses node-logix to read the tag from the PLC. I then call the script from an execution block in node-red. Works great for my simple use-case.

EspenHalvorsen commented 5 years ago

I see you work in Rockwell Automation. I am a distributor:) Can you send over a flow or other example? Or are you capable of making a node which can be downloaded from the pallette? I am better doing PLC code than IT-coding:)

bubtheengineer commented 5 years ago

I am capable of creating node-red nodes, but I don't have a need for one right now. I'd be happy to create something for you at my Rockwell rate ;D

My read tag example is below, you could also use a similar approach to write tags. This example is reading a tag value every minute and writing the data out as json. This gets forwarded to my emoncms server for logging my water usage.

Here's the node.js script I'm using (wellbutler.js):

"use-strict";

/**
 * @description The simplest example of writing and reading a tag from a PLC
 */

const PLC = require("node-logix").default;
PLC.defaultOptions.Micro800 = true;

const comm = new PLC("192.168.1.15");
const tag=process.argv[2];

comm
  .connect()
  .then(() => {
    comm
      .read(tag)
        .then(res => {
          process.stdout.write('{"'+tag + '":' + res + '}');
          comm.close().then(() => process.exit());
        });
  });

And here's the node-red flow:

[
    {
        "id": "2ebe1419.4fcddc",
        "type": "tab",
        "label": "Flow 4",
        "disabled": false,
        "info": ""
    },
    {
        "id": "943c4d3c.8a67",
        "type": "inject",
        "z": "2ebe1419.4fcddc",
        "name": "1 Min Trigger",
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "repeat": "60",
        "crontab": "",
        "once": true,
        "onceDelay": "1",
        "x": 120,
        "y": 240,
        "wires": [
            [
                "6c6649ae.5c3588"
            ]
        ]
    },
    {
        "id": "6c6649ae.5c3588",
        "type": "exec",
        "z": "2ebe1419.4fcddc",
        "command": "node /usr/local/lib/node_modules/wellbutler.js",
        "addpay": false,
        "append": "Totalized_Flow",
        "useSpawn": "false",
        "timer": "5",
        "oldrc": false,
        "name": "Totalized_Flow",
        "x": 320,
        "y": 240,
        "wires": [
            [
                "ed97042b.ad6208"
            ],
            [],
            [
                "67564dcd.e72ac4"
            ]
        ]
    },
    {
        "id": "ed97042b.ad6208",
        "type": "rbe",
        "z": "2ebe1419.4fcddc",
        "name": "Send on update",
        "func": "rbe",
        "gap": "",
        "start": "",
        "inout": "out",
        "property": "payload",
        "x": 520,
        "y": 220,
        "wires": [
            [
                "eb69dec3.663ad"
            ]
        ]
    },
    {
        "id": "67564dcd.e72ac4",
        "type": "debug",
        "z": "2ebe1419.4fcddc",
        "name": "PLC Errors",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "x": 510,
        "y": 260,
        "wires": []
    },
    {
        "id": "eb69dec3.663ad",
        "type": "json",
        "z": "2ebe1419.4fcddc",
        "name": "",
        "property": "payload",
        "action": "",
        "pretty": false,
        "x": 690,
        "y": 220,
        "wires": [
            [
                "fa7136ec.a35648"
            ]
        ]
    },
    {
        "id": "fa7136ec.a35648",
        "type": "emoncms",
        "z": "2ebe1419.4fcddc",
        "name": "Emoncms Push",
        "emonServer": "4bd5c1e0.4c66",
        "nodegroup": "1",
        "datatype": "fulljson",
        "x": 860,
        "y": 220,
        "wires": []
    },
    {
        "id": "4bd5c1e0.4c66",
        "type": "emoncms-server",
        "z": "",
        "server": "https://emoncms.com",
        "name": "EmonCMS"
    }
]
EspenHalvorsen commented 5 years ago

Thank you for sharing. It's still a bit "black magic" for me, but I will give it a try together with some googling and youtubing:)

It's just for private use. Today I am using modbus, but it would be much better to use the tags directly from my Micro820.

If you one day feel the call of making a node I would be really happy:)