Android Question MQTT Connection on Android Problem

ciapw

Member
Licensed User
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

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
 

Attachments

  • errorku.JPG
    84.7 KB · Views: 247

ciapw

Member
Licensed User
hi .. i use MyMqtt app to send the data. I sent haiiiiii to cynthia/status and through the logs i find out that the message was received.

Logger connected to: Meizu m2
--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Berhasil Konek
** Activity (main) Resume **
Connected
done
Message Arrive:
haiiiiii

the value of Payload are :
0 104 (0x68)
1 97 (0x61)
2 105 (0x69)
3 105 (0x69)
4 105 (0x69)
5 105 (0x69)
6 105 (0x69)
7 105 (0x69)
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
the logs i find out that the message was received
so it is working i guess

ETA

So this should work...

B4X:
Private Sub mqtt_MessageArrived (Topic As String, Payload() As Byte)
   Log("Message Arrive: ")
   Dim s As String = BytesToString(Payload,0,Payload.Length,"UTF-8")
   Label1.Text= s
End Sub
 
Last edited:
Upvote 0

ciapw

Member
Licensed User
MyMqtt app is an app that I found on google playstore which can simplify the publish and the subscribe events. So i just need to enter the topic with the message and publish it with a simple touch. I don't know whether it use B4XSerializator or not ..
What is MyMqtt app? Unless it is a B4X app that uses B4XSerializator then you cannot use serializator.ConvertBytesToObject(Payload) to deserialize the payload.
 
Upvote 0

ciapw

Member
Licensed User
Thanks a lot brother. It is working
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…