Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
fab-lab.eu
Published

Easy Start into the World of IoT with MQTT

MQTT is a messaging protocol for the IoT. In this example, we show how quickly you can get up and running without a single line of code!

IntermediateFull instructions provided8,519
Easy Start into the World of IoT with MQTT

Things used in this project

Hardware components

#IoT Octopus
optional - can by and ESP8266 based board
×1
Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
optional - to run the node-RED
×1

Software apps and online services

Node-RED
Node-RED
IoT Werkstatt - Arduino Portable 1.8.5 including Ardublock
required as it build based on "superblocks" the required MQTT code for you - use blocks like in scratch(TM)

Story

Read more

Code

Ardublock

XML
Download the XML and load it in Ardublock
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<cb:CODEBLOCKS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://education.mit.edu/openblocks/ns http://education.mit.edu/openblocks/codeblocks.xsd" xmlns:cb="http://education.mit.edu/openblocks/ns">
<Pages collapsible-pages="yes" drawer-with-page="yes">
<Page page-color="128 128 128" page-drawer="Main" page-infullview="yes" page-name="Main" page-width="1280">
<PageBlocks>
<Block genus-name="mqttpublish" id="231">
<Label>mqtt publish</Label>
<Location>
<X>264</X>
<Y>492</Y>
</Location>
<BeforeBlockId>124</BeforeBlockId>
<AfterBlockId>236</AfterBlockId>
<Sockets num-sockets="2">
<BlockConnector con-block-id="232" connector-kind="socket" connector-type="string" init-type="string" label="Topic" position-type="single"/>
<BlockConnector con-block-id="233" connector-kind="socket" connector-type="string" init-type="string" label="Text" position-type="single"/>
</Sockets>
</Block>
<Block genus-name="delay" id="236">
<Label>Warte</Label>
<Location>
<X>264</X>
<Y>563</Y>
</Location>
<BeforeBlockId>231</BeforeBlockId>
<Sockets num-sockets="1">
<BlockConnector con-block-id="237" connector-kind="socket" connector-type="number" init-type="number" label="Millisekunden" position-type="single"/>
</Sockets>
</Block>
<Block genus-name="number" id="237">
<Label>10000</Label>
<Location>
<X>408</X>
<Y>566</Y>
</Location>
<Plug>
<BlockConnector con-block-id="236" connector-kind="plug" connector-type="number" init-type="number" label="" position-type="mirror"/>
</Plug>
</Block>
<Block genus-name="glue_sn" id="233">
<Label>verbinde</Label>
<Location>
<X>408</X>
<Y>519</Y>
</Location>
<Plug>
<BlockConnector con-block-id="231" connector-kind="plug" connector-type="string" init-type="string" label="" position-type="single"/>
</Plug>
<Sockets num-sockets="1">
<BlockConnector con-block-id="234" connector-kind="socket" connector-type="number" init-type="number" label="" position-type="single"/>
</Sockets>
</Block>
<Block genus-name="GetBME" id="234">
<Location>
<X>492</X>
<Y>519</Y>
</Location>
<Plug>
<BlockConnector con-block-id="233" connector-kind="plug" connector-type="number" init-type="number" label="" position-type="single"/>
</Plug>
<Sockets num-sockets="1">
<BlockConnector con-block-id="235" connector-kind="socket" connector-type="number" init-type="number" label="Sensor" position-type="single"/>
</Sockets>
</Block>
<Block genus-name="BMEVectorPress" id="235">
<Location>
<X>718</X>
<Y>519</Y>
</Location>
<Plug>
<BlockConnector con-block-id="234" connector-kind="plug" connector-type="number" init-type="number" label="" position-type="single"/>
</Plug>
</Block>
<Block genus-name="mqtttopic" id="232">
<Label>/luftdruck</Label>
<Location>
<X>408</X>
<Y>495</Y>
</Location>
<Plug>
<BlockConnector con-block-id="231" connector-kind="plug" connector-type="string" init-type="string" label="" position-type="single"/>
</Plug>
</Block>
<Block genus-name="glue_sn" id="127">
<Label>verbinde</Label>
<Location>
<X>408</X>
<Y>377</Y>
</Location>
<Plug>
<BlockConnector con-block-id="118" connector-kind="plug" connector-type="string" init-type="string" label="" position-type="single"/>
</Plug>
<Sockets num-sockets="1">
<BlockConnector con-block-id="128" connector-kind="socket" connector-type="number" init-type="number" label="" position-type="single"/>
</Sockets>
</Block>
<Block genus-name="GetBME" id="128">
<Location>
<X>492</X>
<Y>377</Y>
</Location>
<Plug>
<BlockConnector con-block-id="127" connector-kind="plug" connector-type="number" init-type="number" label="" position-type="single"/>
</Plug>
<Sockets num-sockets="1">
<BlockConnector con-block-id="129" connector-kind="socket" connector-type="number" init-type="number" label="Sensor" position-type="single"/>
</Sockets>
</Block>
<Block genus-name="BMEVectorTemp" id="129">
<Label>Temperatur</Label>
<Location>
<X>718</X>
<Y>377</Y>
</Location>
<Plug>
<BlockConnector con-block-id="128" connector-kind="plug" connector-type="number" init-type="number" label="" position-type="single"/>
</Plug>
</Block>
<Block genus-name="glue_sn" id="121">
<Location>
<X>408</X>
<Y>448</Y>
</Location>
<Plug>
<BlockConnector con-block-id="124" connector-kind="plug" connector-type="string" init-type="string" label="" position-type="single"/>
</Plug>
<Sockets num-sockets="1">
<BlockConnector con-block-id="122" connector-kind="socket" connector-type="number" init-type="number" label="" position-type="single"/>
</Sockets>
</Block>
<Block genus-name="GetBME" id="122">
<Location>
<X>492</X>
<Y>448</Y>
</Location>
<Plug>
<BlockConnector con-block-id="121" connector-kind="plug" connector-type="number" init-type="number" label="" position-type="single"/>
</Plug>
<Sockets num-sockets="1">
<BlockConnector con-block-id="123" connector-kind="socket" connector-type="number" init-type="number" label="Sensor" position-type="single"/>
</Sockets>
</Block>
<Block genus-name="BMEVectorHum" id="123">
<Location>
<X>718</X>
<Y>448</Y>
</Location>
<Plug>
<BlockConnector con-block-id="122" connector-kind="plug" connector-type="number" init-type="number" label="" position-type="single"/>
</Plug>
</Block>
<Block genus-name="mqttpublish" id="124">
<Location>
<X>264</X>
<Y>421</Y>
</Location>
<BeforeBlockId>118</BeforeBlockId>
<AfterBlockId>231</AfterBlockId>
<Sockets num-sockets="2">
<BlockConnector con-block-id="125" connector-kind="socket" connector-type="string" init-type="string" label="Topic" position-type="single"/>
<BlockConnector con-block-id="121" connector-kind="socket" connector-type="string" init-type="string" label="Text" position-type="single"/>
</Sockets>
</Block>
<Block genus-name="mqtttopic" id="125">
<Label>/luftfeuchtigkeit</Label>
<Location>
<X>408</X>
<Y>424</Y>
</Location>
<Plug>
<BlockConnector con-block-id="124" connector-kind="plug" connector-type="string" init-type="string" label="" position-type="single"/>
</Plug>
</Block>
<Block genus-name="mqttpublish" id="118">
<Location>
<X>264</X>
<Y>350</Y>
</Location>
<BeforeBlockId>113</BeforeBlockId>
<AfterBlockId>124</AfterBlockId>
<Sockets num-sockets="2">
<BlockConnector con-block-id="119" connector-kind="socket" connector-type="string" init-type="string" label="Topic" position-type="single"/>
<BlockConnector con-block-id="127" connector-kind="socket" connector-type="string" init-type="string" label="Text" position-type="single"/>
</Sockets>
</Block>
<Block genus-name="mqtttopic" id="119">
<Label>/temperatur</Label>
<Location>
<X>408</X>
<Y>353</Y>
</Location>
<Plug>
<BlockConnector con-block-id="118" connector-kind="plug" connector-type="string" init-type="string" label="" position-type="single"/>
</Plug>
</Block>
<Block genus-name="mqttpublish" id="113">
<Location>
<X>264</X>
<Y>296</Y>
</Location>
<BeforeBlockId>104</BeforeBlockId>
<AfterBlockId>118</AfterBlockId>
<Sockets num-sockets="2">
<BlockConnector con-block-id="114" connector-kind="socket" connector-type="string" init-type="string" label="Topic" position-type="single"/>
<BlockConnector con-block-id="115" connector-kind="socket" connector-type="string" init-type="string" label="Text" position-type="single"/>
</Sockets>
</Block>
<Block genus-name="IoTmessage" id="115">
<Label>Hallo IoTHackathon</Label>
<Location>
<X>408</X>
<Y>323</Y>
</Location>
<Plug>
<BlockConnector con-block-id="113" connector-kind="plug" connector-type="string" init-type="string" label="" position-type="single"/>
</Plug>
<Sockets num-sockets="1">
<BlockConnector connector-kind="socket" connector-type="string" init-type="string" label="" position-type="single"/>
</Sockets>
</Block>
<Block genus-name="mqtttopic" id="114">
<Label>/octopus</Label>
<Location>
<X>408</X>
<Y>299</Y>
</Location>
<Plug>
<BlockConnector con-block-id="113" connector-kind="plug" connector-type="string" init-type="string" label="" position-type="single"/>
</Plug>
</Block>
<Block genus-name="mqttconnect" id="108">
<Location>
<X>265</X>
<Y>181</Y>
</Location>
<BeforeBlockId>105</BeforeBlockId>
<Sockets num-sockets="4">
<BlockConnector con-block-id="109" connector-kind="socket" connector-type="string" init-type="string" label="Broker" position-type="single"/>
<BlockConnector con-block-id="110" connector-kind="socket" connector-type="string" init-type="string" label="Client" position-type="single"/>
<BlockConnector con-block-id="111" connector-kind="socket" connector-type="string" init-type="string" label="User" position-type="single"/>
<BlockConnector con-block-id="112" connector-kind="socket" connector-type="string" init-type="string" label="Passwort" position-type="single"/>
</Sockets>
</Block>
<Block genus-name="GPRSpass" id="112">
<Label>mqtt</Label>
<Location>
<X>371</X>
<Y>256</Y>
</Location>
<Plug>
<BlockConnector con-block-id="108" connector-kind="plug" connector-type="string" init-type="string" label="" position-type="single"/>
</Plug>
</Block>
<Block genus-name="GPRSuser" id="111">
<Label>mqtt</Label>
<Location>
<X>371</X>
<Y>232</Y>
</Location>
<Plug>
<BlockConnector con-block-id="108" connector-kind="plug" connector-type="string" init-type="string" label="" position-type="single"/>
</Plug>
</Block>
<Block genus-name="mqttclient" id="110">
<Label>octopus</Label>
<Location>
<X>371</X>
<Y>208</Y>
</Location>
<Plug>
<BlockConnector con-block-id="108" connector-kind="plug" connector-type="string" init-type="string" label="" position-type="single"/>
</Plug>
</Block>
<Block genus-name="mqttbroker" id="109">
<Label>172.20.10.2</Label>
<Location>
<X>371</X>
<Y>184</Y>
</Location>
<Plug>
<BlockConnector con-block-id="108" connector-kind="plug" connector-type="string" init-type="string" label="" position-type="single"/>
</Plug>
</Block>
<Block genus-name="connectWLANAP" id="105">
<Location>
<X>265</X>
<Y>108</Y>
</Location>
<BeforeBlockId>104</BeforeBlockId>
<AfterBlockId>108</AfterBlockId>
<Sockets num-sockets="2">
<BlockConnector con-block-id="106" connector-kind="socket" connector-type="string" init-type="string" label="Netzname" position-type="single"/>
<BlockConnector con-block-id="107" connector-kind="socket" connector-type="string" init-type="string" label="Passwort" position-type="single"/>
</Sockets>
</Block>
<Block genus-name="passwort" id="107">
<Label>YOURWIFIPWD</Label>
<Location>
<X>443</X>
<Y>135</Y>
</Location>
<Plug>
<BlockConnector con-block-id="105" connector-kind="plug" connector-type="string" init-type="string" label="" position-type="single"/>
</Plug>
</Block>
<Block genus-name="ssid" id="106">
<Label>YOURWIFISSID</Label>
<Location>
<X>443</X>
<Y>111</Y>
</Location>
<Plug>
<BlockConnector con-block-id="105" connector-kind="plug" connector-type="string" init-type="string" label="" position-type="single"/>
</Plug>
</Block>
<Block genus-name="program" id="104">
<Location>
<X>100</X>
<Y>100</Y>
</Location>
<Sockets num-sockets="2">
<BlockConnector con-block-id="105" connector-kind="socket" connector-type="cmd" init-type="cmd" label="Setup" position-type="single"/>
<BlockConnector con-block-id="113" connector-kind="socket" connector-type="cmd" init-type="cmd" label="Schleife" position-type="single"/>
</Sockets>
</Block>
</PageBlocks>
</Page>
</Pages>
</cb:CODEBLOCKS>

