{"id":309,"date":"2017-05-24T07:24:01","date_gmt":"2017-05-24T14:24:01","guid":{"rendered":"http:\/\/www.kenwalger.com\/blog\/?p=309"},"modified":"2017-05-24T08:35:50","modified_gmt":"2017-05-24T15:35:50","slug":"overview-mqtt","status":"publish","type":"post","link":"https:\/\/www.kenwalger.com\/blog\/iot\/overview-mqtt\/","title":{"rendered":"Overview of the MQTT Protocol"},"content":{"rendered":"<p>There are several different ways one can utilize to connect an Internet of Things (IoT) device to a network. I have <a href=\"http:\/\/www.kenwalger.com\/blog\/iot\/network-access-micropython\/\">previously<\/a> discussed some of the setup for a <a href=\"https:\/\/www.amazon.com\/gp\/product\/B01N0B48NI\/ref=as_li_tl?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=B01N0B48NI&amp;linkCode=as2&amp;tag=kenwalgersite-20&amp;linkId=0046d38f856ec3e7daa13be68ab68b3b\" target=\"_blank\" rel=\"noopener noreferrer\">NodeMCU development board<\/a><img loading=\"lazy\" decoding=\"async\" style=\"border: none !important; margin: 0px !important;\" src=\"\/\/ir-na.amazon-adsystem.com\/e\/ir?t=kenwalgersite-20&amp;l=am2&amp;o=1&amp;a=B01N0B48NI\" alt=\"\" width=\"1\" height=\"1\" border=\"0\" \/> to connect to a WiFi network. In an IoT environment full blown network protocols such as HTTP can be a bit heavy. A popular, lighter weight, solution I&#8217;d like to discuss the basics of is\u00a0MQTT.<\/p>\n<h3>MQTT<\/h3>\n<p>While MQTT\u00a0<em>used<\/em> to stand for MQ Telemetry Transport, it\u00a0is now classified as\u00a0<strong>not<\/strong> being an acronym. So what is it? What does it do? Well, it is a messaging protocol using <a href=\"https:\/\/en.wikipedia.org\/wiki\/Transmission_Control_Protocol\">TCP<\/a> of TCP\/IP fame to provide publish\/subscribe services. It was, and is, designed for small, constrained devices and makes design decisions based on those constraints. Concepts which are important in the IoT world, such as memory, bandwidth, latency, power consumption, and network reliability. Let&#8217;s focus in on one of the main MQTT concepts, the publish\/subscribe pattern.<\/p>\n<h3>MQTT Publish\/Subscribe Pattern<\/h3>\n<p>In a publish\/subscribe pattern a client publishes information and another client can\u00a0<em>subscribe<\/em> to the information it wants. In many cases there is a broker between the clients who facilitates and\/or filters the information. This allows for a loose coupling between entities.<\/p>\n<p>The decoupling can occur in a few different ways, space, time, and synchronization.<\/p>\n<ul>\n<li>Space &#8211; the subscriber doesn&#8217;t need to know who the publisher is, for example by IP address, and vice-versa<\/li>\n<li>Time &#8211; the two clients don&#8217;t have to be running at the same time<\/li>\n<li>Synchronization &#8211; Publishing and receiving doesn&#8217;t halt operations<\/li>\n<\/ul>\n<p>Through the filtering done on the broker not all subscribers have to get the same messages. The broker can filter on subject, content, or type of message. A client, therefore could subscribe to only messages about temperate data. Or only messages with content about centrifuge machines. Or, perhaps, we only want to receive information about specific types of errors.<\/p>\n<p>Thinking about an IoT situation with, for example, a NodeMCU device with some environmental sensors connected to it such as a <a href=\"https:\/\/www.amazon.com\/gp\/product\/B00JYQAIBM\/ref=as_li_tl?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=B00JYQAIBM&amp;linkCode=as2&amp;tag=kenwalgersite-20&amp;linkId=8821b4ca99905bdc27b91f31a6cf6025\" target=\"_blank\" rel=\"noopener noreferrer\">TMP36 temperature sensor<\/a><img loading=\"lazy\" decoding=\"async\" style=\"border: none !important; margin: 0px !important;\" src=\"\/\/ir-na.amazon-adsystem.com\/e\/ir?t=kenwalgersite-20&amp;l=am2&amp;o=1&amp;a=B00JYQAIBM\" alt=\"\" width=\"1\" height=\"1\" border=\"0\" \/> and a <a href=\"https:\/\/www.amazon.com\/gp\/product\/B00AFCNR3U\/ref=as_li_tl?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=B00AFCNR3U&amp;linkCode=as2&amp;tag=kenwalgersite-20&amp;linkId=6ec157d39e27d84ac9e6ede2c459c641\" target=\"_blank\" rel=\"noopener noreferrer\">moisture sensor<\/a><img loading=\"lazy\" decoding=\"async\" style=\"border: none !important; margin: 0px !important;\" src=\"\/\/ir-na.amazon-adsystem.com\/e\/ir?t=kenwalgersite-20&amp;l=am2&amp;o=1&amp;a=B00AFCNR3U\" alt=\"\" width=\"1\" height=\"1\" border=\"0\" \/> we could be publishing that data. Some clients that are connected to our broker may be interested in that, others may not. They would subscribe to the information they wanted.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/uploads\/2017\/05\/pub-sub-model.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"325\" data-permalink=\"https:\/\/www.kenwalger.com\/blog\/iot\/overview-mqtt\/attachment\/pub-sub-model\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/uploads\/2017\/05\/pub-sub-model.png?fit=611%2C396&amp;ssl=1\" data-orig-size=\"611,396\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"pub-sub-model\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/uploads\/2017\/05\/pub-sub-model.png?fit=611%2C396&amp;ssl=1\" class=\"alignnone size-full wp-image-325\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/uploads\/2017\/05\/pub-sub-model.png?resize=611%2C396\" alt=\"MQTT Publish-Subscribe Model\" width=\"611\" height=\"396\" srcset=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/uploads\/2017\/05\/pub-sub-model.png?w=611&amp;ssl=1 611w, https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/uploads\/2017\/05\/pub-sub-model.png?resize=300%2C194&amp;ssl=1 300w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Once connected to the broker the publishing client simply sends its data to the broker. Once there, the broker relays the appropriate data onto the clients who have subscribed for that data. Again, those subscriptions can be filtered. All of this data transferring is done in a light weight fashion designed for small, resource limited devices.<\/p>\n<h6>Message Packet<\/h6>\n<p>The message packet shown in the above diagram is just an example. Along with the message, or payload, a real packet would include additional information such as a packet ID, topic name, quality of service (QoS) level. Also included in the packet would be flags so the broker knows how long to retain the message and if the message is a duplicate.<\/p>\n<h3>Wrap Up<\/h3>\n<p>This is just the tip of the iceberg for MQTT, there are several other features of interest as well. Features such as Retained Messages, Quality of Service, Last Will and Testament, Persistent Sessions, and SYS Topics. It should also come as no surprise given the importance of security in today&#8217;s world, that MQTT has security features as well.<\/p>\n<hr \/>\n<p>Follow me on Twitter <a href=\"https:\/\/www.twitter.com\/kenwalger\">@kenwalger<\/a> to get the latest updates on my postings on IoT topics. If you enjoyed this article, or have questions, leave comments below.<\/p>\n<a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-facebook nolightbox\" data-provider=\"facebook\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Facebook\" href=\"https:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F309&#038;t=Overview%20of%20the%20MQTT%20Protocol&#038;s=100&#038;p&#091;url&#093;=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F309&#038;p&#091;images&#093;&#091;0&#093;=https%3A%2F%2Fi0.wp.com%2Fwww.kenwalger.com%2Fblog%2Fwp-content%2Fuploads%2F2017%2F05%2Fmqtt-e1495426506232.png%3Ffit%3D125%252C125%26ssl%3D1&#038;p&#091;title&#093;=Overview%20of%20the%20MQTT%20Protocol\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"Facebook\" title=\"Share on Facebook\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/facebook.png?resize=48%2C48&#038;ssl=1\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-twitter nolightbox\" data-provider=\"twitter\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Twitter\" href=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F309&#038;text=Hey%20check%20this%20out\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"twitter\" title=\"Share on Twitter\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/twitter.png?resize=48%2C48&#038;ssl=1\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-reddit nolightbox\" data-provider=\"reddit\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Reddit\" href=\"https:\/\/www.reddit.com\/submit?url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F309&#038;title=Overview%20of%20the%20MQTT%20Protocol\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"reddit\" title=\"Share on Reddit\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/reddit.png?resize=48%2C48&#038;ssl=1\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-linkedin nolightbox\" data-provider=\"linkedin\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Linkedin\" href=\"https:\/\/www.linkedin.com\/shareArticle?mini=true&#038;url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F309&#038;title=Overview%20of%20the%20MQTT%20Protocol\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"linkedin\" title=\"Share on Linkedin\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/linkedin.png?resize=48%2C48&#038;ssl=1\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-mail nolightbox\" data-provider=\"mail\" rel=\"nofollow\" title=\"Share by email\" href=\"mailto:?subject=Overview%20of%20the%20MQTT%20Protocol&#038;body=Hey%20check%20this%20out:%20https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F309\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"mail\" title=\"Share by email\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/mail.png?resize=48%2C48&#038;ssl=1\" \/><\/a>","protected":false},"excerpt":{"rendered":"<p>There are several different ways one can utilize to connect an Internet of Things (IoT) device to a network. I have previously discussed some of the setup for a NodeMCU development board to connect to a WiFi network. In an IoT environment full blown network protocols such as HTTP can be a bit heavy. A &hellip; <a href=\"https:\/\/www.kenwalger.com\/blog\/iot\/overview-mqtt\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Overview of the MQTT Protocol&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":310,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pmpro_default_level":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[27,28],"tags":[60],"yst_prominent_words":[497,503,502,99,498,360,389,353,499,496,505,275,500,501,507,504,494,495,506,493],"class_list":["post-309","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-iot","category-micropython","tag-mqtt","pmpro-has-access"],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/uploads\/2017\/05\/mqtt-e1495426506232.png?fit=125%2C125&ssl=1","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8lx70-4Z","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts\/309","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/comments?post=309"}],"version-history":[{"count":8,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts\/309\/revisions"}],"predecessor-version":[{"id":326,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts\/309\/revisions\/326"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/media\/310"}],"wp:attachment":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/media?parent=309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/categories?post=309"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/tags?post=309"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/yst_prominent_words?post=309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}