MQTT: How Do I Write An App To Communicate With IOT Devices?
When we combine MQTT with Delphi we can easily write apps which will communicate with IOT devices. Follow along with us now as Bruno Mileto tells us how to do it.
IoT – the unseen world
Internet of Things, or IoT, is a concept that defines the connection between physical objects with the user and the internet. It makes use of intelligent sensors, in addition to software, used in the collect and transmit data to the network, allowing the control of different devices.
IoT devices communicate using IoT protocols that are a set of rules that determines how data is sent. IoT protocols ensure that information from one device or sensor is read and understood by another device, a gateway and a service.
MQTT stands for Message Queuing Telemetry Transport and it is a simple and easy implement, IoT protocol. It is a message protocol with support for asynchronous communication between the parts. It makes a light application, ideal for remote communication between devices where the amount of data communicated is highly limited, and, in networks whose bandwidth for communication is restricted and of high latency.
The MQTT protocol defines two types of entities: a message broker and numerous clients. The broker is a server that receives all messages from clients and then routes those messages to the relevant target clients. A client is anything that can interact with the broker and receive messages. A customer can be an IoT sensor in the field or an application in a data center.
- The client connects to the broker. He can subscribe to any message “topic” at the broker.
- The client publishes the messages to a topic, sending the message and topic to the broker.
- The broker then forwards the message to all customers who subscribe to that topic.
A Delphi MQTT Client Application
To create a Delphi MQTT client application, we are going to need a Delphi MQTT component. We can use the Delphi WebSocket components from eSeGeCe. The download and installation process, is detailed here.
First drop the two main components to a form, the TsgcWebSocketClient, and the TsgcWSPClient_MQTT. In the TsgcWebSocketClient component, go to properties, and in Authentication/Session/Enabled, change it to True. In Options/Parameters add ‘/ws’, in the host, add www.esegece.com, and in Port, change it to 15675.
There are a lot of servers that act like a broker, so you can use them to test, like:
- broker.hivemq.com
- test.mosca.io
- iot.eclipse.org
- mqtt.simpleml.com
But here, we will stick with the broker found at www.esegece.com
Setting up the MQTT connection
In the TsgcWSPClient_MQTT
component, go to properties, and in Authentication/Enabled, change it to True. In Client, choose the TsgcWebSocketClient
component and in HeartBeat/Interval, change it to 5.
To complete the Delphi MQTT Client Application, you will need at least:
- Three buttons: two to subscribe and unsubscribe to a topic, and one to publish a message.
- Two
TEdit
, to inform the topic to subscribe and the message to publish. - A way to inform the quality of service. We do this in a
TComboBox
component
Quality of service levels
The Quality of Service (QoS) level is an agreement between the sender of a message and the receiver of a message that defines the guarantee of delivery for a specific message. There are 3 QoS levels in MQTT:
- At most once (0) (
mtqsAtMostOnce
) - At least once (1) (
mtqsAtLeastOnce
) - Exactly once (2) (
mtqsExactlyOnce
)
After all that, your form should look something like this:
.
Adding websockets
Now, let’s go to code. The WebSocket component made by eSeGeCe makes it easy to create an MQTT Client. The main parts of the code are the subscribe/unsubscribe and the publish actions and events. You can do just like this for the buttons
MQTT.Subscribe(txtTopicToSubscribe.text); MQTT.UnSubscribe(txtTopicToSubscribe.text); MQTT.Publish(txtTopicToPublish.text, txtMessageToPublish.text, TmqttQoS(cboQoS.ItemIndex), true);
|
For the events, just add what you receive to the TMemo
:
{ MQTT – MQTTSubscribe Event }
memoLog.Lines.Add(‘#Subscribe: ‘ + IntToStr(aPacketIdentifier));
{ MQTT – MQTTUnSubscribe Event }
memoLog.Lines.Add(‘#UnSubscribe: ‘ + IntToStr(aPacketIdentifier));
{ MQTT – MQTTPublish Event } memoLog.Lines.Add(aTopic + ‘: ‘ + aText);
{ MQTT – MQTTSubscribe Event } memoLog.Lines.Add(‘#Subscribe: ‘ + IntToStr(aPacketIdentifier));
{ MQTT – MQTTUnSubscribe Event } memoLog.Lines.Add(‘#UnSubscribe: ‘ + IntToStr(aPacketIdentifier)); |
That’s all you need to do! You can check the whole code at https://github.com/brunomileto/mqtt_demo