Private MQTT As MqttClient
Private MQTTUser As String = "teste"
Private MQTTPassword As String = "00000000"
Private MQTTServerURI As String = "tcp://broker.hivemq.com:1883"'change for other broker
'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.
'Connect to CloudMQTT broker
Sub MQTT_Connect
MQTT_Nome=Nome_Text3.Text
MQTT_Senha=Senha_Text4.Text
MQTT_Url=Url_Text1.Text
MQTT_Port=Port_Text2.Text
Dim ClientId As String = Rnd(0, 999999999) 'create a unique id
MQTT. Initialize("mqtt",MQTT_Url & ":" & MQTT_Port , ClientId)
Dim ConnOpt As MqttConnectOptions
ConnOpt.Initialize(MQTT_Nome,MQTT_Senha)
MQTT.Connect2(ConnOpt)
End Sub
'MQTT CONNECT AND SUBSCRIBE TO TOPIC
Sub MQTT_Connected (Success As Boolean)
If Success = False Then
Log(LastException)
xui.MsgboxAsync("Error de Conexão","")
Else
Conectar.Text = "Conectado"
Conectar.TextColor = xui.Color_Red
MQTT.Subscribe("Control_Rele8/#", 1)'change the name of the topic!
MQTT.Subscribe("Relay_0",1)'change the name of the topic!
MQTT.Subscribe("Relay_1",1)'change the name of the topic!
MQTT.Subscribe("Relay_2",1)'change the name of the topic!
MQTT.Subscribe("Relay_3",1)'change the name of the topic!
MQTT.Subscribe("Relay_4",1)'change the name of the topic!
MQTT.Subscribe("Relay_5",1)'change the name of the topic!
MQTT.Subscribe("Relay_6",1)'change the name of the topic!
MQTT.Subscribe("Relay_7",1)'change the name of the topic!
MQTT.Subscribe("Relay_8",1)'change the name of the topic!
MQTT.Subscribe("Temp_Ds",1)'<--- ADD
End If
End Sub
Private Sub Conectar_Click
If Conectar.Text = "Conectado" Then
xui.MsgboxAsync("Você está Conectado!","Casa Controle 1")
Else
MQTT_Connect
End If
End Sub
Sub mqtt_MessageArrived(Topic As String, payload()As Byte)
If Conectar.Text = "Conectado" Then
Temp = BytesToString(payload,0,payload.Length,"UTF-8")
If Topic = "Temp_Ds" Then
Label4.Text = Temp & " °C"
End If
End If
End Sub
Private Sub Desconectar_Click
If MQTT.IsInitialized Then
MQTT.Close
End If
ExitApplication
End Sub