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)