General Category > Open Discussion

Anyone had any luck getting MQTT to work within AdvancedHMI?

(1/1)

rob1970:
I've been trying to get MQTT to work within AdvancedHMI with no luck as of yet. Has anyone been successful with this?

rob1970:
 I got it to work! At least the subscribe end. If interested I will post how I did it.

dmroeder:
If it wasn't obvious to you, then there are definitely others, you should post your solution.  I'm definitely curious.

rob1970:
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: ---Imports System
Imports System.Reflection.Emit
Imports System.Text
Imports System.Windows.Forms.VisualStyles.VisualStyleElement
Imports uPLibrary.Networking.M2Mqtt
Imports uPLibrary.Networking.M2Mqtt.Messages

--- End code ---

At the beginning of the Public Class MainForm Declare the Client variable as follows:

--- Code: ---Dim client As MqttClient

--- End code ---

I also declared my variables that I moved subscribed topic messages to.

--- Code: --- Dim client As MqttClient
 Dim t1_temp As String
 Dim t1_humidity As String

--- End code ---

On the Private Sub MainForm_Load I inserted the following:

--- Code: --- 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

--- End code ---


I then added this sub:

--- Code: ---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

--- End code ---



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


--- Code: --- 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

--- End code ---

Archie:
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

Navigation

[0] Message Index

Go to full version