Overview of the MQTT Protocol

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 popular, lighter weight, solution I’d like to discuss the basics of is MQTT.

MQTT

While MQTT used to stand for MQ Telemetry Transport, it is now classified as not being an acronym. So what is it? What does it do? Well, it is a messaging protocol using TCP 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’s focus in on one of the main MQTT concepts, the publish/subscribe pattern.

MQTT Publish/Subscribe Pattern

In a publish/subscribe pattern a client publishes information and another client can subscribe 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.

The decoupling can occur in a few different ways, space, time, and synchronization.

  • Space – the subscriber doesn’t need to know who the publisher is, for example by IP address, and vice-versa
  • Time – the two clients don’t have to be running at the same time
  • Synchronization – Publishing and receiving doesn’t halt operations

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.

Thinking about an IoT situation with, for example, a NodeMCU device with some environmental sensors connected to it such as a TMP36 temperature sensor and a moisture sensor 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.

MQTT Publish-Subscribe Model

 

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.

Message Packet

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.

Wrap Up

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’s world, that MQTT has security features as well.


Follow me on Twitter @kenwalger to get the latest updates on my postings on IoT topics. If you enjoyed this article, or have questions, leave comments below.

Facebooktwitterredditlinkedinmail

A Review of PyCon 2017

PyCon 2017 was held about 45 miles south of Portland, Oregon where I am fortunate enough to live. I am typically not a great conference attendee so I was a bit nervous and apprehensive about going. After walking into the Portland Convention Center and being surrounded by 3,500 fellow Python enthusiasts, however, I was super excited.

PyCon 2017 Talks

There were several amazing talks on a wide array of subjects. There were first time PyCon presenters like Jonas Neubert with a fascinating talk on using Python for factory automation. Along with some “heavy hitters” in the Python community such as Philip James and Daniele Procida. Daniele’s talk on documentation was very interesting and definitely a different way of thinking of of the documentation process.

There were some really good keynote speeches as well. I really enjoyed learning from Lisa Guo on the migration path Instagram took to go from Python 2 to Python 3. I also found Dr. Katy Huff‘s talk on some practical applications for Python in the science field to be quite interesting.

Kenneth Love gave a tutorial on the Django Admin which I would encourage people to work through as well. As usual it is a great presentation and Mr. Love provides excellent information.

There were many other great talks, from many great speakers. The conference talks are available on YouTube here. I’d encourage you to watch, listen, and learn from as many as possible.

Vendors

There were some great vendors and businesses in attendance. Big companies like Google, Microsoft, Intel, and Facebook/Instagram were all there. They all offered some excellent short talks in their booths on how they were using Python for their applications or products. They also had a variety of swag they were giving away in exchange for a badge scan.

Google had an hourly trivia session and provided prizes for the correct answer. I picked up a copy of Python in a Nutshell: A Desktop Quick Reference for knowing that PyCon 2018 would be held in Cleveland, Ohio. Intel had several drawings for an Amazon Echo, along with several other vendors actually. Booz Allen Hamilton had several drawings for a Raspberry PI 3 Model B which I sadly wasn’t able to win.

Company branded socks were one of the big swag things at PyCon, beyond the t-shirts, stickers, and fidget spinners. Microsoft, O’Reilly Publishing, Citus Data, and Heroku all had custom socks. Scan your badge… get socks.

PyCon 2017 Vendor socks

Community

In addition to the great learning opportunities the talks provided, and the interaction with sponsors, another key feature of any conference is the people. The opportunity to get to meet and talk with people like Andrew Pinkham who authored Django Unleashed, or Russell Keith-Magee of BeeWare was spectacular. Having the chance to meet and talk with other developers about industry trends was great as well.

I have heard that the Python community is second to none in terms of inclusiveness and I was able to witness first hand that is indeed the case. Due to the exceptional overall experience I had at PyCon 2017, I am sincerely hoping that I can make to the trip to Cleveland in 2018.

Facebooktwitterredditlinkedinmail