node-RED Flow

Textile
Import this flow via the clipboard into node-RED
[
   {
       "id": "5b61a7af.00a208",
       "type": "mqtt in",
       "z": "3dd27bf1.2fba14",
       "name": "octopus",
       "topic": "/octopus",
       "qos": "2",
       "broker": "6cafbcd2.7eca9c",
       "x": 200,
       "y": 100,
       "wires": [
           [
               "395d2bac.ea8e4c"
           ]
       ]
   },
   {
       "id": "395d2bac.ea8e4c",
       "type": "debug",
       "z": "3dd27bf1.2fba14",
       "name": "",
       "active": true,
       "tosidebar": true,
       "console": false,
       "tostatus": false,
       "complete": "payload",
       "x": 430,
       "y": 100,
       "wires": []
   },
   {
       "id": "f98c6a39.cd5c78",
       "type": "mqtt in",
       "z": "3dd27bf1.2fba14",
       "name": "temperatur",
       "topic": "/temperatur",
       "qos": "2",
       "broker": "6cafbcd2.7eca9c",
       "x": 200,
       "y": 180,
       "wires": [
           [
               "8a448cea.1686a8",
               "7d3704fb.0ff13c",
               "d963848e.fc1378"
           ]
       ]
   },
   {
       "id": "8a448cea.1686a8",
       "type": "debug",
       "z": "3dd27bf1.2fba14",
       "name": "",
       "active": true,
       "tosidebar": true,
       "console": false,
       "tostatus": false,
       "complete": "false",
       "x": 430,
       "y": 180,
       "wires": []
   },
   {
       "id": "7d3704fb.0ff13c",
       "type": "ui_gauge",
       "z": "3dd27bf1.2fba14",
       "name": "",
       "group": "f3958dcc.8ac6",
       "order": 0,
       "width": 0,
       "height": 0,
       "gtype": "gage",
       "title": "Temperatur",
       "label": "units",
       "format": "{{value}}",
       "min": "-10",
       "max": "60",
       "colors": [
           "#00B500",
           "#E6E600",
           "#CA3838"
       ],
       "seg1": "",
       "seg2": "",
       "x": 430,
       "y": 260,
       "wires": []
   },
   {
       "id": "d963848e.fc1378",
       "type": "ui_chart",
       "z": "3dd27bf1.2fba14",
       "name": "",
       "group": "10443d4d.5cac13",
       "order": 0,
       "width": 0,
       "height": 0,
       "label": "Temperatur",
       "chartType": "line",
       "legend": "false",
       "xformat": "HH:mm:ss",
       "interpolate": "bezier",
       "nodata": "",
       "dot": true,
       "ymin": "",
       "ymax": "",
       "removeOlder": 1,
       "removeOlderPoints": "",
       "removeOlderUnit": "3600",
       "cutout": 0,
       "useOneColor": false,
       "colors": [
           "#1F77B4",
           "#AEC7E8",
           "#FF7F0E",
           "#2CA02C",
           "#98DF8A",
           "#D62728",
           "#FF9896",
           "#9467BD",
           "#C5B0D5"
       ],
       "useOldStyle": false,
       "x": 430,
       "y": 320,
       "wires": [
           [],
           []
       ]
   },
   {
       "id": "7971c45d.3fdd84",
       "type": "mqtt in",
       "z": "3dd27bf1.2fba14",
       "name": "luftfeuchtigkeit",
       "topic": "/luftfeuchtigkeit",
       "qos": "2",
       "broker": "6cafbcd2.7eca9c",
       "x": 220,
       "y": 400,
       "wires": [
           [
               "3473b6fb.405afa",
               "774b138.37a086c"
           ]
       ]
   },
   {
       "id": "3473b6fb.405afa",
       "type": "debug",
       "z": "3dd27bf1.2fba14",
       "name": "",
       "active": true,
       "tosidebar": true,
       "console": false,
       "tostatus": false,
       "complete": "false",
       "x": 430,
       "y": 400,
       "wires": []
   },
   {
       "id": "774b138.37a086c",
       "type": "ui_gauge",
       "z": "3dd27bf1.2fba14",
       "name": "",
       "group": "f3958dcc.8ac6",
       "order": 0,
       "width": 0,
       "height": 0,
       "gtype": "wave",
       "title": "%",
       "label": "%",
       "format": "{{value}}",
       "min": 0,
       "max": "100",
       "colors": [
           "#00B500",
           "#E6E600",
           "#CA3838"
       ],
       "seg1": "",
       "seg2": "",
       "x": 410,
       "y": 460,
       "wires": []
   },
   {
       "id": "a4e2ef05.771c2",
       "type": "mqtt in",
       "z": "3dd27bf1.2fba14",
       "name": "luftdruck",
       "topic": "/luftdruck",
       "qos": "2",
       "broker": "6cafbcd2.7eca9c",
       "x": 210,
       "y": 560,
       "wires": [
           [
               "7107b17b.9fc46",
               "3f59758b.f66452",
               "d6b1f90d.ae992"
           ]
       ]
   },
   {
       "id": "7107b17b.9fc46",
       "type": "debug",
       "z": "3dd27bf1.2fba14",
       "name": "",
       "active": true,
       "tosidebar": true,
       "console": false,
       "tostatus": false,
       "complete": "false",
       "x": 430,
       "y": 560,
       "wires": []
   },
   {
       "id": "3f59758b.f66452",
       "type": "ui_gauge",
       "z": "3dd27bf1.2fba14",
       "name": "",
       "group": "f3958dcc.8ac6",
       "order": 0,
       "width": 0,
       "height": 0,
       "gtype": "gage",
       "title": "gauge",
       "label": "units",
       "format": "{{value}}",
       "min": "800",
       "max": "1200",
       "colors": [
           "#00B500",
           "#E6E600",
           "#CA3838"
       ],
       "seg1": "",
       "seg2": "",
       "x": 410,
       "y": 620,
       "wires": []
   },
   {
       "id": "d6b1f90d.ae992",
       "type": "ui_chart",
       "z": "3dd27bf1.2fba14",
       "name": "",
       "group": "10443d4d.5cac13",
       "order": 0,
       "width": 0,
       "height": 0,
       "label": "Luftdruck",
       "chartType": "line",
       "legend": "false",
       "xformat": "HH:mm:ss",
       "interpolate": "bezier",
       "nodata": "",
       "dot": true,
       "ymin": "",
       "ymax": "",
       "removeOlder": 1,
       "removeOlderPoints": "",
       "removeOlderUnit": "3600",
       "cutout": 0,
       "useOneColor": false,
       "colors": [
           "#1F77B4",
           "#AEC7E8",
           "#FF7F0E",
           "#2CA02C",
           "#98DF8A",
           "#D62728",
           "#FF9896",
           "#9467BD",
           "#C5B0D5"
       ],
       "useOldStyle": false,
       "x": 420,
       "y": 680,
       "wires": [
           [],
           []
       ]
   },
   {
       "id": "6cafbcd2.7eca9c",
       "type": "mqtt-broker",
       "z": "",
       "name": "",
       "broker": "localhost",
       "port": "1883",
       "clientid": "",
       "usetls": false,
       "compatmode": true,
       "keepalive": "60",
       "cleansession": true,
       "birthTopic": "",
       "birthQos": "0",
       "birthRetain": "false",
       "birthPayload": "",
       "closeTopic": "",
       "closeQos": "0",
       "closeRetain": "false",
       "closePayload": "",
       "willTopic": "",
       "willQos": "0",
       "willRetain": "false",
       "willPayload": ""
   },
   {
       "id": "f3958dcc.8ac6",
       "type": "ui_group",
       "z": "",
       "name": "Graph",
       "tab": "b8fdc503.5eeff8",
       "order": 1,
       "disp": true,
       "width": "5",
       "collapse": false
   },
   {
       "id": "10443d4d.5cac13",
       "type": "ui_group",
       "z": "",
       "name": "Sensor",
       "tab": "b8fdc503.5eeff8",
       "order": 2,
       "disp": true,
       "width": "10",
       "collapse": false
   },
   {
       "id": "b8fdc503.5eeff8",
       "type": "ui_tab",
       "z": "",
       "name": "Sensor Data Octopus",
       "icon": "dashboard",
       "order": 1
   }
]

Credits

fab-lab.eu

fab-lab.eu

22 projects • 276 followers
Maker!

Comments