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

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

mqtt

Do you want to learn to use MQTT to talk to IoT devices? Do you want to learn how IoT devices message each other? Do you simply want to know what MQTT is? How can we use the MQTT protocol with our IoT devices? How to use MQTT with IPWorks IoT for C++ software. In this post, we will answer all these questions.

What is MQTT?

MQTT is a messaging standard protocol that can be used by IoTs with different programming languages. In other terms, it is a Client-Server publish/subscribe messaging transport protocol. It is a very simple protocol to use and designed so as to be easy to implement in many programming languages including C++ and Delphi; it is lightweight, open-source.  These features make it ideal for use in many situations, including constrained environments such as for communication in Machine to Machine (M2M) and Internet of Things (IoT) contexts where a small code footprint is required and/or network bandwidth is at a premium.

A list of MQTT features

Some of the features are can be listed as follows

  • MQTT clients are very small, require minimal resources so can be used on small microcontrollers. MQTT message headers are small to optimize network bandwidth.
  • MQTT allows for messaging between device to cloud and cloud to device. This makes for easy broadcasting messages to groups of things.
  • MQTT can scale to connect with millions of IoT devices.
  • MQTT has 3 defined quality of service levels: 0 – at most once, 1- at least once, 2 – exactly once  Because reliability of message delivery is important for many IoT use cases.
  • MQTT’s support for persistent sessions reduces the time to reconnect the client with the broker and many IoT devices connect over unreliable cellular networks.
  • MQTT makes it easy to encrypt messages using TLS and authenticate clients using modern authentication protocols, such as OAuth.
  • MQTT is an OASIS standard and it’s current MQTT Version is 5.0. The MQTT Specifications are managed by the OASIS MQTT Technical Committee. MQTT Libraries for different programming languages ( including C++ and Delphi) can be downloaded from the official software section.

IPWorks for C++ Builder / Delphi

/n Software is a software company for more than 25 years, and according to them for nearly most of Fortune 500 and Global 2000 company worldwide have used /n software products to build powerful connected applications. One of the most popular their product is IPWorks, it has very suitable components for C++ Builder that developers can use them as a internet component/library worldwide. IPWorks makes it easy to use connection protocols and eliminates much of the complexity of developing connected applications by providing easy-to-use programmable components that facilitate tasks such as sending email, transferring files, browsing the Web, consuming XML Web services, and much more.

Learn To Use MQTT with IPWorks IoT 2020 C++ Builder / Delphi Edition

IPWorks IoT 2020 C++ Builder Edition is a component library that we can easily implement Internet of Things (IoT) communications protocols in our applications on any platform with this lightweight component library. One of the most popular protocol today is MQTT which is an messaging standard protocol that can be used by IoTs with different programming languages in. IPWorks IoT 2020 C++ Builder Edition has TiotMQTT component class in that this edition. We can easily establish our MQTT connections with very simple methods of this component.

Installation

IPWorks IoT 2020 C++ Builder Edition supports from C++ Builder XE4 to C++ Builder 10.4 versions, including Seattle, Berlin, Tokyo, Rio and Sydney versions). It also allows you to select multiple version installations as below;

Learn To Use MQTT - the C++ Builder Setup screen

A trial version of the MQTT components for C++ can be downloaded from here and we can do a MQTT test connection on their server. Download and install the component.

IPWorks IoT 2020 C++ Builder Edition runs on the latest 10.4 C++ Builder version, it has necessary components that can be used for IoT and/or IoT to Desktop App, Desktop to IoT apps. TiotAMQP, TiotAMPQPClassic, TiotCoAP, TioTCoAP, TiotJSON, TiotMQTT, TiotMQTTSN, TiotSTOMP, TiotXMPP components as given below,

The components available from IPWorks so we can learn To use MQTT

TiotMQTT is very easy to use and the MQTT example runs well. It has these properties that can be modified or can be used to get information.

Learn To Use MQTT - the object inspector

During connections and messaging with server or IoTs, we can check events of IoTs or Server. There are very useful event methods as given example event names below,

Learn To Use MQTT component events

Learn to use MQTT with IPWorks IoT 2020 C++ Builder Edition

IPWorks IoT 2020 C++ Builder Edition comes with AMQP, MQTT and STOMP Demos under “C:Program FilesnsoftwareIPWorks IoT 2020 C++ Builder Edition” folder, to compile and run these I highly recommend you move this folder to your Documents or other directories which has read and write permissions to generate compilation outputs.

Learn To Use MQTT with this MQTT demo screen
  1. Create a new VCL or FMX application, save all project and unit files into a folder.
  2. The IPWorks IoT library doesn’t support CLANG compiler yet, I have tested and I had errors. Be sure that in your project settings Project->Options->C++ Compiler “Use ‘classic’ Borland compiler is Checked true. Both VCL and FMX compile successfully.
  3. Drag TiotMQTT component to your form.
  4. Add a new button , double click to create OnClick() event and add lines to do MQTT connection as below,

How to subscribe and unsubscribe to an MQTT server?

How to publish, receive and display an MQTT message?

Here is an example of handling other events of TiotMQTT

Please see their full example for more details.

Here is my simple app to test MQTT, you can also check their official example as given above.

Learn To Use MQTT example output screen

Learn to use MQTT – what we’ve learned

I think given C++ Builder examples above are good to give idea to Delphi developers. If you have problems on coding please check their official MQTT examples. Please check all IP Works Delphi components here and here for the Iot Library

Final Words

As you see IP Works IoT Library is awesome. It is very easy to connect, to subscribe, unsubscribe, publish and receive messages. It has QOS options on published messages. Please check their site here to get more information, demo version and check its licensed version. Note that licensed version it is priced, price would be okay for USA standards, might be little bit high for global developers world wide, you can try other ways to do MQTT connections. /n software and IP Works are great brands and supports most of Delphi and C++ Builder editions, including the latest ones.

There are some old MQTT connectivity libraries, you might have some issues. I’ve spend a lot of time to do MQTT easily, especially for the C++ Builder and this IP Works IoT Library is very easy to use and supports both Delphi and C++ Builder.


C++ Builder is the easiest and fastest C and C++ IDEs for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. There is a free C++ Builder Community Edition for students, beginners and startups, it can be downloaded from here and the professional developers can use the Professional, Architect or Enterprise versions of C++ Builder which has trial version and can be downloaded from here.


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

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.

2 Comments

Leave a Reply

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

IN THE ARTICLES