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 MQTTServerURI As String = "tcp://37.187.106.16:1883"
Private MQTTConnected As Boolean = False
Private mytopic As String = "cynthia/status"
Private serializator As B4XSerializator
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Private Label1 As Label
Private tampung As String
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("Layout1")
Activity.LoadLayout("satu.bal")
mqtt_Connect
End Sub
Sub mqtt_Connect
Dim clientId As String = "bed1"
Mqtt.Initialize("mqtt",MQTTServerURI,clientId)
Mqtt.Connect
Log("Berhasil Konek")
End Sub
Private Sub mqtt_Connected (Success As Boolean)
If Success Then
MQTTConnected = Success
Log("Connected")
'Mqtt.Publish(mytopic,"Berhasil".GetBytes("UTF8"))
Mqtt.Subscribe(mytopic,0)
Else
MQTTConnected = False
Log("Failed")
End If
End Sub
Private Sub mqtt_Disconnected
Log("Disconnected")
End Sub
Private Sub mqtt_MessageArrived (Topic As String, Payload() As Byte)
Log("Message Arrive: ")
Log(BytesToString(Payload,0,Payload.Length,"UTF-8"))
Dim aku As Object = serializator.ConvertBytesToObject(Payload)
Dim s As String = aku
Label1.Text= s
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
[/CODE