The Message Queuing Telemetry Transport is a lightweight, publish-subscribe network protocol that transports messages between mostly low-powered devices via an intermediate software called a broker. The protocol usually runs over TCP/IP. This is different from than client-server protocol. In client-server protocol, the client makes a request and the server responds. The MQTT protocol provides a lightweight method of carrying out messaging using a publish/subscribe model. This makes it suitable for Internet of Things messaging such as with low power sensors or mobile devices such as phones, embedded computers or microcontrollers.

In the very basic, publisher (a publishing client in the system) can publish it’s own data to the broker. Some other interested consumers might tell the broker that we are interested in same kinds of data, if those type of data are available then let me know.  In that case, if no body is interested in data still publisher can publish the data. On the other had, some consumers (or subscriber client may be interested or has registered for data  but  there may not have any publisher, in that case they just wait). In most cases, broker software runs in the cloud and both publisher and subscriber can access the broker and send receive message.

Broker keeps trucks of all kinds or records like who is publishing, who is subscribing, should it remember the session if the client goes out of network etc. In this article, we shall build sample application using MS Windows and the Beckhoff PLC devices.


Free broker:

Eclipse Mosquitto is an open source (EPL/EDL licensed) message broker that implements the MQTT protocol versions 5.0, 3.1.1 and 3.1. Mosquitto is lightweight and is suitable for use on all devices from low power single board computers to full servers. The Mosquitto project also provides a C library for implementing MQTT clients, and the very popular mosquitto_pub and mosquitto_sub command line MQTT clients. It also provide free testing platform for development, more can be found at

The Mosquitto Project runs a test server at where you can test your clients in a variety of ways: plain MQTT, MQTT over TLS, MQTT over TLS (with client certificate), MQTT over WebSockets and MQTT over WebSockets with TLS. 


MQTT Windows Platform

MQTT client application development is super easy in the dot net platform. It is also very easy to test by using free eclipse foundation broker.

Before starting or trying the sample application you need to do pre work.

=>Download the Paho software from this site

=>On the other hand you can download the sample from by the link on the right side of this page.

=>Open the solution in Visual Studio and build all.

=>We are using a Windows Form application an using the to test our application.

=>We have added a reference to the library from M2Mqtt.Net project, the library can be found at:


PROJECT_FOLDER is the root where you have installed the sample.

=>Also we need JSON parser which can be downloaded as Nuget packages. We are using Newtonsoft.Json for this.

The code snippet that connect and subscribe data from the broker.

private MqttClient client;
private string clientId;
private bool isRunning = false;
private object lockObject = new object();
private string BrokerAddress = "";
private Thread workingThread;
public Form1()
    client = new MqttClient(BrokerAddress);
    // register a callback-function (we have to implement, see below) which is called by the library when a message was received
    client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
    // use a unique id as client id, each time we start the application
    clientId = Guid.NewGuid().ToString();


client_MqttMsgPublishReceived  is  call back function, this is called when data is received by the subscriber client. When data is received by this subscribing client, it is converted to the Sensor class (by JSON prser) and displayed on the UI.

 private void client_MqttMsgPublishReceived(object sender, uPLibrary.Networking.M2Mqtt.Messages.MqttMsgPublishEventArgs e)
string ReceivedMessage = Encoding.UTF8.GetString(e.Message);
SensorMeasurement sensorMeasurement = JsonConvert.DeserializeObject<SensorMeasurement>(ReceivedMessage);



MQTT C# sample example

Download the Windows sample:

Downloads  Windows Publisher and subscriber,

In the sample, we have a sensor that want to publish it’s value with some interval. This is fictitious case. We have a class for this sensor as shown in the following code snippet. It has an id, publishing time and the value at that time.

    public class SensorMeasurement
        public int IDSensor { get; set; }
        public DateTime Datetime { get; set; }
        public double Value { get; set; }



When we press on the publisher button then this application will start to publish data to the cloud and when we press the subscribe button it will start to subscribe from the cloud. When data is available then it will display it to the field.

Subscription of the data:

//Publish 700 milliseconds interval
Random random = new Random();
SensorMeasurement sensorMeasurement = new SensorMeasurement();
sensorMeasurement.IDSensor = 1;
sensorMeasurement.Datetime = DateTime.Now;
sensorMeasurement.Value = Math.Round(18 + random.NextDouble() * 5, 2);
string json = JsonConvert.SerializeObject(sensorMeasurement);
client.Publish("mytopic/test", Encoding.UTF8.GetBytes(json), MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE, false);

Download the Windows sample:


Downloads  Windows Publisher and subscriber,



MQTT in Beckhoff PLC

Beckhoff has implemented the MQTT specification in the TF6701 IoT communication module. We can easily integrate their great work into our software without any pain. If you have read or tried our Windows C# client in the previous section you may have noticed that we are using  as a free broker for testing.

We shall do the same for PLC structured text program. This PLC program will work as a publisher and subscriber and the data can be visible in TwinCAT HMI. Though we are using all in the same computer,  these applications can be located on any computer anywhere in the world.

New architecture of the sample is shown in the following image. TwinCAT client (Beckhoff PLC) can publish message to the MQTT broker and Windows client (see the previous sample) can publish or subscribe the message.  In addition we have added a HMI that can display the published/subscribed data to the UI as shown in the following image.

The following screenshoot is showing the TwinCAT HMI, part of the PLC Program and the windows Client as well.