Author Topic: Anyone had any luck getting MQTT to work within AdvancedHMI?  (Read 929 times)

rob1970

  • Newbie
  • *
  • Posts: 18
    • View Profile
Anyone had any luck getting MQTT to work within AdvancedHMI?
« on: October 04, 2023, 02:19:49 PM »
I've been trying to get MQTT to work within AdvancedHMI with no luck as of yet. Has anyone been successful with this?

rob1970

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Anyone had any luck getting MQTT to work within AdvancedHMI?
« Reply #1 on: October 05, 2023, 11:28:15 PM »
 I got it to work! At least the subscribe end. If interested I will post how I did it.
« Last Edit: October 06, 2023, 12:02:46 AM by rob1970 »

dmroeder

  • Global Moderator
  • Full Member
  • *****
  • Posts: 206
    • View Profile
Re: Anyone had any luck getting MQTT to work within AdvancedHMI?
« Reply #2 on: October 06, 2023, 12:45:59 AM »
If it wasn't obvious to you, then there are definitely others, you should post your solution.  I'm definitely curious.

rob1970

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Anyone had any luck getting MQTT to work within AdvancedHMI?
« Reply #3 on: October 10, 2023, 05:57:19 AM »
First right click on AdvancedHMI in the Solution Explorer tab. Select Manage NuGet Packages. Click the Browse tab and search for "Plt.M2Mqtt" and install it.
At the very top of the coding page insert this:
Code: [Select]
Imports System
Imports System.Reflection.Emit
Imports System.Text
Imports System.Windows.Forms.VisualStyles.VisualStyleElement
Imports uPLibrary.Networking.M2Mqtt
Imports uPLibrary.Networking.M2Mqtt.Messages

At the beginning of the Public Class MainForm Declare the Client variable as follows:
Code: [Select]
Dim client As MqttClient

I also declared my variables that I moved subscribed topic messages to.
Code: [Select]
Dim client As MqttClient
 Dim t1_temp As String
 Dim t1_humidity As String

On the Private Sub MainForm_Load I inserted the following:
Code: [Select]
Try
     client = New MqttClient("<Broker IP>", 1883, False, Nothing, Nothing, MqttSslProtocols.None)

     Dim clientId As String = "HMI"

     AddHandler client.MqttMsgPublishReceived, AddressOf Client_MqttMsgPublishReceived
     AddHandler client.ConnectionClosed, AddressOf Client_Disconnect

     client.Connect(clientId, "<user name>", "<password>")

 Catch ex As Exception
     ToolStripStatusLabel1.Text = "Error mqtt connect."
     MsgBox(ex.Message(), MsgBoxStyle.Critical)
 End Try

 Try
     Dim topics() As String = {"t1_temp", "t1_humidity"}
     Dim qosLevels() As Byte = {MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE, MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE}
     client.Subscribe(topics, qosLevels)

 Catch ex As Exception
     ToolStripStatusLabel1.Text = "Error mqtt subcribe"
     MsgBox(ex.Message, MsgBoxStyle.Critical)
 End Try


I then added this sub:
Code: [Select]
Private Sub Client_MqttMsgPublishReceived(ByVal sender As Object, ByVal e As MqttMsgPublishEventArgs)
    If e.Topic = "t1_temp" Then
        t1_temp = System.Text.Encoding.UTF8.GetString(e.Message)
    ElseIf e.Topic = "t1_humidity" Then
        t1_humidity = System.Text.Encoding.UTF8.GetString(e.Message)

    End If
End Sub



For Publishing you can set it up in a clicked sub or a timer:

Code: [Select]
Try
     Dim Qos As Byte = 1
     client.Publish("<Topic>", Encoding.Default.GetBytes(<Variable that holds message> ), Qos, False)

 Catch ex As Exception
     ToolStripStatusLabel1.Text = "Error"
     MsgBox(ex.Message, MsgBoxStyle.Critical)
 End Try
« Last Edit: October 15, 2023, 08:06:56 AM by Archie »

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5262
    • View Profile
    • AdvancedHMI
Re: Anyone had any luck getting MQTT to work within AdvancedHMI?
« Reply #4 on: October 15, 2023, 08:06:27 AM »
Nice detailed explanation. I am interested in trying this.

BTW, I reformatted your post to make it easier to follow by using the [code ]  [/code ] tags