Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Private MQTT As MqttClient
Private MQTTUser As String = "xxxxxxxx" 'Tu Usuario
Private MQTTPassword As String = "xxxxxxxx" 'Tu pwd
Private MQTTServerURI As String = "tcp://blablabla.cloudmqtt.com:10672"
Private MQTTOpt As MqttConnectOptions
End Sub
Sub MQTT_Connect
Dim ClientId As String = Rnd(0, 999999999) 'create a unique id
MQTT.Initialize("MQTT", MQTTServerURI, ClientId)
MQTTOpt.Initialize(MQTTUser, MQTTPassword)
MQTT.Connect2(MQTTOpt)
End Sub
Sub MQTT_Connected (Success As Boolean)
If Success = False Then
Log(LastException)
LabelConexion.Text = "Error de conexion"
LabelConexion.Color = Colors.Red
Else
LabelConexion.Text = "Conectado"
LabelConexion.Color = Colors.Green
'MQTT.Subscribe("temperatura", 0)
'MQTT.Subscribe("humedad", 0)
'MQTT.Subscribe($"${MQTTUser}/temperatura/#"$, 0)
MQTT.Subscribe("#", 0)
End If
End Sub
Private Sub MQTT_MessageArrived (Topic As String, Payload() As Byte)
If Topic.Contains("datos") Then
Dim t,h As String
Dim Txt As String = BytesToString(Payload,0,Payload.Length,"UTF-8")
Dim myans() As String = SplitText(Txt) 'Recibo datos en dos lineas
t = myans(0)
h = myans(1)
LabelTemperatura.Text = t & " ºC"
LabelHumedad.Text = h & " %"
End If
Sub SplitText(Text As String) As String()
Dim sText() As String
sText = Regex.Split(CRLF, Text)
Return sText
End Sub