MQTT Sparkplug Tutorial
In this article, I’m going to describe to you what MQTT and Sparkplug B is and how it works. I’m going to begin by taking you on a deep dive into MQTT, and then I’ll describe how the Sparkplug B specification leverages the MTT network infrastructure. After that, we’ll go through a demo where I’ll take you through steps of setting up an MQTT Sparkplug enabled device and connect it to an MQTT broker to start publishing messages into the MQTT network network.
If your prefer to watch the video version of this article then you may click on the YouTube link below.
MQTT is a communication protocol used by devices and applications that need to exchange information in an Internet of Things network. To facilitate communication, it uses what is called a publish-subscribe model of communication, whereby a centralized server called a Broker manages the delivery of messages to clients, which are devices and application connected to a network. And to identify the published data clients use hierarchical topics which can be created on the fly such as the one shown below.
Now let us zoom into a typical communication session between an MQTT client and server, as depicted in the image below. First of all, for an MQTT Client and Server to successfully connect and communicate with each other, there are certain communication technologies that need to be present, and they are stacked up in layers.
The first layer is what your IoT device software would interact with, and this layer comprises of software libraries that know how to convert your device message into an MQTT packet, we’ll discuss what an MQTT packet looks like shortly. As an example, if this was an HTTP based communication network, this is also the layer that would be responsible for creating HTTP packets.
The second layer is also another software entity that resides on your device. Conceptually it is called the Transport layer. Its job is to accept an MQTT packet from the application layer through what is called a port, and MQTT uses port 1883 for non secure communication and port 8883 for secure communications. Now, when the transport layer gets the MQTT packet, it splits it into smaller packets to be individually sent over the Internet.
And to achieve that, the transport layer employs the TCP standard, which defines how to initiate and maintain a conversation with an application layer protocols such as MQTT. The third layer is the Internet layer, which is yet another software entity that resides on your device.
So what happens is that when your transport layer is finished splitting your MQTT messages into packets and adding the necessary control information, it pushes the packet onto the internet layer. The Internet layer then uses the rules specified by the Internet Protocol to attach your Broker IP address and your device IP address on each of the packets so as to make sure they arrive where they are intended to go. And then finally, your MQTT message packets are placed on a physical medium where they are transmitted as electrical signals.