Msg from function block

Hello. I have been using net-snmp inside a function node to get some snmpv3 information.

I thanks to some help earlier I was able to get it to send the message with the information I need.

I am wanting to use a switch node though to send the information like temp, voltage, ect to different nodes but I am unable to get it to work. I have 0 js knowledge so i am finding it hard. Hoping somebody can point me in the right direction.

Here is what I have so far.

var user = {
name: "********",
level: snmp.SecurityLevel.authPriv,
authProtocol: snmp.AuthProtocols.md5,
authKey: "**********",
privProtocol: snmp.PrivProtocols.aes,
privKey: "********"
};

var session = snmp.createV3Session ("***.***.***.***", user);

var oids = ["*.*.*.*.*.*.*.*.*", "*.*.*.*.*.*.*.*.*"];

session.get (oids, function (error, varbinds) {
    if (error) {
        console.error (error);
    } else {
        for (var i = 0; i < varbinds.length; i++) {
            if (snmp.isVarbindError (varbinds[i])) {
                console.error (snmp.varbindError (varbinds[i]));
            } else {
                msg.payload.temp = varbinds[0].value;
                msg.payload.current= varbinds[1].value
                msg.payload.voltage = varbinds[2].value
node.send(msg)
            }
        }
    }
    session.close ();
});

Thanks.

In your function node click setup, set outputs to 3

var varbinds = [1,2,3];

var first = {payload: varbinds[0]};
var second = {payload: varbinds[1]};
var last = {payload: varbinds[2]};

return [first,second,last];
[
    {
        "id": "9edca76c921390d3",
        "type": "inject",
        "z": "6c108793ad655479",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "true",
        "payloadType": "bool",
        "x": 170,
        "y": 660,
        "wires": [
            [
                "b5d217c77f1cbbd0"
            ]
        ]
    },
    {
        "id": "b5d217c77f1cbbd0",
        "type": "function",
        "z": "6c108793ad655479",
        "name": "",
        "func": "var varbinds = [1,2,3];\n\nvar first = {payload: varbinds[0]};\nvar second = {payload: varbinds[1]};\n    var last = {payload: varbinds[2]};\n\nreturn [first,second,last];\n",
        "outputs": 3,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 360,
        "y": 660,
        "wires": [
            [
                "1149932b5a5cc64b"
            ],
            [
                "9ab82f9ea4289141"
            ],
            [
                "c11293604c2c05b1"
            ]
        ]
    },
    {
        "id": "1149932b5a5cc64b",
        "type": "debug",
        "z": "6c108793ad655479",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 600,
        "y": 640,
        "wires": []
    },
    {
        "id": "9ab82f9ea4289141",
        "type": "debug",
        "z": "6c108793ad655479",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 610,
        "y": 700,
        "wires": []
    },
    {
        "id": "c11293604c2c05b1",
        "type": "debug",
        "z": "6c108793ad655479",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 590,
        "y": 740,
        "wires": []
    }
]

If you had used the code I gave you in your other post, you would see I pushed the oid into the topic.

Then after the function, you can simply use a switch node to route the message based on msg.topic

Yes I started to play with this at home where I have some more freedom and realised I was to blame for it not working and had my switch node wrong.

Is there a way to push a list in order instead of the oid? So for them oid have it make the topic Temperature?

Thanks again for your help.

var session = snmp.createV3Session ("10.20.155.10", user);

var nastemp = "1.3.6.1.4.1.6574.1.2.0";
var hddtemp1 = "1.3.6.1.4.1.6574.2.1.1.6.0";
var hddtemp2 = "1.3.6.1.4.1.6574.2.1.1.6.1";
var hddtemp3 = "1.3.6.1.4.1.6574.2.1.1.6.2";
var hddtemp4 = "1.3.6.1.4.1.6574.2.1.1.6.3";
var hddtemp5 = "1.3.6.1.4.1.6574.2.1.1.6.4";
var pwrstatus = "1.3.6.1.4.1.6574.1.3.0";

var oids = [nastemp, hddtemp1, hddtemp2, hddtemp3, hddtemp4, hddtemp5, pwrstatus];

session.get (oids, function (error, varbinds) {
    if (error) {
        console.error (error);
    } else {
        for (var i = 0; i < varbinds.length; i++) {
            if (snmp.isVarbindError (varbinds[i])) {
                console.error (snmp.varbindError (varbinds[i]));
            } else {
                msg.payload = varbinds[i].value;
                msg.topic = varbinds[i].oid;
                node.send(msg)
            }
        }
    }
    session.close ();
});

@jonkers71 fyi

I have updated the node-red-node to v1.0.0 complete with snmpve support. You might want to give that a go?

I'd appreciate the feedback

Thanks.

@Steve-Mcl

Have just done a little testing with it. Seems to work very well.

I need to learn more so I can help with this stuff.

2 Likes

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.