{"id":2512,"date":"2018-12-11T00:00:22","date_gmt":"2018-12-10T23:00:22","guid":{"rendered":"https:\/\/iot.fkainka.de\/?p=2512"},"modified":"2019-12-11T21:57:50","modified_gmt":"2019-12-11T20:57:50","slug":"day11-2018","status":"publish","type":"post","link":"https:\/\/iot.fkainka.de\/en\/day11-2018","title":{"rendered":"Day 11: MQTT"},"content":{"rendered":"<p>There is a cable set again hidden behind the door today. We will need it in the next few days when the setups become more complex.<\/p>\n<p>Over the past few days we have sent most of our data via UDP. This protocol is very convenient; on the one hand it has a small overhead (data is sent in addition to the actual application data) and on the other hand, it is a broadcasting system and can thus send data to several devices simultaneously. But today we will learn about a second protocol that plays a major role in IoT. This is the message protocol MQTT (<strong>Message Queue Telemetry Transport<\/strong>) and it was specifically developed for machine-to-machine communication (M2M). The reason that this is such a brilliant protocol is due to its compactness and simplicity when it comes to exchanging data between devices such as control data, measurement data or text. Compared to HTTP, the overhead is very small. That is why embedded devices such as microcontrollers like our NodeESP can process the data well.<\/p>\n<p>A so-called Broker forms the basis for communication. A broker is a server application that processes all the messages. A device like our NodeESP can connect to the broker and publish messages and also send them to the broker. Another device can then subscribe to certain topics and will instantly receive all new messages from the broker published on these topics.<\/p>\n<div id=\"attachment_2513\" style=\"width: 626px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/iot.fkainka.de\/wp-content\/uploads\/2018\/11\/T11_MQTT_1.jpg\"><img aria-describedby=\"caption-attachment-2513\" decoding=\"async\" loading=\"lazy\" class=\"size-large wp-image-2513\" src=\"https:\/\/iot.fkainka.de\/wp-content\/uploads\/2018\/11\/T11_MQTT_1-1024x669.jpg\" alt=\"Der MQTT-Broker stellt die Nachrichten zu\" width=\"616\" height=\"402\" data-id=\"2513\" srcset=\"https:\/\/iot.fkainka.de\/wp-content\/uploads\/2018\/11\/T11_MQTT_1-1024x669.jpg 1024w, https:\/\/iot.fkainka.de\/wp-content\/uploads\/2018\/11\/T11_MQTT_1-300x196.jpg 300w, https:\/\/iot.fkainka.de\/wp-content\/uploads\/2018\/11\/T11_MQTT_1-768x501.jpg 768w, https:\/\/iot.fkainka.de\/wp-content\/uploads\/2018\/11\/T11_MQTT_1-972x635.jpg 972w, https:\/\/iot.fkainka.de\/wp-content\/uploads\/2018\/11\/T11_MQTT_1.jpg 1550w\" sizes=\"(max-width: 616px) 100vw, 616px\" \/><\/a><p id=\"caption-attachment-2513\" class=\"wp-caption-text\">The MQTT broker delivers messages<\/p><\/div>\n<p>Topics are structured hierarchically, whereby subtopics are separated by <strong>\/<\/strong>. And then there are the wildcards. You can subscribe to several topics at the same time using the signs <strong>+<\/strong> or <strong>#<\/strong>. If you subscribed to <strong>MyHome\/+\/Temperature<\/strong> you would receive the topics <strong>MyHome\/Kitchen\/Temperature<\/strong> and <strong>MyHome\/ Bathroom\/Temperature<\/strong> all together for example. &#8220;Plus&#8221; will replace only one topic level and &#8220;hash&#8221; will replace them all. You would receive all messages posted under all subtopics of <strong>MyHome<\/strong> if you subscribed to <strong>MyHome\/#. <\/strong>A message can be just about anything: from plain text to readings to simple 1\/0 light switching commands.<\/p>\n<p>We don\u2019t need to set up hardware today. We will start with some basic tests. Go to the settings on the configuration page. Most certainly you have already noticed the area with <strong>MQTT Settings<\/strong>. Here you will find already properly preconfigured settings. We have set up a server especially for the Advent calendar project as the broker address, namely <strong>broker.fkainka.de<\/strong>. But you can also set up a broker yourself on your Raspberry Pi.\u00a0 I recommend the open-source software <strong>Mosquitto<\/strong>. The client ID is generated using the board\u2019s MAC address. This ID is globally assigned to one single device across all servers. As a MAC address should be unique in the world, it is very suitable as an ID here. You don\u2019t need a username and password for the test broker.<\/p>\n<p>Test the connection by clicking on the <strong>Test Connection<\/strong> button below. The colour of the area next to it should change to green, if the connection to the broker was established successfully. If this won\u2019t work, it may be that the server is down and you need to change the broker. An alternative freely available broker is<strong> for example broker.mqttdashboard.com<\/strong>.<\/p>\n<p>Once you have tested the basic connection, you can now set up the program of today. Then go to the page<\/p>\n<p style=\"text-align: center;\"><a href=\"https:\/\/www.hivemq.com\/demos\/websocket-client\/?\">https:\/\/www.hivemq.com\/demos\/websocket-client\/?<\/a><\/p>\n<div id=\"attachment_2514\" style=\"width: 653px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/iot.fkainka.de\/wp-content\/uploads\/2018\/11\/T11_Broker.jpg\"><img aria-describedby=\"caption-attachment-2514\" decoding=\"async\" loading=\"lazy\" class=\" wp-image-2514\" src=\"https:\/\/iot.fkainka.de\/wp-content\/uploads\/2018\/11\/T11_Broker.jpg\" alt=\"Die HiveMQ Seite\" width=\"643\" height=\"460\" data-id=\"2514\" srcset=\"https:\/\/iot.fkainka.de\/wp-content\/uploads\/2018\/11\/T11_Broker.jpg 852w, https:\/\/iot.fkainka.de\/wp-content\/uploads\/2018\/11\/T11_Broker-300x215.jpg 300w, https:\/\/iot.fkainka.de\/wp-content\/uploads\/2018\/11\/T11_Broker-768x550.jpg 768w\" sizes=\"(max-width: 643px) 100vw, 643px\" \/><\/a><p id=\"caption-attachment-2514\" class=\"wp-caption-text\">The HiveMQ Page<\/p><\/div>\n<p>and click <strong>Try the websocket client<\/strong>. You will see a log on screen to an MQTT broker with WebSocket support. Under <strong>Host<\/strong>, enter a broker, here we use <strong>broker.fkainka.de<\/strong>, and then <strong>connect<\/strong>. The screen you are seeing above opens next. You can now subscribe to your own personal topic by clicking on <strong>Add New Topic Subscription<\/strong>. The topic consists of the term NodeESP and the client ID. For my board this was for example:<\/p>\n<p><strong>NodeESP\/240AC48178E4\/<\/strong><\/p>\n<p>Any MQTT communication from and to the board will start with this topic. From now on I will shorten the client ID part using <strong>[PID]<\/strong> which stands for &#8220;personal ID&#8221;. Of course, you will need to replace this part with your own client ID. Experiment with receiving all communication from the board using the topic<\/p>\n<p><strong>NodeESP\/[PID]\/#<\/strong><\/p>\n<p>When you reset the board, you will see a message soon after telling you that the board has established a connection to the broker. This message is written to the topic <strong>NodeESP\/[PID]\/status<\/strong> and by subscribing with # you will also receive it. Now you can send your first message to the board. To do this, in the <strong>Publish<\/strong> area enter your personal topic plus the subtopic<strong> testtopic <\/strong>into the <strong>Topic<\/strong> field. In my case:<\/p>\n<p><strong>NodeESP\/[PID]\/testtopic<\/strong><\/p>\n<p>Enter any message into the <strong>Message<\/strong> field. You can also leave the field blank. Then click <strong>Publish<\/strong> and a moment later you will receive two messages in the <strong>Messages <\/strong>area. One is a welcoming text and was published under the <strong>NodeESP\/[PID]\/testtopic\/out topic<\/strong>. The second message is also welcoming text but as JSON string including command key, and was published under the <strong>NodeESP\/[PID]\/out <\/strong>topic.<\/p>\n<p>These two messages are the result of the board converting the topics into the known command format and then evaluating them. The answers appear in a topic that is composed of the command <strong>out<\/strong>, in our case that is.<strong>..\/testtopic \/out<\/strong> and at the same time also in the <strong>NodeESP\/[PID]\/out <\/strong>channel.<\/p>\n<p>The equivalent to the <strong>out <\/strong>topic is the <strong>in <\/strong>topic, where you can directly publish commands in JSON format. For example, if you wrote to the <strong>NodeESP\/[PID]\/<\/strong> topic the JSON string <strong>{&#8220;cmd&#8221;:&#8221;testtopic&#8221;}<\/strong>, you would get the same answers as in your first experiment. This means that you can send a command either via a topic or via the <strong>in<\/strong>-channel as a JSON object. Don\u2019t be surprised to find out that some well-known commands such <strong>get_wifi_status<\/strong> etc. won\u2019t work. The reason is that these commands may contain sensitive data and should not be sent over a public broker.<\/p>\n<p>The connection to the board via MQTT is now established. Tomorrow we will take a closer look and learn what we can do with it.<\/p>","protected":false},"excerpt":{"rendered":"<p>There is a cable set again hidden behind the door today. We will need it in the next few days when the setups become more complex. Over the past few days we have sent most of our data via UDP&#8230;.<br \/><a class=\"read-more-button\" href=\"https:\/\/iot.fkainka.de\/en\/day11-2018\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":2513,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[315,312,313],"tags":[],"jetpack_publicize_connections":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Day 11: MQTT - Internet of Things with the NanoESP<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/iot.fkainka.de\/en\/day11-2018\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Day 11: MQTT - Internet of Things with the NanoESP\" \/>\n<meta property=\"og:description\" content=\"There is a cable set again hidden behind the door today. We will need it in the next few days when the setups become more complex. Over the past few days we have sent most of our data via UDP....Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/iot.fkainka.de\/en\/day11-2018\" \/>\n<meta property=\"og:site_name\" content=\"Internet of Things with the NanoESP\" \/>\n<meta property=\"article:published_time\" content=\"2018-12-10T23:00:22+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-12-11T20:57:50+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/iot.fkainka.de\/wp-content\/uploads\/2018\/11\/T11_MQTT_1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1550\" \/>\n\t<meta property=\"og:image:height\" content=\"1012\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"fk\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"fk\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/iot.fkainka.de\/en\/day11-2018\",\"url\":\"https:\/\/iot.fkainka.de\/en\/day11-2018\",\"name\":\"Day 11: MQTT - Internet of Things with the NanoESP\",\"isPartOf\":{\"@id\":\"https:\/\/iot.fkainka.de\/#website\"},\"datePublished\":\"2018-12-10T23:00:22+00:00\",\"dateModified\":\"2019-12-11T20:57:50+00:00\",\"author\":{\"@id\":\"https:\/\/iot.fkainka.de\/#\/schema\/person\/d3205fdf2649027fc35e240d4f8b285a\"},\"breadcrumb\":{\"@id\":\"https:\/\/iot.fkainka.de\/en\/day11-2018#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/iot.fkainka.de\/en\/day11-2018\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/iot.fkainka.de\/en\/day11-2018#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/iot.fkainka.de\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Day 11: MQTT\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/iot.fkainka.de\/#website\",\"url\":\"https:\/\/iot.fkainka.de\/\",\"name\":\"Internet of Things with the NanoESP\",\"description\":\"The webpage for the Arduino compatible WiFi-Board\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/iot.fkainka.de\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/iot.fkainka.de\/#\/schema\/person\/d3205fdf2649027fc35e240d4f8b285a\",\"name\":\"fk\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/iot.fkainka.de\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/141cf958a831e66efabf9674e117e8d9?s=96&d=identicon&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/141cf958a831e66efabf9674e117e8d9?s=96&d=identicon&r=g\",\"caption\":\"fk\"},\"url\":\"https:\/\/iot.fkainka.de\/en\/author\/fk\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Day 11: MQTT - Internet of Things with the NanoESP","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/iot.fkainka.de\/en\/day11-2018","og_locale":"en_US","og_type":"article","og_title":"Day 11: MQTT - Internet of Things with the NanoESP","og_description":"There is a cable set again hidden behind the door today. We will need it in the next few days when the setups become more complex. Over the past few days we have sent most of our data via UDP....Read more","og_url":"https:\/\/iot.fkainka.de\/en\/day11-2018","og_site_name":"Internet of Things with the NanoESP","article_published_time":"2018-12-10T23:00:22+00:00","article_modified_time":"2019-12-11T20:57:50+00:00","og_image":[{"width":1550,"height":1012,"url":"https:\/\/iot.fkainka.de\/wp-content\/uploads\/2018\/11\/T11_MQTT_1.jpg","type":"image\/jpeg"}],"author":"fk","twitter_card":"summary_large_image","twitter_misc":{"Written by":"fk","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/iot.fkainka.de\/en\/day11-2018","url":"https:\/\/iot.fkainka.de\/en\/day11-2018","name":"Day 11: MQTT - Internet of Things with the NanoESP","isPartOf":{"@id":"https:\/\/iot.fkainka.de\/#website"},"datePublished":"2018-12-10T23:00:22+00:00","dateModified":"2019-12-11T20:57:50+00:00","author":{"@id":"https:\/\/iot.fkainka.de\/#\/schema\/person\/d3205fdf2649027fc35e240d4f8b285a"},"breadcrumb":{"@id":"https:\/\/iot.fkainka.de\/en\/day11-2018#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/iot.fkainka.de\/en\/day11-2018"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/iot.fkainka.de\/en\/day11-2018#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/iot.fkainka.de\/"},{"@type":"ListItem","position":2,"name":"Day 11: MQTT"}]},{"@type":"WebSite","@id":"https:\/\/iot.fkainka.de\/#website","url":"https:\/\/iot.fkainka.de\/","name":"Internet of Things with the NanoESP","description":"The webpage for the Arduino compatible WiFi-Board","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/iot.fkainka.de\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/iot.fkainka.de\/#\/schema\/person\/d3205fdf2649027fc35e240d4f8b285a","name":"fk","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/iot.fkainka.de\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/141cf958a831e66efabf9674e117e8d9?s=96&d=identicon&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/141cf958a831e66efabf9674e117e8d9?s=96&d=identicon&r=g","caption":"fk"},"url":"https:\/\/iot.fkainka.de\/en\/author\/fk"}]}},"jetpack_featured_media_url":"https:\/\/iot.fkainka.de\/wp-content\/uploads\/2018\/11\/T11_MQTT_1.jpg","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6McI0-Ew","_links":{"self":[{"href":"https:\/\/iot.fkainka.de\/en\/wp-json\/wp\/v2\/posts\/2512"}],"collection":[{"href":"https:\/\/iot.fkainka.de\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/iot.fkainka.de\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/iot.fkainka.de\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/iot.fkainka.de\/en\/wp-json\/wp\/v2\/comments?post=2512"}],"version-history":[{"count":7,"href":"https:\/\/iot.fkainka.de\/en\/wp-json\/wp\/v2\/posts\/2512\/revisions"}],"predecessor-version":[{"id":3081,"href":"https:\/\/iot.fkainka.de\/en\/wp-json\/wp\/v2\/posts\/2512\/revisions\/3081"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/iot.fkainka.de\/en\/wp-json\/wp\/v2\/media\/2513"}],"wp:attachment":[{"href":"https:\/\/iot.fkainka.de\/en\/wp-json\/wp\/v2\/media?parent=2512"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iot.fkainka.de\/en\/wp-json\/wp\/v2\/categories?post=2512"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iot.fkainka.de\/en\/wp-json\/wp\/v2\/tags?post=2512"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}