Android Question Need help on MQTT communication with android

ciapw

Member
Licensed User
I am trying to establish a communication between mqtt with my android. I ran this code and there is no problem except there is an error log stated below. i am trying to send data from my pc to my phone through mqtt. Please help. I do appreciate any comments and given feedback .
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

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 mytopic As String
    Private MQTTUser As String = "xxxxxxx"
    Private MQTTPassword As String = "_xxxxxxx"
    Private mqttserveruri As String = "tcp://37.187.106.16:1883"
    'Dim Label1 As Label
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.
    Dim Label1 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("mqttcoba.bal")
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    If FirstTime Then
        CallSub(Me,Mqtt_Connect)
    End If


End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Mqtt_Connect
    Dim ClientId As String = Rnd(0, 999999999) 'create a unique id
    mqtt.Initialize("MQTT", mqttserveruri, ClientId)

    Dim ConnOpt As MqttConnectOptions
    ConnOpt.Initialize(MQTTUser, MQTTPassword)
    mqtt.Connect2(ConnOpt)
End Sub

Sub Mqtt_Connected (Success As Boolean)

    If Success = False Then
        Log(LastException)
        Label1.Text = "Error connecting"
    Else
        Label1.Text = " - Connected to broker"
        mqtt.Subscribe("cynthia/status", 0)
    End If
End Sub
' error in log (MqttSecurityException) Not authorized to connect (5)
 

ciapw

Member
Licensed User
Hello Erel. Do you mean the starter service is the Starter with Thunder icon? So it means i have to move all the Mqtt_Connect sub to the starter (make a new sub called Mqtt_Connect in starter)?
 
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
Do you mean the starter service is the Starter with Thunder icon?
Yes it is. Named "Starter" :)
So it means i have to move all the Mqtt_Connect sub to the starter (make a new sub called Mqtt_Connect in starter)?
Yes, if mqtt is initialized in Starter (it preferably should)
 
Upvote 0

ciapw

Member
Licensed User
Erel.. i have corrected my code and just now it could run and shows "Connected to the broker". but when i run it again.. there is an error (ErrnoException) android.system.ErrnoException :isConnected failed : ECONNREFUSED
Here is the code :
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

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 mytopic As String = "cynthia/status"
    Private MQTTUser As String = ""
    Private MQTTPassword As String = ""
    Private mqttserveruri As String = "tcp://37.187.106.16:1883"
   
   
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.
    Dim Label1 As Label
    Dim Label2 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("mqttcoba.bal")   
    If FirstTime Then   
        Mqtt_Connect       
    End If   
       
End Sub

Sub Mqtt_Connect
    Dim ClientId As String = "cynthia putri" 'create a unique id
    mqtt.Initialize("MQTT", mqttserveruri, ClientId)
   
    Dim ConnOpt As MqttConnectOptions
    ConnOpt.Initialize(MQTTUser, MQTTPassword)
    mqtt.Connect2(ConnOpt)
End Sub

Sub Mqtt_Connected (Success As Boolean)   
    If Success = False Then
        Log(LastException)
        Label1.Text = "Error connecting"
    Else
        Label1.Text = " Connected to broker"
        mqtt.Subscribe(mytopic, 0)
    End If
End Sub



Sub MQTT_Disconnected
    Label1.Text = " - Disconnected from broker"
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
    If UserClosed = True Then
        If mqtt.Connected = True Then
            mqtt.Unsubscribe(mytopic)
            mqtt.Close
        End If
    End If
End Sub
 
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
but when i run it again.. there is an error
Hello again,
Probably because you only connect at FirstTime (cf. Activity_Create). This will disappear when using the Starter service as Erel suggested :)
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
Test your code with the following in your Globals.

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 mytopic As String = "cynthia/status"
    Private MQTTUser As String = ""
    Private MQTTPassword As String = ""
    Private mqttserveruri As String = "tcp://broker.hivemq.com:1883"
End Sub

Enjoy...
 
Upvote 0
Top