I am trying to establish connection between my b4j-bridge to mqtt broker (broker.hivemq.com) using nanopi . I found something strange here. If I connect b4j-bridge to nanopi, the mqtt connection will failed ( I see this using the log "failed"), but when I disconnected the b4j bridge and compile the program, it says connected and I can receive the message that is being published. However, I need to connect b4j-bridge to my nanopi because I want to access the GPIO for button and LEDS.
If you guys notice something strange in my program, please comment . I am looking forward to your comments and feedbacks.
here is my code :
If you guys notice something strange in my program, please comment . I am looking forward to your comments and feedbacks.
here is my code :
B4X:
'Non-UI application (console / server application)
#Region Project Attributes
#CommandLineArgs:
#MergeLibraries: True
#End Region
Sub Process_Globals
Private Mqtt As MqttClient
Private MQTTServerURI As String = "tcp://52.58.80.28:1883"
Private MQTTConnected As Boolean
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.
End Sub
Sub AppStart (Args() As String)
Log("hai")
mqtt_Connect
StartMessageLoop
End Sub
Sub mqtt_Connect
Dim clientId As String = "bed1"
Mqtt.Initialize("mqtt",MQTTServerURI,clientId)
Mqtt.Connect
End Sub
Private Sub mqtt_Connected (Success As Boolean)
If Success = True Then
MQTTConnected = Success
Log("Connected")
Mqtt.Publish(mytopic,"Berhasil000".GetBytes("UTF8"))
'Mqtt.Subscribe(mytopic,0)
Log("done")
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"))
End Sub
'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
Return True
End Sub