Skip to content

Instantly share code, notes, and snippets.

@alistairhenderson
Created August 1, 2023 09:05
Show Gist options
  • Save alistairhenderson/1d58cd9fd54df2ccbed1eb2a8074652f to your computer and use it in GitHub Desktop.
Save alistairhenderson/1d58cd9fd54df2ccbed1eb2a8074652f to your computer and use it in GitHub Desktop.
Example Node-red Sparkplub MQTT write
[
{
"id": "7b0afdd8dfe283b2",
"type": "tab",
"label": "put-Test-Flow",
"disabled": false,
"info": "",
"env": []
},
{
"id": "bbdd4a1bef6595fd",
"type": "inject",
"z": "7b0afdd8dfe283b2",
"name": "",
"props": [
{
"p": "timestamp",
"v": "",
"vt": "date"
},
{
"p": "topic",
"vt": "str"
},
{
"p": "payloads",
"v": "{\"sensor\":{\"Lt0\":5.39,\"Lt45\":5.32,\"Lt90\":5.32,\"Lt135\":5.41,\"Lt180\":5.41,\"Lt225\":5.42,\"Lt270\":5.36,\"Lt315\":5.34}}",
"vt": "json"
},
{
"p": "payload_1",
"v": "123456789",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "spBv1.0/RSL1/inoex/l1/DDATA",
"x": 270,
"y": 180,
"wires": [
[
"b64be32bacb51cdf",
"c8e95d41a5f895fa"
]
]
},
{
"id": "8340478a0d7e0129",
"type": "mqtt out",
"z": "7b0afdd8dfe283b2",
"name": "Test sending to MQTT",
"topic": "",
"qos": "2",
"retain": "",
"respTopic": "",
"contentType": "",
"userProps": "",
"correl": "",
"expiry": "",
"broker": "f1515b8646ea8926",
"x": 1040,
"y": 60,
"wires": []
},
{
"id": "5f03ae22bbf7357e",
"type": "encode",
"z": "7b0afdd8dfe283b2",
"name": "",
"protofile": "6f0d34e25e3b0a28",
"protoType": "Payload",
"x": 760,
"y": 60,
"wires": [
[
"8340478a0d7e0129"
]
]
},
{
"id": "871df6c8153a6537",
"type": "debug",
"z": "7b0afdd8dfe283b2",
"name": "debug 5",
"active": false,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 1000,
"y": 160,
"wires": []
},
{
"id": "b64be32bacb51cdf",
"type": "function",
"z": "7b0afdd8dfe283b2",
"name": "function 2",
"func": "const originalObject = msg.payloads.sensor;\n\nif (!originalObject) {\n return null; // or handle the error in another suitable way\n}\n\nconst sensorsArray = [];\nObject.keys(originalObject).forEach(key => {\n sensorsArray.push({\n name: key,\n alias: 1, // Adjust according to your setup\n //dataType: \"FLOAT\",\n type: \"FLOAT\",\n //dataType:9,\n value: originalObject[key]\n })\n ;\n});\n\nconst sparkplugBMsg = {\n timestamp: new Date().getTime(), // Current timestamp\n metrics: sensorsArray,\n \"seq\":-1\n // Add other required Sparkplug B fields here\n};\n\nmsg.payload = sparkplugBMsg;\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 500,
"y": 120,
"wires": [
[
"5f03ae22bbf7357e",
"871df6c8153a6537",
"db39a89817ec1fc1"
]
]
},
{
"id": "c8e95d41a5f895fa",
"type": "debug",
"z": "7b0afdd8dfe283b2",
"name": "debug 6",
"active": false,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"statusVal": "",
"statusType": "auto",
"x": 600,
"y": 280,
"wires": []
},
{
"id": "db39a89817ec1fc1",
"type": "mqtt sparkplug out",
"z": "7b0afdd8dfe283b2",
"name": "",
"topic": "spBv1.0/My Devices/DDATA/EoN Name/My Device",
"qos": "",
"retain": "",
"broker": "4dc45388473c78d3",
"x": 1130,
"y": 260,
"wires": []
},
{
"id": "f1515b8646ea8926",
"type": "mqtt-broker",
"name": "acme.net",
"broker": "acme-systems.net",
"port": "1883",
"tls": "",
"clientid": "nodered01",
"autoConnect": true,
"usetls": true,
"protocolVersion": "4",
"keepalive": "60",
"cleansession": false,
"birthTopic": "",
"birthQos": "2",
"birthRetain": "false",
"birthPayload": "",
"birthMsg": {},
"closeTopic": "",
"closeQos": "0",
"closeRetain": "false",
"closePayload": "",
"closeMsg": {},
"willTopic": "",
"willQos": "0",
"willRetain": "false",
"willPayload": "",
"willMsg": {},
"userProps": "",
"sessionExpiry": ""
},
{
"id": "6f0d34e25e3b0a28",
"type": "protobuf-file",
"protopath": "/root/.node-red/sparkplug_b.proto",
"watchFile": true,
"keepCase": false
},
{
"id": "4dc45388473c78d3",
"type": "mqtt-sparkplug-broker",
"name": "acme.net",
"deviceGroup": "test-devices",
"eonName": "node-red",
"broker": "acme.net",
"port": "1883",
"tls": "",
"clientid": "nodered01",
"usetls": true,
"protocolVersion": "4",
"keepalive": "60",
"cleansession": false,
"enableStoreForward": false,
"compressAlgorithm": "",
"aliasMetrics": false,
"primaryScada": ""
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment