Hi . I have ran this code and the mqtt connection is connected ( i know this because my logger shows "Berhasil Konek" and "Connected". Then it will subscribe to mytopic. I am trying to send data as publisher from MyMQTT app on another phone, and get the data by using serializator then i will show the data through label.
I think there is problem in mqtt_MessageArrived because there is an error in logger showing :
I am sorry i couldnt find a way to copy the logger error, so i capture it
Please comment. I do appreciate any given comments and feedbacks . Thanks a lot
I think there is problem in mqtt_MessageArrived because there is an error in logger showing :
I am sorry i couldnt find a way to copy the logger error, so i capture it
Please comment. I do appreciate any given comments and feedbacks . Thanks a lot
B4X:
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