generic-mqtt-to-sparkplugb

Sparkplug is a specification for MQTT enabled devices and applications to send and received messages in a stateful way. Generic MQTT doesn't ensure all data on a receiving MQTT application is current or valid. With Sparkplug, it can provide a mechanism for ensuring that the remote device is current and valid.

The MQTT function on the MGate 5105 and NPort IA5105 does not include the device lifecycle messages such as the required birth and last will and testament message that must be sent to ensure the device lifecycle and data and integrity. However, it is still possible to send data to a Ignition (Sparkplug).

[{"id":"908e0854.b45898","type":"tab","label":"VFD/RFID","disabled":false,"info":""},{"id":"88477fb0.59047","type":"sparkplug","z":"908e0854.b45898","name":"","broker":"tcp://192.168.118.60","port":"1883","clientid":"RFID","groupid":"Sparkplug Devices","edgenode":"Node-RED Edge Node -- Serial","version":"spBv1.0","enablecache":"false","publishdeath":"false","user":"admin","password":"__PWRD__","x":560,"y":343.3333435058594,"wires":[["ec432ea9.94bb","7a37007d.4af6f"]]},{"id":"6fc3becc.7363c","type":"function","z":"908e0854.b45898","name":"DDATA","func":"var devID = \"VFD\",\n    timestamp = parseISO8601(msg.payload.dateTime)\n\nfunction parseISO8601(n) {\n    var newDate = new Date(n)\n    var timestamp = newDate.getTime()\n    return timestamp\n}\n\ngetTopic = function(type) {\n    return devID + \"/\" + type;\n}\n\ngetBirthPayload = function() {\n    return {\n        \"timestamp\" : new Date().getTime(),\n        \"metrics\" : [\n            { \"name\" : \"Payload/gwID\", \"value\" : \" \", \"type\" : \"string\" },\n            { \"name\" : \"Payload/devID\", \"value\" : \" \", \"type\" : \"string\"},\n            {\n                \"name\" : \"VFD/Rated_Motor_Speed\",\n                \"value\" : 0.0,\n                \"type\" : \"float\"\n            },\n            {\n                \"name\" : \"VFD/Rated_Motor_Frequency\",\n                \"value\" : 0.0,\n                \"type\" : \"float\"\n            },\n            {\n                \"name\" : \"VFD/Rated_Motor_Voltage\",\n                \"value\" : 0.0,\n                \"type\":  \"float\"\n            },\n            {\n                \"name\" : \"VFD/Rated_Motor_Current\",\n                \"value\" : 0.0,\n                \"type\": \"float\"\n            }\n        ]};\n}\n\ngetDataPayload = function() {\n    return {\n        \"timestamp\" : timestamp,\n        \"metrics\" : [\n            { \"name\" : \"Payload/gwID\", \"value\" : msg.payload.gwID, \"type\" : \"string\" },\n            { \"name\" : \"Payload/devID\", \"value\" : msg.payload.msgID, \"type\" : \"string\"},\n            {\n                \"name\" : \"VFD/Rated_Motor_Speed\",\n                \"value\" : msg.payload.VFD_Tags.Rated_Motor_Speed,\n                \"type\" : \"float\"\n            },\n            {\n                \"name\" : \"VFD/Rated_Motor_Frequency\",\n                \"value\" : msg.payload.VFD_Tags.Rated_Motor_Frequency,\n                \"type\" : \"float\"\n            },\n            {\n                \"name\" : \"VFD/Rated_Motor_Voltage\",\n                \"value\" : msg.payload.VFD_Tags.Rated_Motor_Voltage,\n                \"type\":  \"float\"\n            },\n            {\n                \"name\" : \"VFD/Rated_Motor_Current\",\n                \"value\" : msg.payload.VFD_Tags.Rated_Motor_Current,\n                \"type\": \"float\"\n            }\n        ]};\n};\n\nif (msg.topic == \"rebirth\") {\n    return {\n        \"topic\" : getTopic(\"DBIRTH\"),\n        \"payload\" : getBirthPayload()\n    };\n}\n\nif (msg.topic === \"VFD\") {\n    msg.topic = getTopic(\"DDATA\");\n    msg.payload = getDataPayload();\n    return msg;\n}\n\nreturn null;","outputs":1,"noerr":0,"x":563,"y":400.3333740234375,"wires":[["6bde269d.1733a8","e288712b.3957d"]]},{"id":"fdb27e83.f559b","type":"mqtt in","z":"908e0854.b45898","name":"","topic":"VFD","qos":"0","broker":"563040b7.36d18","x":195,"y":401,"wires":[["be0f199b.e9a7b8"]]},{"id":"be0f199b.e9a7b8","type":"json","z":"908e0854.b45898","name":"","property":"payload","action":"","pretty":false,"x":339,"y":401,"wires":[["6fc3becc.7363c"]]},{"id":"901e37eb.2eb678","type":"mqtt in","z":"908e0854.b45898","name":"","topic":"NPortIO/JSON/SPort1/Pub/Data","qos":"0","broker":"563040b7.36d18","x":276.5,"y":124,"wires":[["b166e64f.70ee18"]]},{"id":"b166e64f.70ee18","type":"json","z":"908e0854.b45898","name":"","property":"payload","action":"","pretty":false,"x":338.5,"y":189,"wires":[["15b59e25.e43f52"]]},{"id":"15b59e25.e43f52","type":"function","z":"908e0854.b45898","name":"Decode Base54","func":"var msgFrame = new Buffer(msg.payload.msgFrame, 'base64')\nvar newFrame = msgFrame.toString('hex');\n\nfunction hex_to_ascii(str1)\n {\n\tvar hex  = str1.toString();\n\tvar str = '';\n\tfor (var n = 0; n < hex.length; n += 2) {\n\t\tstr += String.fromCharCode(parseInt(hex.substr(n, 2), 16));\n\t}\n\treturn str;\n }\n\nlet n = hex_to_ascii(newFrame)\nn = n.replace().replace( /\\D+/g, '');\n\nlet long = Number(n)\nvar string = long.toString()\nmsg.payload.msgFrame = string;\n\nreturn msg;","outputs":1,"noerr":0,"x":350,"y":243,"wires":[["ec432ea9.94bb"]]},{"id":"ec432ea9.94bb","type":"function","z":"908e0854.b45898","name":"NPort","func":"var gwID = msg.payload.gwID,\n    devID = \"RFID\",\n    data = msg.payload.msgFrame,\n    timestamp = parseISO8601(msg.payload.dateTime)\n\nfunction parseISO8601(n) {\n    var newDate = new Date(n)\n    var timestamp = newDate.getTime()\n    return timestamp\n}\n\ngetTopic = function(type) {\n    return devID + \"/\" + type;\n}\n\ngetBirthPayload = function() {\n    return {\n        \"timestamp\" : new Date().getTime(),\n        \"metrics\" : [\n            { \"name\" : \"Payload/gwID\", \"value\" : \" \", \"type\" : \"string\" },\n            { \"name\" : \"Payload/msgFrame\", \"value\" :  \" \", \"type\" : \"string\" }\n        ]\n    };\n}\n\ngetDataPayload = function() {\n    return {\n        \"timestamp\" : timestamp,\n        \"metrics\" : [\n            { \"name\" : \"Payload/gwID\", \"value\" : gwID, \"type\" : \"string\" },\n            { \"name\" : \"Payload/msgFrame\", \"value\" : data, \"type\" : \"string\" }\n        ]\n    };\n};\n\nif (msg.topic == \"rebirth\") {\n    return {\n        \"topic\" : getTopic(\"DBIRTH\"),\n        \"payload\" : getBirthPayload()\n    };\n}\n\nif (msg.topic === \"NPortIO/JSON/SPort1/Pub/Data\") {\n    msg.topic = getTopic(\"DDATA\");\n    msg.payload = getDataPayload();\n    return msg;\n}\n\nreturn null;","outputs":1,"noerr":0,"x":555.5,"y":274,"wires":[["88477fb0.59047","7a37007d.4af6f"]]},{"id":"7a37007d.4af6f","type":"debug","z":"908e0854.b45898","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":810,"y":341,"wires":[]},{"id":"6bde269d.1733a8","type":"sparkplug","z":"908e0854.b45898","name":"","broker":"tcp://192.168.118.60","port":"1883","clientid":"VFD","groupid":"Sparkplug Devices","edgenode":"Node-RED Edge Node -- FieldBus","version":"spBv1.0","enablecache":"false","publishdeath":"false","user":"admin","password":"__PWRD__","x":564,"y":460,"wires":[["6fc3becc.7363c","e288712b.3957d"]]},{"id":"e288712b.3957d","type":"debug","z":"908e0854.b45898","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":812,"y":400,"wires":[]},{"id":"563040b7.36d18","type":"mqtt-broker","z":"","name":"Ignition","broker":"192.168.118.60","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"80","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

Flow Info

Created 6 years, 4 months ago
Rating: not yet rated

Owner

Actions

Rate:

Node Types

Core
  • debug (x2)
  • function (x3)
  • json (x2)
  • mqtt in (x2)
  • mqtt-broker (x1)
Other

Tags

  • MQTT
  • Sparkplug
  • IIoT
  • Moxa
  • MGate
  • NPort
  • Ignition
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option