From Clomosy Docs

No edit summary
No edit summary
 
Line 160: Line 160:
* [[Object Properties]]
* [[Object Properties]]
* [[AddNewEvent]]
* [[AddNewEvent]]
{{#seo:|title=TclMQTT Using in Clomosy - Clomosy Docs}}
{{#seo:|description=Explore MQTT integration on Clomosy! Learn how to implement efficient messaging for IoT applications with the MQTT protocol.}}
{{#seo:|description=Explore MQTT integration on Clomosy! Learn how to implement efficient messaging for IoT applications with the MQTT protocol.}}

Latest revision as of 13:27, 24 December 2024

AComponent : Specifies the parent of the object to be defined.

xName : The name of the defined TclMqtt object should be written.

The MQTT protocol allows data transmission between devices or systems. This component can connect to an MQTT broker to perform operations like sending data (publish) or receiving data (subscribe).

Feature Use of Definition
TclMQTT MQTT1 : TclMQTT; A variable belonging to the TclMQTT class is created.
AddNewMQTTConnection MQTT1 = Form1.AddNewMQTTConnection(Panel1,'MQTT1'); A new TclMQTT object is added to the form.
Connect MQTT1.Connect; Mqtt connection is provided.
Connected MQTT1.Connected It is checked whether the connection is established or not. Returns true or false. If true, the connection has been established.
Channel MQTT1.Channel = 'clomosy';//project guid + channel The term "Channel" represents messages directed to the MQTT broker or a topic. Subject is the address where messages are published or subscribed to.
Send MQTT1.Send('Hello!'); It is used to send the desired message to the Mqtt network.
tbeOnMQTTStatusChanged Form1.AddNewEvent(MQTT1, tbeOnMQTTStatusChanged,'MyMQTTStatusChanged'); This is an event that occurs when the state of the MQTT connection changes. When an event change occurs, the event written in the 3rd parameter is triggered.
tbeOnMQTTPublishReceived Form1.AddNewEvent(MQTT1, tbeOnMQTTPublishReceived,'MyMQTTPublishReceived'); This is an event that occurs when an MQTT message is received. The event in the 3rd parameter is used to process the received message.
ReceivedAlright MQTT1.ReceivedAlright It returns true if there is an oncoming return on the network, otherwise it returns false.
ReceivedMessage MQTT1.ReceivedMessage It is used to access messages coming from the network.
ReceivedTopic MQTT1.ReceivedTopic It is a feature that returns the network path of the channel when a message arrives on the network.

Example

var
 MyForm:TclForm;
 BtnSend:TclProButton;
 MemMsg:TclMemo;
 chatSectionMemo:TclMemo;
 bigPanel,middlePanel:TclProPanel;
 bigLyt:TClLayout;
 MyMQTT : TclMQTT;

 void BtnSendClick;
 {
     if (MemMsg.Text == '')
     {
         ShowMessage('Write a message!');
     }
     else
     {
        MyMQTT.Send(MemMsg.Text);
        chatSectionMemo.Lines.Add(MemMsg.Text);
         chatSectionMemo.Lines.Add('');
   chatSectionMemo.ScrollTo(0,chatSectionMemo.Lines.Count*chatSectionMemo.Lines.Count,True);
         MemMsg.Text = '';
     }
 }

 void MyMQTTPublishReceived;
 {
       If (MyMQTT.ReceivedAlright) 
       {
         chatSectionMemo.Lines.Add('');
         chatSectionMemo.Lines.Add('                   ' + MyMQTT.ReceivedMessage);
     chatSectionMemo.ScrollTo(0,chatSectionMemo.Lines.Count*chatSectionMemo.Lines.Count,True);
       }
 }
 
{
 MyForm = TclForm.Create(Self);

 bigLyt = MyForm.AddNewLayout(MyForm,'bigLyt');
 bigLyt.Align=alContents;
 bigLyt.Margins.Left=10;
 bigLyt.Margins.Right=10;
 bigLyt.Margins.Top=10;
 bigLyt.Margins.Bottom=30;

 MyMQTT = MyForm.AddNewMQTTConnection(MyForm,'MyMQTT');
MyForm.AddNewEvent(MyMQTT,tbeOnMQTTPublishReceived,'MyMQTTPublishReceived');
 MyMQTT.Channel = 'ChatAI';
 MyMQTT.Connect;

 middlePanel=MyForm.AddNewProPanel(bigLyt,'middlePanel');
 middlePanel.Align = AlTop;
 middlePanel.Width = 300;
 middlePanel.Height = 50;
 middlePanel.Margins.Right = 10;
 middlePanel.Margins.Top = 30;
 middlePanel.Margins.Left = 10;
 middlePanel.clProSettings.IsRound = True;
 middlePanel.clProSettings.RoundHeight = 10;
 middlePanel.clProSettings.RoundWidth = 10;
 middlePanel.clProSettings.BorderColor = clAlphaColor.clHexToColor('#008000');
 middlePanel.clProSettings.BorderWidth = 3;
 middlePanel.SetclProSettings(middlePanel.clProSettings);

 MemMsg= MyForm.AddNewMemo(middlePanel,'MemMsg','');
 MemMsg.Align = alTop;
 MemMsg.Margins.Right=10;
 MemMsg.Margins.Left=10;
 MemMsg.Margins.Top=10;
 MemMsg.Margins.Bottom= 10;

 bigPanel=MyForm.AddNewProPanel(bigLyt,'bigPanel');
 bigPanel.Align = AlClient;
 bigPanel.Margins.Right = 10;
 bigPanel.Margins.Left = 10;
 bigPanel.clProSettings.IsRound = True;
 bigPanel.clProSettings.RoundHeight = 10;
 bigPanel.clProSettings.RoundWidth = 10;
 bigPanel.clProSettings.BorderColor = clAlphaColor.clHexToColor('#008000');
 bigPanel.clProSettings.BorderWidth = 3;
 bigPanel.SetclProSettings(bigPanel.clProSettings);

 chatSectionMemo= MyForm.AddNewMemo(bigPanel,'chatSectionMemo','');
 chatSectionMemo.Align = alClient;
 chatSectionMemo.ReadOnly = True;
 chatSectionMemo.Margins.Top= 10;
 chatSectionMemo.Margins.Left= 10;
 chatSectionMemo.Margins.Right =10;
 chatSectionMemo.Margins.Bottom =10;
 chatSectionMemo.TextSettings.Font.Size=26;
 chatSectionMemo.TextSettings.WordWrap = True;
 chatSectionMemo.EnabledScroll = True;  


 BtnSend = MyForm.AddNewProButton(bigLyt,'BtnSend','SEND');
 BtnSend.Align = AlTop;
 BtnSend.Margins.Right = 100;
 BtnSend.Margins.Left = 100;
 BtnSend.Margins.Bottom = 8;
 BtnSend.Margins.Top = 8;
 BtnSend.clProSettings.TextSettings.Font.Style = [fsBold];
 BtnSend.clProSettings.IsRound = True;
 BtnSend.clProSettings.RoundHeight = 2;
 BtnSend.clProSettings.RoundWidth = 2;
 BtnSend.clProSettings.BorderColor = clAlphaColor.clHexToColor('#808080');
 BtnSend.clProSettings.BorderWidth = 2;
 BtnSend.SetclProSettings(BtnSend.clProSettings);
 MyForm.AddNewEvent(BtnSend,tbeOnClick,'BtnSendClick');

 MyForm.Run;
}

See Also