In this post, you’ll learn about IoT protocols and how IoT devices communicate with one another. What exactly is MQTT? How can we use the MQTT protocol with our Internet of Things devices? By learning MQTT connections with IPWorks IoT Library, it will help you build C++ applications with the use of C++ IDE.
Table of Contents
MQTT, Messaging Standard for IoTs
MQTT is an messaging standard protocol that can be used by IoTs with different programming languages in. In another term, 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 light weight, open. 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.
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
/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 and components for Delphi that developers can use them as a internet component/library worldwide. IPWorks makes easy to use connection protocols, 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.
IPWorks IoT 2020 C++ Builder 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;
Trial version 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 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 it has very useful event methods as given example event names below,
How to make an MQTT Connection with IPWorks IoT 2020 C++ Builder Edition
IPWorks IoT 2020 C++ Builder Edition comes with AMQP, MQTT and STOMP Demos under “C:\Program Files\nsoftware\IPWorks 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.
- Create a new VCL or FMX application, save all project and unit files into a folder
- As I see 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 compiles successfully.
- Drag TiotMQTT component to your form
- Add a new button , double click to create OnClick() event and add lines to do MQTT connection as below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
void __fastcall TForm1::Button1Click(TObject *Sender) { try { iotMQTT1->CleanSession = true; // Set CleanSession ON iotMQTT1->SSLEnabled = false; // Enable SSL iotMQTT1->ClientId = L"client1"; // Client ID iotMQTT1->User = L"CppBuilder_User1"; // User name if needed iotMQTT1->Password = L""; // Password if needed iotMQTT1->Connect( L"test.mosquitto.org", 1883); // Connect to HOST with PORT NUMBER } catch (Exception *e) // Log the error { Memo1->Lines->Add(e->Message); } } |
Subscribing and Unsubscribing to MQTT server,
1 2 3 4 5 6 7 8 9 10 11 |
void __fastcall TForm1::Button2Click(TObject *Sender) { iotMQTT1->Subscribe("nsoftwaretest", 0); // 0, 1, 2 } //--------------------------------------------------------------------------- void __fastcall TForm1::Button3Click(TObject *Sender) { iotMQTT1->Unsubscribe("nsoftwaretest"); } |
Example to other events of TiotMQTT, please see their full example for more details.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
void __fastcall TForm1::iotMQTT1SSLServerAuthentication(TObject *Sender, TiotMQTTSSLServerAuthenticationEventParams *e) { e->Accept = true; } //--------------------------------------------------------------------------- void __fastcall TForm1::iotMQTT1Connected(TObject *Sender, TiotMQTTConnectedEventParams *e) { if (e->StatusCode == 0) { // Check for a stored session if (LowerCase(iotMQTT1->Config("SessionPresent")) == "true") { iotMQTT1->RestoreSession(sessionState); Memo1->Lines->Add("Connected"); } } else { // Connection was not successful, reset form } } //--------------------------------------------------------------------------- void __fastcall TForm1::iotMQTT1Disconnected(TObject *Sender, TiotMQTTDisconnectedEventParams *e) { sessionState = iotMQTT1->SaveSession(); } |
Publishing MQTT Message and Receiving and displaying MQTT Message,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
void __fastcall TForm1::Button4Click(TObject *Sender) { iotMQTT1->PublishMessage("nsoftwaretest", 0, Edit1->Text); // QOS Mode = 0, 1, 2 } //--------------------------------------------------------------------------- void __fastcall TForm1::iotMQTT1MessageIn(TObject *Sender, TiotMQTTMessageInEventParams *e) { Memo1->Lines->Add("Message from <" + e->Topic + "> (QoS " + IntToStr(e->QOS) + "):"); Memo1->Lines->Add(" " + e->Message); } |
MQTT Connection with IPWorks IoT 2020 Delphi Edition
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 very great and 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 spend a lot of time 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++ IDE 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 download 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.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition