Send data to InUse through MQTT

Send data to InUse through MQTT

This flow explains how to send data to InUse Public MQTT broker using the InUse Standard JSON format.

In order to make it work make sure to use the correct credentials as explained in InUse documentation

This example flow works as following:

  • Simulate the data in the node Simulate data that should be replaced with your data.
  • Queue the messages in queue before sending them to InUse MQTT broker through mqtt node. The queue node is useful to handle network disconnections or when there are a lot of messages that should be sent.

Other good practices

  • Avoid sending data that does not change in time. You could use the node-rbe to send data using a deadband.
  • You could also use a minimum logging interval to force a message to be send even if there is no change (every minute for example)
[{"id":"29a7ee4ebc55555e","type":"tab","label":"Send data to InUse","disabled":false,"info":"# Send data to InUse through MQTT \n\nThis flow explains how to send data to InUse Public MQTT broker using the [InUse Standard JSON format](https://inuse.gitbook.io/docs/features/connect/mqtt/mqtt-connector#content).\n\nIn order to make it work make sure to use the correct credentials as explained in [InUse documentation](https://inuse.gitbook.io/docs/features/connect/mqtt/connect-to-inuse-mqtt-broker)\n\nThis example flow works as following:\n- Simulate the fetching of data in the node `fetch data` that should be replaced with your data. \n- Queue the messages in `queue` before sending them to InUse MQTT broker through `mqtt` node. The `queue` node is useful to handle network disconnections or when there are a lot of messages that should be sent.\n\n## Other good practices\n- Avoid sending data that does not change in time. You could use the [node-rbe](https://flows.nodered.org/node/node-red-node-rbe) to send data using a deadband. \n- You could also use a minimum logging interval to force a message to be send even if there is no change (every minute for example)\n\n# RBE node\n\nThe rbe node enable to send only payload with a value change.\nbe carefull if there is a timestamp in the payload RBE will detect a change of the time and send the message\nIf only one value has changed all the payload is send. with an inuse standard payload\n\n# Big payload\nIn case of large payloads, it is necesary to filter the payload further to only \nsend a payload with data where values have changed.\nthe function: `Filter Payload_Changed_Value` is doing this function\n\nThe RBE filter is still necesary to avoid sending an empty payload.","env":[]},{"id":"4a30d089b3fda1d1","type":"debug","z":"29a7ee4ebc55555e","name":"debug 6","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1000,"y":80,"wires":[]},{"id":"c284a3756d253efc","type":"function","z":"29a7ee4ebc55555e","name":"Filter Payload_Changed_Value","func":"//Filter the payload message to keep only keys with a changed value.\nvar data;\nvar backupvalue = context.get(\"S_value\");\n\nif (backupvalue != undefined){\n    // store values that changed\n    data = {};\n    Object.keys(msg.payload)\n        .forEach(key => {\n            if (backupvalue[key] != msg.payload[key])\n                data[key] = msg.payload[key];\n        });\n}\nelse {\n    //at startup, there is no backupvalue. we store all the data\n    data = msg.payload;\n}\n\n//store the values for next messages to compare\ncontext.set(\"S_value\", msg.payload);\n\n//format to inuse mqtt standard format and send it\nif (Object.keys(data).length > 0){\n    msg.payload = {\n        ts: new Date(msg.ts).toISOString(),\n        data\n    }\n    return msg;\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":630,"y":140,"wires":[["4a30d089b3fda1d1","0c45d0fe9eb94ad9"]],"info":"This node is doing a filter on the payload.\r\nif a key didn't change since the last time the key is removed fom\r\nthe payload\r\n\r\nonly the fist message after starting the node red is full with all the key\r\n\r\nthis node also format the payload to the Inuse Standart for sending \r\ndata to inuse via MQTT.\r\n\r\n{\r\n    data:{\r\n    var1:42,\r\n    data2: 12\r\n    },\r\nts:\"2022-YY-DDTHH:mm:ss.mmmZ\"\r\n}"},{"id":"57f1ab60fdbd34a2","type":"rbe","z":"29a7ee4ebc55555e","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":true,"property":"payload","topi":"topic","x":410,"y":140,"wires":[["c284a3756d253efc"]]},{"id":"11416aea16e5603b","type":"function","z":"29a7ee4ebc55555e","name":"fetch data","func":"// Here we should fetch the data from the data source (PLC, sensors, etc.)\n// The following example simulates random data\nmsg.payload = {\n        speed: Math.random()*100,// simulate a random speed\n        default:Math.random() * 100,// simulate a random default\n        unit_in:10000\n    }\nreturn msg;\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":260,"y":140,"wires":[["57f1ab60fdbd34a2"]],"info":" this function is an example to create the payload without timestanmp\r\n so the message can be filtered whith the node RBE."},{"id":"5b151a4725240981","type":"inject","z":"29a7ee4ebc55555e","name":"generate message","props":[{"p":"ts","v":"","vt":"date"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"my_producer_code/my_site_code/my_machine_code","x":130,"y":60,"wires":[["11416aea16e5603b"]]},{"id":"42e957e8dc7bc5bb","type":"mqtt out","z":"29a7ee4ebc55555e","name":"","topic":"","qos":"0","retain":"false","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"c6a7840a.d9f748","x":1130,"y":140,"wires":[]},{"id":"0c45d0fe9eb94ad9","type":"queue","z":"29a7ee4ebc55555e","name":"queue","connected":"^connected","connectedType":"re","disconnected":"","disconnectedType":"re","sqlite":"db_mqtt.sqlite","x":970,"y":140,"wires":[["42e957e8dc7bc5bb"]]},{"id":"c6a7840a.d9f748","type":"mqtt-broker","name":"InUse MQTT Broker","broker":"mqtts://my_company.mqtt.productinuse.com","port":"8883","tls":"273daffe.81304","clientid":"my_company_node_red_1","autoConnect":true,"usetls":true,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""},{"id":"273daffe.81304","type":"tls-config","name":"","cert":"","key":"","ca":"","certname":"","keyname":"","caname":"inuse.cer","servername":"","verifyservercert":false,"alpnprotocol":""}]

Collection Info

Flow Info

Created 3 years ago
Updated 2 years, 10 months ago
Rating: not yet rated

Owner

Actions

Rate:

Node Types

Core
  • debug (x1)
  • function (x2)
  • inject (x1)
  • mqtt out (x1)
  • mqtt-broker (x1)
  • rbe (x1)
  • tls-config (x1)
Other
  • queue (x1)
  • tab (x1)

Tags

  • inuse
  • mqtt
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option