Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
C++CodeDelphiRAD Studio

MQTT: How Do I Write An App To Communicate With IOT Devices?

smartmockups_kpbqfw4t

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.

What is MQTT ?

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.

  1. The client connects to the broker. He can subscribe to any message “topic” at the broker.
  2. The client publishes the messages to a topic, sending the message and topic to the broker.
  3. 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:

MQTT example screen shot

.

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

For the events, just add what you receive to the TMemo:

That’s all you need to do! You can check the whole code at https://github.com/brunomileto/mqtt_demo



What's New for RAD Studio 11

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

Related posts
C++Code

The Top 6 Picks From Last Week On LearnCPlusPlus.org

C++Code

Learn To Use MQTT API, The IoT Messaging Standard In C++

DelphiRAD Studio

Powerful Enterprise-Grade Real-Time Streaming For IoT

C++CodeDelphi

Implementing A Blazing Fast IoT Network With MQTT

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Worth reading...
Example WebSockets Server And Client Chat Application

IN THE ARTICLES