Difference between revisions of "Public MQTT Brokers"

From Open Source Controls Wiki
Jump to navigation Jump to search
Line 108: Line 108:
Node-RED version 2 now allows one to manage MQTT subscriptions at run time through software, allowing the processes of encryption and subscribing to be automated.
Node-RED version 2 now allows one to manage MQTT subscriptions at run time through software, allowing the processes of encryption and subscribing to be automated.


 
[[File:Mqttcon1.png]]
<pre>
<pre>
[{"id":"564812e6529f65a5","type":"tab","label":"Flow 2","disabled":false,"info":"","env":[]},{"id":"a33ecb08689a7eb7","type":"mqtt in","z":"564812e6529f65a5","name":"mqtt1","topic":"","qos":"2","datatype":"auto","broker":"209b6731446a7c26","nl":false,"rap":true,"rh":0,"inputs":1,"x":810,"y":220,"wires":[["6c88cfc3c6f35ded"]]},{"id":"bc150d0459b9610b","type":"mqtt out","z":"564812e6529f65a5","name":"","topic":"","qos":"0","retain":"false","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"209b6731446a7c26","x":810,"y":520,"wires":[]},{"id":"97920119052172d3","type":"inject","z":"564812e6529f65a5","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"[{\"broker\":\"broker.emqx.io\",\"port\":8883,\"portTSL\":8883}]","payloadType":"json","x":190,"y":260,"wires":[["55272ae6e147c672"]]},{"id":"c8790f20c7b81ee6","type":"function","z":"564812e6529f65a5","name":"connect","func":"var brokers = flow.get(\"brokers\") || [];\n\nvar msg1 = {};\n\nmsg1.action = \"connect\";\nmsg1.broker = brokers[0];\nmsg1.broker.force=true;\n\nvar msg2 = {};\n\nmsg2.action = \"connect\";\nmsg2.broker = brokers[1];\nmsg2.broker.force=true;\n\n\nreturn [msg1,msg2];","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":600,"y":260,"wires":[["a33ecb08689a7eb7"],["84911e40ee113652"]]},{"id":"55272ae6e147c672","type":"change","z":"564812e6529f65a5","name":"","rules":[{"t":"set","p":"brokers","pt":"flow","to":"[{\"broker\":\"broker.emqx.io\",\"port\":8883,\"portTSL\":8883},{\"broker\":\"mqtt.fluux.io\",\"port\":8883,\"portTSL\":8883}]","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":260,"wires":[["c8790f20c7b81ee6","735e31d28738772f"]]},{"id":"7a4bc93a4ad7fca6","type":"link in","z":"564812e6529f65a5","name":"connect","links":[],"x":225,"y":180,"wires":[["55272ae6e147c672"]]},{"id":"84911e40ee113652","type":"mqtt in","z":"564812e6529f65a5","name":"mqtt2","topic":"","qos":"2","datatype":"auto","broker":"dcd4fcb0c3ba9634","nl":false,"rap":true,"rh":0,"inputs":1,"x":810,"y":300,"wires":[["6c88cfc3c6f35ded"]]},{"id":"37622328e8ce31d5","type":"mqtt out","z":"564812e6529f65a5","name":"","topic":"","qos":"0","retain":"false","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"dcd4fcb0c3ba9634","x":930,"y":580,"wires":[]},{"id":"ae188866b99e9304","type":"function","z":"564812e6529f65a5","name":"","func":"\n\nflow.set(msg.status.source.name + \"status\",\"\"+msg.status.text);\n\nmsg.topic = msg.status.source.name + \"status\";\nmsg.payload = msg.status.text;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":980,"y":400,"wires":[["c95f8190f6df0cc0"]]},{"id":"b46bf8618475f23d","type":"status","z":"564812e6529f65a5","name":"","scope":["a33ecb08689a7eb7","84911e40ee113652"],"x":820,"y":400,"wires":[["ae188866b99e9304"]]},{"id":"c95f8190f6df0cc0","type":"debug","z":"564812e6529f65a5","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1130,"y":400,"wires":[]},{"id":"4bf70e392eca5550","type":"switch","z":"564812e6529f65a5","name":"","property":"mqtt1status","propertyType":"flow","rules":[{"t":"cont","v":".connected","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":630,"y":540,"wires":[["bc150d0459b9610b","02eaf674b24f20df"],["db9c6cba958073d0"]]},{"id":"db9c6cba958073d0","type":"switch","z":"564812e6529f65a5","name":"","property":"mqtt2status","propertyType":"flow","rules":[{"t":"cont","v":".connected","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":750,"y":620,"wires":[["37622328e8ce31d5"],["2876d1fe18c7557a"]]},{"id":"83eec79b9b452b33","type":"link in","z":"564812e6529f65a5","name":"publish","links":["f7be6a97d63cad07"],"x":185,"y":540,"wires":[["1b04abaf18045792"]]},{"id":"2876d1fe18c7557a","type":"function","z":"564812e6529f65a5","name":"not sent","func":"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":940,"y":680,"wires":[[]]},{"id":"6c88cfc3c6f35ded","type":"link out","z":"564812e6529f65a5","name":"incoming","mode":"link","links":["63c5ee9dd5dd59aa"],"x":985,"y":260,"wires":[]},{"id":"63c5ee9dd5dd59aa","type":"link in","z":"564812e6529f65a5","name":"","links":["6c88cfc3c6f35ded"],"x":215,"y":720,"wires":[["37274bfdb40f80c8","c87ed47ca0b33e61"]]},{"id":"37274bfdb40f80c8","type":"debug","z":"564812e6529f65a5","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":390,"y":660,"wires":[]},{"id":"beb8f0fda6a53533","type":"credentials","z":"564812e6529f65a5","name":"","props":[{"value":"publicTopic","type":"msg"}],"x":330,"y":80,"wires":[["69f367686b656cb7"]]},{"id":"bf21787fde563136","type":"inject","z":"564812e6529f65a5","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":170,"y":80,"wires":[["beb8f0fda6a53533"]]},{"id":"69f367686b656cb7","type":"change","z":"564812e6529f65a5","name":"","rules":[{"t":"set","p":"publicTopic","pt":"flow","to":"publicTopic","tot":"msg","dc":true}],"action":"","property":"","from":"","to":"","reg":false,"x":530,"y":80,"wires":[[]]},{"id":"1b04abaf18045792","type":"function","z":"564812e6529f65a5","name":"prefix","func":"\nmsg.payload = JSON.stringify({ \"topic\": msg.topic, \"payload\": msg.payload });\n\nmsg.topic = flow.get(\"publicTopic\") + \"/\" + msg.topic.split(\"/\")[3];\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":310,"y":540,"wires":[["e388a8917e1a8a1f"]]},{"id":"e388a8917e1a8a1f","type":"encrypt","z":"564812e6529f65a5","name":"","algorithm":"AES","key":"aaabbbccc","x":460,"y":540,"wires":[["4bf70e392eca5550"]]},{"id":"735e31d28738772f","type":"delay","z":"564812e6529f65a5","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":420,"y":320,"wires":[["da72769bf2270a2e","7a497090087e6f05"]]},{"id":"da72769bf2270a2e","type":"function","z":"564812e6529f65a5","d":true,"name":"subscribe","func":"\nvar publicTopic  = flow.get(\"publicTopic\") || \"unknown\";\n\n\n\nvar msg1 = {};\n\nmsg1.action = \"subscribe\";\nmsg1.topic = [{\"topic\":publicTopic + \"/cmd\", \"qos\":0},{\"topic\":publicTopic + \"/set\", \"qos\":0}];\n\nvar msg2 = {};\n\nmsg2.action = \"subscribe\";\nmsg2.topic = [{\"topic\":publicTopic + \"/cmd\", \"qos\":0},{\"topic\":publicTopic + \"/set\", \"qos\":0}];\n\nif (flow.get(\"mqtt1status\").indexOf(\".connected\")<0) { msg1 = null; }\nif (flow.get(\"mqtt2status\").indexOf(\".connected\")<0) { msg2 = null; }\n\nreturn [msg1,msg2];","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":600,"y":320,"wires":[["a33ecb08689a7eb7"],["84911e40ee113652"]]},{"id":"02eaf674b24f20df","type":"debug","z":"564812e6529f65a5","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":830,"y":480,"wires":[]},{"id":"c8383d21cfd1c1bb","type":"debug","z":"564812e6529f65a5","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":690,"y":720,"wires":[]},{"id":"7a497090087e6f05","type":"function","z":"564812e6529f65a5","d":true,"name":"subscribe","func":"\nvar publicTopic  = flow.get(\"publicTopic\") || \"unknown\";\n\n\n\nvar msg1 = {};\n\nmsg1.action = \"subscribe\";\nmsg1.topic = [{\"topic\":publicTopic + \"/#\", \"qos\":0}];\n\nvar msg2 = {};\n\nmsg2.action = \"subscribe\";\nmsg2.topic = [{\"topic\":publicTopic + \"/#\", \"qos\":0}];\n\nif (flow.get(\"mqtt1status\").indexOf(\".connected\")<0) { msg1 = null; }\nif (flow.get(\"mqtt2status\").indexOf(\".connected\")<0) { msg2 = null; }\n\nreturn [msg1,msg2];","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":600,"y":380,"wires":[["a33ecb08689a7eb7"],["84911e40ee113652"]]},{"id":"c87ed47ca0b33e61","type":"decrypt","z":"564812e6529f65a5","name":"","algorithm":"AES","key":"aaabbbccc","x":360,"y":720,"wires":[["cbf3d192113bc4ac"]]},{"id":"cbf3d192113bc4ac","type":"function","z":"564812e6529f65a5","name":"prefix","func":"\nvar data = JSON.parse( msg.payload );\n\nmsg.payload = data.payload;\nmsg.topic = data.topic;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":510,"y":720,"wires":[["c8383d21cfd1c1bb"]]},{"id":"209b6731446a7c26","type":"mqtt-broker","name":"mqtt1","broker":"none","port":"1883","tls":"","clientid":"","autoConnect":false,"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":"dcd4fcb0c3ba9634","type":"mqtt-broker","name":"mqtt2","broker":"none","port":"1883","tls":"","clientid":"","autoConnect":false,"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":"564812e6529f65a5","type":"tab","label":"Flow 2","disabled":false,"info":"","env":[]},{"id":"a33ecb08689a7eb7","type":"mqtt in","z":"564812e6529f65a5","name":"mqtt1","topic":"","qos":"2","datatype":"auto","broker":"209b6731446a7c26","nl":false,"rap":true,"rh":0,"inputs":1,"x":810,"y":220,"wires":[["6c88cfc3c6f35ded"]]},{"id":"bc150d0459b9610b","type":"mqtt out","z":"564812e6529f65a5","name":"","topic":"","qos":"0","retain":"false","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"209b6731446a7c26","x":810,"y":520,"wires":[]},{"id":"97920119052172d3","type":"inject","z":"564812e6529f65a5","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"[{\"broker\":\"broker.emqx.io\",\"port\":8883,\"portTSL\":8883}]","payloadType":"json","x":190,"y":260,"wires":[["55272ae6e147c672"]]},{"id":"c8790f20c7b81ee6","type":"function","z":"564812e6529f65a5","name":"connect","func":"var brokers = flow.get(\"brokers\") || [];\n\nvar msg1 = {};\n\nmsg1.action = \"connect\";\nmsg1.broker = brokers[0];\nmsg1.broker.force=true;\n\nvar msg2 = {};\n\nmsg2.action = \"connect\";\nmsg2.broker = brokers[1];\nmsg2.broker.force=true;\n\n\nreturn [msg1,msg2];","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":600,"y":260,"wires":[["a33ecb08689a7eb7"],["84911e40ee113652"]]},{"id":"55272ae6e147c672","type":"change","z":"564812e6529f65a5","name":"","rules":[{"t":"set","p":"brokers","pt":"flow","to":"[{\"broker\":\"broker.emqx.io\",\"port\":8883,\"portTSL\":8883},{\"broker\":\"mqtt.fluux.io\",\"port\":8883,\"portTSL\":8883}]","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":260,"wires":[["c8790f20c7b81ee6","735e31d28738772f"]]},{"id":"7a4bc93a4ad7fca6","type":"link in","z":"564812e6529f65a5","name":"connect","links":[],"x":225,"y":180,"wires":[["55272ae6e147c672"]]},{"id":"84911e40ee113652","type":"mqtt in","z":"564812e6529f65a5","name":"mqtt2","topic":"","qos":"2","datatype":"auto","broker":"dcd4fcb0c3ba9634","nl":false,"rap":true,"rh":0,"inputs":1,"x":810,"y":300,"wires":[["6c88cfc3c6f35ded"]]},{"id":"37622328e8ce31d5","type":"mqtt out","z":"564812e6529f65a5","name":"","topic":"","qos":"0","retain":"false","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"dcd4fcb0c3ba9634","x":930,"y":580,"wires":[]},{"id":"ae188866b99e9304","type":"function","z":"564812e6529f65a5","name":"","func":"\n\nflow.set(msg.status.source.name + \"status\",\"\"+msg.status.text);\n\nmsg.topic = msg.status.source.name + \"status\";\nmsg.payload = msg.status.text;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":980,"y":400,"wires":[["c95f8190f6df0cc0"]]},{"id":"b46bf8618475f23d","type":"status","z":"564812e6529f65a5","name":"","scope":["a33ecb08689a7eb7","84911e40ee113652"],"x":820,"y":400,"wires":[["ae188866b99e9304"]]},{"id":"c95f8190f6df0cc0","type":"debug","z":"564812e6529f65a5","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1130,"y":400,"wires":[]},{"id":"4bf70e392eca5550","type":"switch","z":"564812e6529f65a5","name":"","property":"mqtt1status","propertyType":"flow","rules":[{"t":"cont","v":".connected","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":630,"y":540,"wires":[["bc150d0459b9610b","02eaf674b24f20df"],["db9c6cba958073d0"]]},{"id":"db9c6cba958073d0","type":"switch","z":"564812e6529f65a5","name":"","property":"mqtt2status","propertyType":"flow","rules":[{"t":"cont","v":".connected","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":750,"y":620,"wires":[["37622328e8ce31d5"],["2876d1fe18c7557a"]]},{"id":"83eec79b9b452b33","type":"link in","z":"564812e6529f65a5","name":"publish","links":["f7be6a97d63cad07"],"x":185,"y":540,"wires":[["1b04abaf18045792"]]},{"id":"2876d1fe18c7557a","type":"function","z":"564812e6529f65a5","name":"not sent","func":"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":940,"y":680,"wires":[[]]},{"id":"6c88cfc3c6f35ded","type":"link out","z":"564812e6529f65a5","name":"incoming","mode":"link","links":["63c5ee9dd5dd59aa"],"x":985,"y":260,"wires":[]},{"id":"63c5ee9dd5dd59aa","type":"link in","z":"564812e6529f65a5","name":"","links":["6c88cfc3c6f35ded"],"x":215,"y":720,"wires":[["37274bfdb40f80c8","c87ed47ca0b33e61"]]},{"id":"37274bfdb40f80c8","type":"debug","z":"564812e6529f65a5","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":390,"y":660,"wires":[]},{"id":"beb8f0fda6a53533","type":"credentials","z":"564812e6529f65a5","name":"","props":[{"value":"publicTopic","type":"msg"}],"x":330,"y":80,"wires":[["69f367686b656cb7"]]},{"id":"bf21787fde563136","type":"inject","z":"564812e6529f65a5","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":170,"y":80,"wires":[["beb8f0fda6a53533"]]},{"id":"69f367686b656cb7","type":"change","z":"564812e6529f65a5","name":"","rules":[{"t":"set","p":"publicTopic","pt":"flow","to":"publicTopic","tot":"msg","dc":true}],"action":"","property":"","from":"","to":"","reg":false,"x":530,"y":80,"wires":[[]]},{"id":"1b04abaf18045792","type":"function","z":"564812e6529f65a5","name":"prefix","func":"\nmsg.payload = JSON.stringify({ \"topic\": msg.topic, \"payload\": msg.payload });\n\nmsg.topic = flow.get(\"publicTopic\") + \"/\" + msg.topic.split(\"/\")[3];\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":310,"y":540,"wires":[["e388a8917e1a8a1f"]]},{"id":"e388a8917e1a8a1f","type":"encrypt","z":"564812e6529f65a5","name":"","algorithm":"AES","key":"aaabbbccc","x":460,"y":540,"wires":[["4bf70e392eca5550"]]},{"id":"735e31d28738772f","type":"delay","z":"564812e6529f65a5","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":420,"y":320,"wires":[["da72769bf2270a2e","7a497090087e6f05"]]},{"id":"da72769bf2270a2e","type":"function","z":"564812e6529f65a5","d":true,"name":"subscribe","func":"\nvar publicTopic  = flow.get(\"publicTopic\") || \"unknown\";\n\n\n\nvar msg1 = {};\n\nmsg1.action = \"subscribe\";\nmsg1.topic = [{\"topic\":publicTopic + \"/cmd\", \"qos\":0},{\"topic\":publicTopic + \"/set\", \"qos\":0}];\n\nvar msg2 = {};\n\nmsg2.action = \"subscribe\";\nmsg2.topic = [{\"topic\":publicTopic + \"/cmd\", \"qos\":0},{\"topic\":publicTopic + \"/set\", \"qos\":0}];\n\nif (flow.get(\"mqtt1status\").indexOf(\".connected\")<0) { msg1 = null; }\nif (flow.get(\"mqtt2status\").indexOf(\".connected\")<0) { msg2 = null; }\n\nreturn [msg1,msg2];","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":600,"y":320,"wires":[["a33ecb08689a7eb7"],["84911e40ee113652"]]},{"id":"02eaf674b24f20df","type":"debug","z":"564812e6529f65a5","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":830,"y":480,"wires":[]},{"id":"c8383d21cfd1c1bb","type":"debug","z":"564812e6529f65a5","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":690,"y":720,"wires":[]},{"id":"7a497090087e6f05","type":"function","z":"564812e6529f65a5","d":true,"name":"subscribe","func":"\nvar publicTopic  = flow.get(\"publicTopic\") || \"unknown\";\n\n\n\nvar msg1 = {};\n\nmsg1.action = \"subscribe\";\nmsg1.topic = [{\"topic\":publicTopic + \"/#\", \"qos\":0}];\n\nvar msg2 = {};\n\nmsg2.action = \"subscribe\";\nmsg2.topic = [{\"topic\":publicTopic + \"/#\", \"qos\":0}];\n\nif (flow.get(\"mqtt1status\").indexOf(\".connected\")<0) { msg1 = null; }\nif (flow.get(\"mqtt2status\").indexOf(\".connected\")<0) { msg2 = null; }\n\nreturn [msg1,msg2];","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":600,"y":380,"wires":[["a33ecb08689a7eb7"],["84911e40ee113652"]]},{"id":"c87ed47ca0b33e61","type":"decrypt","z":"564812e6529f65a5","name":"","algorithm":"AES","key":"aaabbbccc","x":360,"y":720,"wires":[["cbf3d192113bc4ac"]]},{"id":"cbf3d192113bc4ac","type":"function","z":"564812e6529f65a5","name":"prefix","func":"\nvar data = JSON.parse( msg.payload );\n\nmsg.payload = data.payload;\nmsg.topic = data.topic;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":510,"y":720,"wires":[["c8383d21cfd1c1bb"]]},{"id":"209b6731446a7c26","type":"mqtt-broker","name":"mqtt1","broker":"none","port":"1883","tls":"","clientid":"","autoConnect":false,"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":"dcd4fcb0c3ba9634","type":"mqtt-broker","name":"mqtt2","broker":"none","port":"1883","tls":"","clientid":"","autoConnect":false,"usetls":true,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]

Revision as of 19:30, 2 June 2022

Cloud Based Implementation of IoT Using MQTT Brokers

This is a useful article that sums up the various MQTT options.

https://www.opensourceforu.com/2020/10/cloud-based-implementation-of-iot-using-mqtt-brokers/

When using public brokers, it is advised to consider encrypting traffic as standard. Both topics and payloads can be encrypted, with Node-RED providing nodes for such operations. Only devices that have the encryption key can find (subscribe) and make sense of the data.

The interesting question to ask is if public brokers are as good as a dedicated private broker.

Using public brokers has the following advantages:

  • A central list can be maintained (the table below can be loaded into software from this site)
  • With three or more brokers reliably available, one can establish primary and secondary routes, adding resilience and making for increased reliability
  • No maintenance, and no costs. A completely free approach to communications.
  • Data can be encrypted.
  • It is possible to share data by providing someone with encryption keys that lets them subscribe to data.

Public MQTT Brokers

Name Broker Address TCP Port TLS Port WebSocket Port Message Retention Persistent Session Sign Up Requred Link
Eclipse mqtt.eclipse.org 1883 N/A 80, 443 YES YES NO http://iot.eclipse.org/sandbox.html
Mosquitto test.mosquitto.org 1883 8883, 8884 80 YES YES NO https://test.mosquitto.org/
HiveMQ broker.hivemq.com 1883 N/A 8000 YES YES NO https://www.hivemq.com/try-out/
Flespi mqtt.flespi.io 1883 8883 80, 443 YES YES YES https://flespi.com/mqtt-broker
Dioty mqtt.dioty.co 1883 8883 8080, 8880 YES YES YES http://www.dioty.co/
Fluux mqtt.fluux.io 1883 8883 N/A N/A N/A NO https://fluux.io/
EMQX broker.emqx.io 1883 8883 8083 YES YES NO https://emqx.io/

EMQX Public MQTT 5 Broker


Node-RED Automation

Node-RED version 2 now allows one to manage MQTT subscriptions at run time through software, allowing the processes of encryption and subscribing to be automated.

Mqttcon1.png

[{"id":"564812e6529f65a5","type":"tab","label":"Flow 2","disabled":false,"info":"","env":[]},{"id":"a33ecb08689a7eb7","type":"mqtt in","z":"564812e6529f65a5","name":"mqtt1","topic":"","qos":"2","datatype":"auto","broker":"209b6731446a7c26","nl":false,"rap":true,"rh":0,"inputs":1,"x":810,"y":220,"wires":[["6c88cfc3c6f35ded"]]},{"id":"bc150d0459b9610b","type":"mqtt out","z":"564812e6529f65a5","name":"","topic":"","qos":"0","retain":"false","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"209b6731446a7c26","x":810,"y":520,"wires":[]},{"id":"97920119052172d3","type":"inject","z":"564812e6529f65a5","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"[{\"broker\":\"broker.emqx.io\",\"port\":8883,\"portTSL\":8883}]","payloadType":"json","x":190,"y":260,"wires":[["55272ae6e147c672"]]},{"id":"c8790f20c7b81ee6","type":"function","z":"564812e6529f65a5","name":"connect","func":"var brokers = flow.get(\"brokers\") || [];\n\nvar msg1 = {};\n\nmsg1.action = \"connect\";\nmsg1.broker = brokers[0];\nmsg1.broker.force=true;\n\nvar msg2 = {};\n\nmsg2.action = \"connect\";\nmsg2.broker = brokers[1];\nmsg2.broker.force=true;\n\n\nreturn [msg1,msg2];","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":600,"y":260,"wires":[["a33ecb08689a7eb7"],["84911e40ee113652"]]},{"id":"55272ae6e147c672","type":"change","z":"564812e6529f65a5","name":"","rules":[{"t":"set","p":"brokers","pt":"flow","to":"[{\"broker\":\"broker.emqx.io\",\"port\":8883,\"portTSL\":8883},{\"broker\":\"mqtt.fluux.io\",\"port\":8883,\"portTSL\":8883}]","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":260,"wires":[["c8790f20c7b81ee6","735e31d28738772f"]]},{"id":"7a4bc93a4ad7fca6","type":"link in","z":"564812e6529f65a5","name":"connect","links":[],"x":225,"y":180,"wires":[["55272ae6e147c672"]]},{"id":"84911e40ee113652","type":"mqtt in","z":"564812e6529f65a5","name":"mqtt2","topic":"","qos":"2","datatype":"auto","broker":"dcd4fcb0c3ba9634","nl":false,"rap":true,"rh":0,"inputs":1,"x":810,"y":300,"wires":[["6c88cfc3c6f35ded"]]},{"id":"37622328e8ce31d5","type":"mqtt out","z":"564812e6529f65a5","name":"","topic":"","qos":"0","retain":"false","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"dcd4fcb0c3ba9634","x":930,"y":580,"wires":[]},{"id":"ae188866b99e9304","type":"function","z":"564812e6529f65a5","name":"","func":"\n\nflow.set(msg.status.source.name + \"status\",\"\"+msg.status.text);\n\nmsg.topic = msg.status.source.name + \"status\";\nmsg.payload = msg.status.text;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":980,"y":400,"wires":[["c95f8190f6df0cc0"]]},{"id":"b46bf8618475f23d","type":"status","z":"564812e6529f65a5","name":"","scope":["a33ecb08689a7eb7","84911e40ee113652"],"x":820,"y":400,"wires":[["ae188866b99e9304"]]},{"id":"c95f8190f6df0cc0","type":"debug","z":"564812e6529f65a5","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1130,"y":400,"wires":[]},{"id":"4bf70e392eca5550","type":"switch","z":"564812e6529f65a5","name":"","property":"mqtt1status","propertyType":"flow","rules":[{"t":"cont","v":".connected","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":630,"y":540,"wires":[["bc150d0459b9610b","02eaf674b24f20df"],["db9c6cba958073d0"]]},{"id":"db9c6cba958073d0","type":"switch","z":"564812e6529f65a5","name":"","property":"mqtt2status","propertyType":"flow","rules":[{"t":"cont","v":".connected","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":750,"y":620,"wires":[["37622328e8ce31d5"],["2876d1fe18c7557a"]]},{"id":"83eec79b9b452b33","type":"link in","z":"564812e6529f65a5","name":"publish","links":["f7be6a97d63cad07"],"x":185,"y":540,"wires":[["1b04abaf18045792"]]},{"id":"2876d1fe18c7557a","type":"function","z":"564812e6529f65a5","name":"not sent","func":"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":940,"y":680,"wires":[[]]},{"id":"6c88cfc3c6f35ded","type":"link out","z":"564812e6529f65a5","name":"incoming","mode":"link","links":["63c5ee9dd5dd59aa"],"x":985,"y":260,"wires":[]},{"id":"63c5ee9dd5dd59aa","type":"link in","z":"564812e6529f65a5","name":"","links":["6c88cfc3c6f35ded"],"x":215,"y":720,"wires":[["37274bfdb40f80c8","c87ed47ca0b33e61"]]},{"id":"37274bfdb40f80c8","type":"debug","z":"564812e6529f65a5","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":390,"y":660,"wires":[]},{"id":"beb8f0fda6a53533","type":"credentials","z":"564812e6529f65a5","name":"","props":[{"value":"publicTopic","type":"msg"}],"x":330,"y":80,"wires":[["69f367686b656cb7"]]},{"id":"bf21787fde563136","type":"inject","z":"564812e6529f65a5","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":170,"y":80,"wires":[["beb8f0fda6a53533"]]},{"id":"69f367686b656cb7","type":"change","z":"564812e6529f65a5","name":"","rules":[{"t":"set","p":"publicTopic","pt":"flow","to":"publicTopic","tot":"msg","dc":true}],"action":"","property":"","from":"","to":"","reg":false,"x":530,"y":80,"wires":[[]]},{"id":"1b04abaf18045792","type":"function","z":"564812e6529f65a5","name":"prefix","func":"\nmsg.payload = JSON.stringify({ \"topic\": msg.topic, \"payload\": msg.payload });\n\nmsg.topic = flow.get(\"publicTopic\") + \"/\" + msg.topic.split(\"/\")[3];\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":310,"y":540,"wires":[["e388a8917e1a8a1f"]]},{"id":"e388a8917e1a8a1f","type":"encrypt","z":"564812e6529f65a5","name":"","algorithm":"AES","key":"aaabbbccc","x":460,"y":540,"wires":[["4bf70e392eca5550"]]},{"id":"735e31d28738772f","type":"delay","z":"564812e6529f65a5","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":420,"y":320,"wires":[["da72769bf2270a2e","7a497090087e6f05"]]},{"id":"da72769bf2270a2e","type":"function","z":"564812e6529f65a5","d":true,"name":"subscribe","func":"\nvar publicTopic  = flow.get(\"publicTopic\") || \"unknown\";\n\n\n\nvar msg1 = {};\n\nmsg1.action = \"subscribe\";\nmsg1.topic = [{\"topic\":publicTopic + \"/cmd\", \"qos\":0},{\"topic\":publicTopic + \"/set\", \"qos\":0}];\n\nvar msg2 = {};\n\nmsg2.action = \"subscribe\";\nmsg2.topic = [{\"topic\":publicTopic + \"/cmd\", \"qos\":0},{\"topic\":publicTopic + \"/set\", \"qos\":0}];\n\nif (flow.get(\"mqtt1status\").indexOf(\".connected\")<0) { msg1 = null; }\nif (flow.get(\"mqtt2status\").indexOf(\".connected\")<0) { msg2 = null; }\n\nreturn [msg1,msg2];","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":600,"y":320,"wires":[["a33ecb08689a7eb7"],["84911e40ee113652"]]},{"id":"02eaf674b24f20df","type":"debug","z":"564812e6529f65a5","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":830,"y":480,"wires":[]},{"id":"c8383d21cfd1c1bb","type":"debug","z":"564812e6529f65a5","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":690,"y":720,"wires":[]},{"id":"7a497090087e6f05","type":"function","z":"564812e6529f65a5","d":true,"name":"subscribe","func":"\nvar publicTopic  = flow.get(\"publicTopic\") || \"unknown\";\n\n\n\nvar msg1 = {};\n\nmsg1.action = \"subscribe\";\nmsg1.topic = [{\"topic\":publicTopic + \"/#\", \"qos\":0}];\n\nvar msg2 = {};\n\nmsg2.action = \"subscribe\";\nmsg2.topic = [{\"topic\":publicTopic + \"/#\", \"qos\":0}];\n\nif (flow.get(\"mqtt1status\").indexOf(\".connected\")<0) { msg1 = null; }\nif (flow.get(\"mqtt2status\").indexOf(\".connected\")<0) { msg2 = null; }\n\nreturn [msg1,msg2];","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":600,"y":380,"wires":[["a33ecb08689a7eb7"],["84911e40ee113652"]]},{"id":"c87ed47ca0b33e61","type":"decrypt","z":"564812e6529f65a5","name":"","algorithm":"AES","key":"aaabbbccc","x":360,"y":720,"wires":[["cbf3d192113bc4ac"]]},{"id":"cbf3d192113bc4ac","type":"function","z":"564812e6529f65a5","name":"prefix","func":"\nvar data = JSON.parse( msg.payload );\n\nmsg.payload = data.payload;\nmsg.topic = data.topic;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":510,"y":720,"wires":[["c8383d21cfd1c1bb"]]},{"id":"209b6731446a7c26","type":"mqtt-broker","name":"mqtt1","broker":"none","port":"1883","tls":"","clientid":"","autoConnect":false,"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":"dcd4fcb0c3ba9634","type":"mqtt-broker","name":"mqtt2","broker":"none","port":"1883","tls":"","clientid":"","autoConnect":false,"usetls":true,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]