Site icon Embarcadero RAD Studio, Delphi, & C++Builder Blogs

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

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;

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,

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.

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 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.

  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,
[crayon-676d0afa2a37f909211845/]

How to subscribe and unsubscribe to an MQTT server?

[crayon-676d0afa2a38e401652788/]

How to publish, receive and display an MQTT message?

[crayon-676d0afa2a390269721820/]

Here is an example of handling other events of TiotMQTT

Please see their full example for more details.

[crayon-676d0afa2a392349827254/]

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

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.

Exit mobile version